BONUS

In this question, we're just going to go crazy with data structures and looping.

Part A

In this question, you'll write some code that takes two lists, and creates a third list that is a collection of sums from the corresponding elements in the first two lists.

As an example, if the two input lists are [1, 2, 3] and [4, 5, 6], then the summed third list would be [5, 7, 9], where each element of the list is the sum of the corresponding elements from the other two lists.


In [ ]:
def sum_lists(list1, list2):
    sum_list = []
    
    ### BEGIN SOLUTION
    
    ### END SOLUTION
    
    return sum_list

In [ ]:
import numpy as np
a1 = np.random.random(50)
a2 = np.random.random(50)
np.testing.assert_allclose(a1 + a2, np.array(sum_lists(a1.tolist(), a2.tolist())))

Part B

In the code below, there's a function named how_many_calls that takes one argument. That argument is a number (an integer); specifically, a stopping criterion.

Your goal is to write a while loop inside the how_many_calls function. In each iteration of the while loop, it should call the function magic(), which will return an integer. You should then check to see if that integer equals stop_val; if it does, then the while loop ends. If not, you should increment calls and keep going.

The goal is to see how many times the while loop has to be executed (as counted by the calls variable) until the integer returned by magic() is the same as the stop_val argument.

To be clear: the calls variable should count the number of times the magic() function is called.


In [ ]:
def how_many_calls(stop_val):
    calls = 0
    
    ### BEGIN SOLUTION
    
    ### END SOLUTION
    
    return calls

In [ ]:
def magic():
    return np.random.randint(0, 10)

import numpy as np
np.random.seed(3849)
s1 = 5
l1 = 7
assert l1 == how_many_calls(s1)

np.random.seed(895768)
s2 = 3
l2 = 21
assert l2 == how_many_calls(s2)