Q2
python
class VendingMachine:
"""A vending machine that vends some product for some price.
>>> v = VendingMachine('candy', 10)
>>> v.vend()
'Nothing left to vend. Please restock.'
>>> v.add_funds(15)
'Nothing left to vend. Please restock. Here is your $15.'
>>> v.restock(2)
'Current candy stock: 2'
>>> v.vend()
'Please add $10 more funds.'
>>> v.add_funds(7)
'Current balance: $7'
>>> v.vend()
'Please add $3 more funds.'
>>> v.add_funds(5)
'Current balance: $12'
>>> v.vend()
'Here is your candy and $2 change.'
>>> v.add_funds(10)
'Current balance: $10'
>>> v.vend()
'Here is your candy.'
>>> v.add_funds(15)
'Nothing left to vend. Please restock. Here is your $15.'
>>> w = VendingMachine('soda', 2)
>>> w.restock(3)
'Current soda stock: 3'
>>> w.restock(3)
'Current soda stock: 6'
>>> w.add_funds(2)
'Current balance: $2'
>>> w.vend()
'Here is your soda.'
"""
"*** YOUR CODE HERE ***" # This implimentation is too ugly, should be more compact and more functionative
def __init__(self, product_name, product_price):
self.product_name = product_name
self.stock = 0
self.product_price = product_price
self.funds = 0
def add_funds(self, funds):
if not self.stock:
return f"Nothing left to vend. Please restock. Here is your ${funds}."
else:
self.funds += funds
return f"Current balance: ${self.funds}"
def restock(self, number):
self.stock += number
return f"Current {self.product_name} stock: {self.stock}"
def vend(self):
if not self.stock:
return "Nothing left to vend. Please restock."
elif self.funds < self.product_price:
return f"Please add ${self.product_price - self.funds} more funds."
elif self.funds == self.product_price:
self.funds = 0
self.stock -= 1
return f"Here is your {self.product_name}."
else:
self.stock -= 1
return f"Here is your {self.product_name} and ${self.return_change()} change."
def return_change(self):
self.funds -= self.product_price
self.change = self.funds
self.funds = 0
return self.change
Q3
IMPROVE: Ugly code, find better solution
python
def store_digits(n):
"""Stores the digits of a positive number n in a linked list.
>>> s = store_digits(1)
>>> s
Link(1)
>>> store_digits(2345)
Link(2, Link(3, Link(4, Link(5))))
>>> store_digits(876)
Link(8, Link(7, Link(6)))
>>> store_digits(2450)
Link(2, Link(4, Link(5, Link(0))))
>>> # a check for restricted functions
>>> import inspect, re
>>> cleaned = re.sub(r"#.*\\n", '', re.sub(r'"{3}[\s\S]*?"{3}', '', inspect.getsource(store_digits)))
>>> print("Do not use str or reversed!") if any([r in cleaned for r in ["str", "reversed"]]) else None
"""
"*** YOUR CODE HERE ***"
if n < 10:
return Link(n)
tmp, length = n, 0 # Any better approach??
while tmp:
tmp //= 10
length += 1
return Link(n // pow(10, length-1) ,store_digits(n % pow(10, length-1)))
Q4
python
def deep_map_mut(func, lnk):
"""Mutates a deep link lnk by replacing each item found with the
result of calling func on the item. Does NOT create new Links (so
no use of Link's constructor).
Does not return the modified Link object.
>>> link1 = Link(3, Link(Link(4), Link(5, Link(6))))
>>> print(link1)
<3 <4> 5 6>
>>> # Disallow the use of making new Links before calling deep_map_mut
>>> Link.__init__, hold = lambda *args: print("Do not create any new Links."), Link.__init__
>>> try:
... deep_map_mut(lambda x: x * x, link1)
... finally:
... Link.__init__ = hold
>>> print(link1)
<9 <16> 25 36>
"""
"*** YOUR CODE HERE ***"
if lnk is Link.empty:
return
elif isinstance(lnk.first, Link):
deep_map_mut(func, lnk.first)
else:
lnk.first = func(lnk.first)
deep_map_mut(func, lnk.rest)
*Q5
python
def two_list(vals, counts):
"""
Returns a linked list according to the two lists that were passed in. Assume
vals and counts are the same size. Elements in vals represent the value, and the
corresponding element in counts represents the number of this value desired in the
final linked list. Assume all elements in counts are greater than 0. Assume both
lists have at least one element.
>>> a = [1, 3]
>>> b = [1, 1]
>>> c = two_list(a, b)
>>> c
Link(1, Link(3))
>>> a = [1, 3, 2]
>>> b = [2, 2, 1]
>>> c = two_list(a, b)
>>> c
Link(1, Link(1, Link(3, Link(3, Link(2)))))
"""
"*** YOUR CODE HERE ***"
if len(vals) == 1:
if counts[0] == 1:
return Link(vals[0])
else:
return Link(vals[0], two_list(vals, [counts[0] - 1]))
else:
if counts[0] == 1:
return Link(vals[0], two_list(vals[1:], counts[1:]))
else:
return Link(vals[0], two_list(vals, [counts[0]-1]+ counts[1:]))