Q5

In this question, we'll work exclusively with while loops.

A

In the code below, there's a function named how_many_loops that takes one argument. That argument is a number (an integer); specifically, a stopping criterion. The other function, magic, returns a random number between 0 and 10 (both inclusive).

Your goal is to write a while loop that counts how many times it needed to run before the magic function magic returned the same number as stop_val. Store that count in the loops variable and return it.


In [ ]:
import numpy as np

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

def how_many_loops(stop_val):
    loops = 0
    
    ### BEGIN SOLUTION
    
    ### END SOLUTION
    
    return loops

In [ ]:
np.random.seed(3849)
s1 = 5
l1 = 6
assert l1 == how_many_loops(s1)

np.random.seed(895768)
s2 = 3
l2 = 20
assert l2 == how_many_loops(s2)

B

In this question, you'll loop over a list, looking for the first negative number. When you find a negative number, you'll return the index of that element in the list.

So if my input list is [3, -1, 14, -2], I would return 1. If my list is [13, 68, 2, -4, 6], I would return 3. Store this value in the variable index.


In [ ]:
def first_negative(inlist):
    index = 0
    
    ### BEGIN SOLUTION
    
    ### END SOLUTION
    
    return index

In [ ]:
import numpy as np

np.random.seed(85435)
i1 = np.random.randint(-50, 50, 10).tolist()
assert 0 == first_negative(i1)

np.random.seed(9893743)
i2 = np.random.randint(-25, 75, 10000).tolist()
assert 4 == first_negative(i2)

C

Often when you're testing some boolean expression, the expression itself doesn't consist of just one check; it can consist of many conditions you're testing simultaneously. For example, just in Part B above, what would have happened if none of the numbers I passed in were negative? Rather than simply looping until you found a negative number (which would have gone on forever or until the program crashed), you could look for a negative number while also counting how many numbers you've looked at, and "giving up" after a certain number of checks to prevent going on forever.

You can string multiple boolean conditions together with the keyword and.

while x < 5 and x > 0:

In doing so, you're telling the loop to continue only as long as both conditions are True; once even one of them changes to False, break the loop.

In this problem, you'll fill in the code below for a new version of the Part B code that also maintains a counter to see how many checks it's made. If the number of checks surpass a given limit, the loop ends and the current value of index is returned.

In summary: you'll do the same thing as before, but check two conditions in your while loop: if the number you're looking at is negative, and if you've checked fewer than limit numbers, then return that index as before. But if you exceed the check limit, then just return whatever the value of index is when the loop ends.


In [ ]:
def first_negative_limit(inlist, limit):
    index = 0
    
    ### BEGIN SOLUTION
    
    ### END SOLUTION
    
    return index

In [ ]:
import numpy as np

np.random.seed(85435)
i1 = np.random.randint(-50, 50, 10).tolist()
assert 0 == first_negative_limit(i1, 10)

np.random.seed(9893743)
i2 = np.random.randint(-25, 75, 10000).tolist()
assert 3 == first_negative_limit(i2, 3)