Open Casper17-max opened 3 years ago
Hi Casper
Take a look at the cell that you ran as [80]. In each round of the loop, you printed an index and the element in a list that was associated with that index.
Now, you want to write a function that does a similar thing. I.e. it should end with
return (some_index, some_element)
However, instead of just printing/returning all indices and elements, you want to return only the index that is associated with the minimum value and that exect minimum_value. You cannot print enumerate(x)
as this is a very different type of object.
Was this helpful? Otherwise, I can provide an example.
Best, Joachim
Hi,
An example would be great.
I really don't understand the general notation or intuition for how to write it.
The following code produces neither output nor error message, so I don't understand what's wrong.
Hi Casper,
One problem in the function above is that you are calling the function minimum()
within minimum()
itself.
Consider the function below that takes a list of numbers as inputs and finds the index and value associated with the first prime number in the list. Try and explain each step for yourself and see if this helps you write the right minimum function.
Best, Joachim
def find_first_prime(my_list):
my_index, my_val = float('inf'), float('inf')
for (number_index, value) in enumerate(list_of_numbers):
if my_val == float('inf'):
cont = 0
for i in range (2,value):
if value % i == 0:
cont = 1
if cont == 0:
my_index = number_index
my_val = value
return (my_index, my_val)
list_of_numbers = [4, 9, 6, 16, 13, 3, 10, 12]
print(find_first_prime(list_of_numbers))
I think I have the minimum and index of x:![image](https://user-images.githubusercontent.com/86822716/126134206-bac9b8cd-5c1f-48c9-9407-4008686163af.png)
but I don't know how to put them together in the minimum function:![image](https://user-images.githubusercontent.com/86822716/126134352-05fa12c0-3f1c-4ae1-b8c9-95bd02a04235.png)