isdsucph / isds2021

Introduction to Social Data Science 2021 - a summer school course https://isdsucph.github.io/isds2021/
MIT License
22 stars 37 forks source link

ex. 0.3.1 #11

Open Casper17-max opened 3 years ago

Casper17-max commented 3 years ago

I think I have the minimum and index of x: image

but I don't know how to put them together in the minimum function: image

joachimkrasmussen commented 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

Casper17-max commented 3 years ago

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. image

joachimkrasmussen commented 3 years ago

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))