algorithm to Smallest odd number in a set of N numbers
Answers
Answered by
1
Answer:
Use what is called a "watermark" algorithm. First apply a filter to isolate a list of odd numbers. Then create a variable representing the lowest number of the list and set it equal to float('inf').
Loop through the list and check each element; if its lower than the current min, set the current min equal to that element. At the end, whatever the value of the current min variable is will be the minimum of your list.
example code:
lst = [] ##input your list here
m = float('Inf')
odds = list(filter(lambda n: n%2, lst))
for ele in odds:
if ele < m: m = ele
print("The smallest odd is: ", m)
Similar questions
Physics,
2 months ago
Science,
2 months ago
Math,
5 months ago
Social Sciences,
5 months ago
Environmental Sciences,
11 months ago
Math,
11 months ago