hw04

.py

School

University of California, Berkeley *

*We aren’t endorsed by this school

Course

61A

Subject

Finance

Date

Feb 20, 2024

Type

py

Pages

3

Uploaded by JudgeScience13298

def merge(lst1, lst2): """Merges two sorted lists. >>> merge([1, 3, 5], [2, 4, 6]) [1, 2, 3, 4, 5, 6] >>> merge([], [2, 4, 6]) [2, 4, 6] >>> merge([1, 2, 3], []) [1, 2, 3] >>> merge([5, 7], [2, 4, 6]) [2, 4, 5, 6, 7] """ new_lst = [] def merge_helper(lst1, lst2): if len(lst1) == 0 or len(lst2) == 0: # Adds remainder elements of list to new_lst when one is empty and returns if len(lst1) > 0: new_lst.extend(lst1) else: new_lst.extend(lst2) return new_lst if lst1[0] < lst2[0]: new_lst.append(lst1[0]) return merge_helper(lst1[1:], lst2) else: new_lst.append(lst2[0]) return merge_helper(lst1, lst2[1:]) return merge_helper(lst1, lst2) class Mint: """A mint creates coins by stamping on years. The update method sets the mint's stamp to Mint.present_year. >>> mint = Mint() >>> mint.year 2021 >>> dime = mint.create(Dime) >>> dime.year 2021 >>> Mint.present_year = 2101 # Time passes >>> nickel = mint.create(Nickel) >>> nickel.year # The mint has not updated its stamp yet 2021 >>> nickel.worth() # 5 cents + (80 - 50 years) 35 >>> mint.update() # The mint's year is updated to 2101 >>> Mint.present_year = 2176 # More time passes >>> mint.create(Dime).worth() # 10 cents + (75 - 50 years) 35 >>> Mint().create(Dime).worth() # A new mint has the current year 10 >>> dime.worth() # 10 cents + (155 - 50 years) 115 >>> Dime.cents = 20 # Upgrade all dimes! >>> dime.worth() # 20 cents + (155 - 50 years) 125
""" present_year = 2021 def __init__(self): self.update() def create(self, coin): return coin(self.year) def update(self): self.year = Mint.present_year class Coin: def __init__(self, year): self.year = year def worth(self): coin_age = Mint.present_year - self.year if coin_age > 50: return self.cents + (coin_age - 50) else: return self.cents class Nickel(Coin): cents = 5 class Dime(Coin): cents = 10 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() 'You must add $10 more funds.' >>> v.add_funds(7) 'Current balance: $7' >>> v.vend() 'You must 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)
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help