hw04
py
keyboard_arrow_up
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)
>>> 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.'
"""
balance = 0
product_stock = 0
def __init__(self, product, price):
self.product = product
self.price = price
def vend(self):
if self.product_stock == 0:
return f'Nothing left to vend. Please restock.'
elif self.balance < self.price:
return f'You must add ${self.price - self.balance} more funds.'
elif self.balance == self.price:
self.balance = 0
self.product_stock -= 1
return f'Here is your {self.product}.'
else:
change = self.balance - self.price
self.balance = 0
self.product_stock -= 1
return f'Here is your {self.product} and ${change} change.'
def add_funds(self, funds):
if self.product_stock == 0:
return f'Nothing left to vend. Please restock. Here is your ${funds}.'
else:
self.balance += funds
return f'Current balance: ${self.balance}'
def restock(self, product_stock):
self.product_stock += product_stock
return f'Current {self.product} stock: {self.product_stock}'
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
Related Documents
Related Questions
Which of the following is a feature of Power Query?
arrow_forward
Which of the following can be used to link two tables together?Select one:a. Primary Keyb. Foreign keyc. Data Dictionaryd. None of the above
arrow_forward
State the purpose for each of the following six built-in spreadsheet functions. (a) PV(i%, n, A, F) (b) FV(i%, n, A, P) (c) RATE(n, A, P, F) (d ) IRR(first_cell:last_cell) (e) PMT(i%, n, P, F) ( f ) NPER(i%, A, P, F)
arrow_forward
Create a SQL table using your name with the following features:
the columns of your table must include, at least the data types (in this order) and one more of your choice
NOTE: You need to specify a 2 column (i.e 2 attribute
1. varchar (n), // where n covers the string length you want to enter
2. Int,
3. decimal, (precision = 8, scale = 3
4. date.
5. ??? your choice here ???
Table constraints:
1. It has a two column primary key
2. a check constraint on 2 columns, on the decimal and the date field
3. Use '2024-02-18' date as the default on the date field
5. write down your relational schema
5. Create the table, insert at least 4 rows, and do a Select * to show them
example don't just copy these, change the constraint names
arrow_forward
I need answer typing clear urjent no chatgpt used pls i will give 5 Upvotes.
arrow_forward
What is the result of inputting lF(5>9,ok,error) into a cell?
#NAME?
ok
error
arrow_forward
I need typing clear urjent no chatgpt used i will give 5 upvotes pls full explain
arrow_forward
typing clear no chatgpt used i will give 5 upvote
arrow_forward
A partial dependency A.) is the result of simultaneous user requests for the same data in a partitioned database environment. B) is a name given to one of the three anomalies that result from unnormalized database tables. c) can exist only in a table with a composite primary key D.) may exist in tables that are normalized at the 2NF
arrow_forward
supposed S= 9 + 9/1.07 + 9/1.07^2 + ...+ 9/1.07^961, Find Value of S. (please break down in step by step)
arrow_forward
not use ai please don't
arrow_forward
myFunc = function (x, y = 2)
{z = 7
}
z+x^2+y
Assuming that this was the first thing entered in a new R session, if the next command entered is z+1, what is the output?
O 8.
● An error, z does not exist.
O 10.
O 7.
arrow_forward
Work out the two possible values of c3x(x+12) = 3x2 + c2x
arrow_forward
I need typing clear urjent no chatgpt used i will give 5 upvotes
arrow_forward
What is the mode for the following data set: 99,100,99, 100,8,9,8,7,8? Show all works.
arrow_forward
If the following pseudocode were an actual program, what would it display?
Declare Integer a = 5
Declare Integer b = 2
Declare Integer c = 5
Declare Integer result
Set result a % (b+c)
Display result
Select one:
a. 21
b. 0
C.
5
d. 2
arrow_forward
For the spreadsheet built-in function PV(i%,n,A,F), the only parameter that can be completely omitted is:a. i%b. nc. Ad. F
arrow_forward
No chatgpt used typing clear
arrow_forward
How many three-letter sequences are possible that use the letters a, p, j, e, z at most once each?
----------------------------------sequences
arrow_forward
SEE MORE QUESTIONS
Recommended textbooks for you

Survey of Accounting (Accounting I)
Accounting
ISBN:9781305961883
Author:Carl Warren
Publisher:Cengage Learning
Related Questions
- Which of the following is a feature of Power Query?arrow_forwardWhich of the following can be used to link two tables together?Select one:a. Primary Keyb. Foreign keyc. Data Dictionaryd. None of the abovearrow_forwardState the purpose for each of the following six built-in spreadsheet functions. (a) PV(i%, n, A, F) (b) FV(i%, n, A, P) (c) RATE(n, A, P, F) (d ) IRR(first_cell:last_cell) (e) PMT(i%, n, P, F) ( f ) NPER(i%, A, P, F)arrow_forward
- Create a SQL table using your name with the following features: the columns of your table must include, at least the data types (in this order) and one more of your choice NOTE: You need to specify a 2 column (i.e 2 attribute 1. varchar (n), // where n covers the string length you want to enter 2. Int, 3. decimal, (precision = 8, scale = 3 4. date. 5. ??? your choice here ??? Table constraints: 1. It has a two column primary key 2. a check constraint on 2 columns, on the decimal and the date field 3. Use '2024-02-18' date as the default on the date field 5. write down your relational schema 5. Create the table, insert at least 4 rows, and do a Select * to show them example don't just copy these, change the constraint namesarrow_forwardI need answer typing clear urjent no chatgpt used pls i will give 5 Upvotes.arrow_forwardWhat is the result of inputting lF(5>9,ok,error) into a cell? #NAME? ok errorarrow_forward
- I need typing clear urjent no chatgpt used i will give 5 upvotes pls full explainarrow_forwardtyping clear no chatgpt used i will give 5 upvotearrow_forwardA partial dependency A.) is the result of simultaneous user requests for the same data in a partitioned database environment. B) is a name given to one of the three anomalies that result from unnormalized database tables. c) can exist only in a table with a composite primary key D.) may exist in tables that are normalized at the 2NFarrow_forward
- supposed S= 9 + 9/1.07 + 9/1.07^2 + ...+ 9/1.07^961, Find Value of S. (please break down in step by step)arrow_forwardnot use ai please don'tarrow_forwardmyFunc = function (x, y = 2) {z = 7 } z+x^2+y Assuming that this was the first thing entered in a new R session, if the next command entered is z+1, what is the output? O 8. ● An error, z does not exist. O 10. O 7.arrow_forward
arrow_back_ios
SEE MORE QUESTIONS
arrow_forward_ios
Recommended textbooks for you
- Survey of Accounting (Accounting I)AccountingISBN:9781305961883Author:Carl WarrenPublisher:Cengage Learning

Survey of Accounting (Accounting I)
Accounting
ISBN:9781305961883
Author:Carl Warren
Publisher:Cengage Learning