Hi
Is there an equivalent of float(‘inf’) in Python to use in min operations to find the smallest value in a loop?
Thanks
Hi
Is there an equivalent of float(‘inf’) in Python to use in min operations to find the smallest value in a loop?
Thanks
I’m not sure if it is a Python 2.X thing but, you can use float('Infinity')
.
But I would use just a big number like 1e100
The answer is to set the minimum value to the first element in the array/list then check it against all the other elements, updating the minimum value if we find a smaller value. Can use similar approach for maximum values.
This algorithm has the advantage in that it makes no assumptions about the values in the array/list so will work in all cases provided all the elements are the same type and are comparable.