isdsucph / isds2022

Introduction to Social Data Science 2022 - a summer school course https://isdsucph.github.io/isds2022/
MIT License
21 stars 23 forks source link

Problem 0.3.1 #7

Closed Frejafalk closed 2 years ago

Frejafalk commented 2 years ago

We have tried to solve problem 0.3.1, but are stuck with this and can't move forward.. Can you help us how we should set up the code with index and store it in the answer_031 variable afterwards? And is this even correct so far?


my_list=[-342, 195, 573, -234, 762, -175, 847, -882, 153, -22]

c_enumerate = enumerate(my_list)

def minimum(x,y): x_min= min(my_list) return my_list, x_min

print('answer_031 ', minimum(c_enumerate, my_list))


We get this result: answer_031 ([-342, 195, 573, -234, 762, -175, 847, -882, 153, -22], -882)


Thank you!

isdsucph commented 2 years ago

Hi @Frejafalk

Your function minimum should only take one argument as input, the list of numbers. The output should be a tuple with the index of the smallest number and the smallest number. Your function definition should therefore be def minimum(numbers) where numbers is the list of numbers given in the assignment text. And your function's return statement could be ("could" because you are free to name the variables whatever you like) return idx_min_value, min_value where idx_min_value is the index of the smallest value in the list of numbers and min_value is the smallest number. The function will therefore look like

def minimum(numbers):
    # Code goes here 
    return idx_min_value, min_value

The line x_min = min(my_list) that you write is a big step in the direction of the solution. Ignore the hint from the assignment with the enumerate function. There is a list method which takes as input a value and returns the index of the specified value (if the value exists in the list, else it returns a ValueError). E.g.

animals = ["Cat", "Dog", "Horse"] 
animals.index("Horse")
# output: 2 

The line animals.index("Horse") outputs the number 2 while "Horse" is the third element in the list animals. Apply this method inside the minimum function on the minimum value found and you should get the index of the minimum value. Remember to store the solution in a variable named answer_031 i.e. answer_031 = minimum(numbers).

Cheers Jonas