Homework 2 Solutions
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: Shopping Total Cost
A shopping cart is represented as a list of 3-element tuples like this:
[(item1, cost1, quantity1), (item2, cost2, quantity2), ..., (itemN, costN, quantityN)]
Complete the function total_cost
which takes in a list that represents a shopping cart called shopping_cart
and returns the total cost of all the items before tax in that shopping cart.
def total_cost(shopping_cart):
""" Returns a float that is the total cost of all items in the shopping cart.
>>> fruit_cart = [("apple", 0.5, 3), ("banana", 0.25, 4)]
>>> total_cost(fruit_cart)
2.5
>>> cal_cart = [("oski", 1000, 1), ("go", 1.25, 2), ("bears", 3.5, 2)]
>>> total_cost(cal_cart)
1009.5
"""
return sum([price*quantity for (name, price, quantity) in shopping_cart])
Use OK to test your code:
python3 ok -q total_cost
Question 3: Shopping Cart Tax
Complete the functiontax
which takes in a list that represents a shopping cart called shopping_cart
and return a new list that also represents the same shopping cart but with a percent
tax added to the price of each item.
def tax(shopping_cart, percent):
""" Returns a new list where a `percent` tax is added to each item's price in a shopping cart.
>>> fruit_cart = [("apple", 0.5, 3), ("banana", 0.25, 4)]
>>> tax(fruit_cart, 10)
[('apple', 0.55, 3), ('banana', 0.275, 4)]
>>> cal_cart = [("oski", 1000, 1), ("go", 1.25, 2), ("bears", 3.5, 2)]
>>> tax(cal_cart, 100)
[('oski', 2000.0, 1), ('go', 2.5, 2), ('bears', 7.0, 2)]
"""
tax_multiplier= 1 + (percent / 100)
return [(name, price * tax_multiplier, quantity) for (name, price, quantity) in shopping_cart]
Use OK to test your code:
python3 ok -q tax
Question 4: Deck of cards
Write a list comprehension that will create a deck of cards, given a
list of suits
and a list of numbers
. Each
element in the list will be a card, which is represented by a 2-element list
of the form [suit, number]
.
def deck(suits, numbers):
"""Creates a deck of cards (a list of 2-element lists) with the given
suits and numbers. Each element in the returned list should be of the form
[suit, number].
>>> deck(['S', 'C'], [1, 2, 3])
[['S', 1], ['S', 2], ['S', 3], ['C', 1], ['C', 2], ['C', 3]]
>>> deck(['S', 'C'], [3, 2, 1])
[['S', 3], ['S', 2], ['S', 1], ['C', 3], ['C', 2], ['C', 1]]
>>> deck([], [3, 2, 1])
[]
>>> deck(['S', 'C'], [])
[]
"""
return [[suit, number] for suit in suits
for number in numbers]
Use OK to test your code:
python3 ok -q deck
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