Q3

In this question, we'll focus on looping.

A

In the question below, you'll write code that generates a new list, reverse, that is the same as the input list except with its elements backwards relative to the original.

As an example, if the input list is [1, 2, 3], the output list reverse should be [3, 2, 1].


In [ ]:
def reverse_list(inlist):
    reverse = []

    ### BEGIN SOLUTION
    
    ### END SOLUTION
    
    return reverse

In [ ]:
t1 = [1, 2, 3]
t2 = [3, 2, 1]
assert set(t2) == set(reverse_list(t1))

t1 = [2, 6, 52, 64, 3.4, "something", 3, 6.2]
t2 = [6.2, 3, "something", 3.4, 64, 52, 6, 2]
assert set(t1) == set(reverse_list(t2))

B

In this question, you'll implement a piece of the Fibonacci sequence. Specifically, you'll fill in code that's required to compute the next number in the sequence, given a list of all previous numbers.

Remember how the Fibonacci sequence works: the $n^{th}$ number in the sequence is the sum of numbers $n - 1$ and $n - 2$. So if you're given an input list of just the first two numbers, [1, 1], then you should compute 2. If you're given a list of the first five numbers, [1, 1, 2, 3, 5], then you should return 8.

Use the variable n to store the next number in the sequence.


In [ ]:
def next_fib(prev_fibs):
    n = -1
    
    ### BEGIN SOLUTION
    
    ### END SOLUTION
    
    return n

In [ ]:
i1 = [1, 1]
a1 = 2
assert a1 == next_fib(i1)

In [ ]:
i2 = [1, 1, 2, 3, 5, 8, 13, 21, 34]
a2 = 55
assert a2 == next_fib(i2)