Solutions: You can find the file with solutions for all questions here.
Questions
Question 1: Fibonacci
The Fibonacci sequence is a famous sequence in mathematics. The first element in the sequence is 0 and the second element is 1. The nth element is defined as Fn = Fn-1 + Fn-2.
Implement the fib
function, which takes an integer n
and returns
the n
th Fibonacci number. Use a while
loop in your solution.
def fib(n):
"""Returns the nth Fibonacci number.
>>> fib(0)
0
>>> fib(1)
1
>>> fib(2)
1
>>> fib(3)
2
>>> fib(4)
3
>>> fib(5)
5
>>> fib(6)
8
>>> fib(100)
354224848179261915075
"""
curr, next = 0, 1
while n > 0:
curr, next = next, curr + next
n -= 1
return curr
Use OK to test your code:
python3 ok -q fib
Question 2: Nonzero
Write a function that takes in a list and returns the first nonzero entry.
def nonzero(lst):
""" Returns the first nonzero element of a list
>>> nonzero([1, 2, 3])
1
>>> nonzero([0, 1, 2])
1
>>> nonzero([0, 0, 0, 0, 0, 0, 5, 0, 6])
5
"""
for i in lst:
if i != 0:
return i
Use OK to test your code:
python3 ok -q nonzero
Question 3: Contains N
Write a function that takes in a list and a number, and returns whether or not the list contains the value n.
def has_n(lst, n):
""" Returns whether or not a list contains the value n.
>>> has_n([1, 2, 2], 2)
True
>>> has_n([0, 1, 2], 3)
False
>>> has_n([], 5)
False
"""
for elem in lst:
if elem == n:
return True
return False
Use OK to test your code:
python3 ok -q has_n
Question 4: Total Price
Implement the function total_price
, which takes in a list of prices of individual products and needs to find the total price. Unfortunately, any product that is priced greater than or equal to $20 has a 50 percent tax, so include that in the final price.
Try to do this in one line!
Cast your final answer to an integer to avoid floating point precision errors. For example, if x
contains your final answer, return int(x)
!
def total_price(prices):
"""
Finds the total price of all products in prices including a
50% tax on products with a price greater than or equal to 20.
>>> total_price([5, 20, 30, 7])
87
>>> total_price([8, 4, 3])
15
>>> total_price([10, 100, 4])
164
"""
return int(sum([x if x < 20 else 1.5*x for x in prices]))
Use OK to test your code:
python3 ok -q total_price
Question 5: arange
Implement the function arange
, which behaves just like np.arange(start, end, step) from Data 8. You only need to support positive values for step.
def arange(start, end, step=1):
"""
arange behaves just like np.arange(start, end, step).
You only need to support positive values for step.
>>> arange(1, 3)
[1, 2]
>>> arange(0, 25, 2)
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24]
>>> arange(999, 1231, 34)
[999, 1033, 1067, 1101, 1135, 1169, 1203]
"""
value = start
result = []
while value < end:
result.append(value)
value += step
Use OK to test your code:
python3 ok -q arange
Question 6: Reverse (iteratively)
Write a function reverse_iter_for
that takes a list and returns a new
list that is the reverse of the original using a for
loop. You should not
need any indexing notation.
def reverse_iter_for(lst):
"""Returns the reverse of the given list.
>>> reverse_iter_for([1, 2, 3, 4])
[4, 3, 2, 1]
"""
rev_lst = []
for e in lst:
rev_lst = [e] + rev_lst
return rev_lst
Use OK to test your code:
python3 ok -q reverse_iter_for
Complete the function reverse_iter_while
that behaves identically to
reverse_iter_for
but is implemented as using a while
loop.
You may use indexing or slicing notation. Do not use lst[::-1]
!
def reverse_iter_while(lst):
"""Returns the reverse of the given list.
>>> reverse_iter_while([1, 2, 3, 4])
[4, 3, 2, 1]
"""
rev_lst = []
i = 0
while i < len(lst):
rev_lst = [lst[i]] + rev_lst
i += 1
return rev_lst
Use OK to test your code:
python3 ok -q reverse_iter_while