Can anyone help me with the logic of this function?
This function should get an integer vector and return the smallest value within that vector.
Ex: minimo([1,2,3,5,8,9,0,-2]) -> -2
Can anyone help me with the logic of this function?
This function should get an integer vector and return the smallest value within that vector.
Ex: minimo([1,2,3,5,8,9,0,-2]) -> -2
The logic of your function would look like this:
You create a minimo
variable that has the value of the first element in the list (if you do not want to use the same function name, call min
or something else).
Then you go through the rest of the list element by element, replacing the value of minimo
with the element if it is less than minimo
current.
At the end you return minimo
.
For the function to be more resistant to errors you can at the beginning of the function check if the list is empty, and then throw an exception or return a special value.
You may also want to check the type of the parameter lista
, after all it may not be a list, it may not be an iterable, or it may not contain integers. These things can cause run-time error in dynamic languages.
To build this function you need to know the following language elements: for
, if
, lists, slices and how to create functions.