-
Notifications
You must be signed in to change notification settings - Fork 0
/
hw06.py
119 lines (105 loc) · 3.1 KB
/
hw06.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
passphrase = '*** PASSPHRASE HERE ***'
def survey(p):
"""
You do not need to understand this code.
>>> survey(passphrase)
'3d2eea56786a3d9e503a4c07dd667867ef3d92bfccd68b2aa0900ead'
"""
import hashlib
return hashlib.sha224(p.encode('utf-8')).hexdigest()
class Fib():
"""A Fibonacci number.
>>> start = Fib()
>>> start
0
>>> start.next()
1
>>> start.next().next()
1
>>> start.next().next().next()
2
>>> start.next().next().next().next()
3
>>> start.next().next().next().next().next()
5
>>> start.next().next().next().next().next().next()
8
>>> start.next().next().next().next().next().next() # Ensure start isn't changed
8
"""
def __init__(self, value=0):
self.value = value
def next(self):
"*** YOUR CODE HERE ***"
if self.value == 0:
result = Fib(1)
else:
result = Fib(self.value + self.previous)
result.previous = self.value
return result
def __repr__(self):
return str(self.value)
class VendingMachine:
"""A vending machine that vends some product for some price.
>>> v = VendingMachine('candy', 10)
>>> v.vend()
'Machine is out of stock.'
>>> v.deposit(15)
'Machine is out of stock. Here is your $15.'
>>> v.restock(2)
'Current candy stock: 2'
>>> v.vend()
'You must deposit $10 more.'
>>> v.deposit(7)
'Current balance: $7'
>>> v.vend()
'You must deposit $3 more.'
>>> v.deposit(5)
'Current balance: $12'
>>> v.vend()
'Here is your candy and $2 change.'
>>> v.deposit(10)
'Current balance: $10'
>>> v.vend()
'Here is your candy.'
>>> v.deposit(15)
'Machine is out of stock. Here is your $15.'
>>> w = VendingMachine('soda', 2)
>>> w.restock(3)
'Current soda stock: 3'
>>> w.restock(3)
'Current soda stock: 6'
>>> w.deposit(2)
'Current balance: $2'
>>> w.vend()
'Here is your soda.'
"""
"*** YOUR CODE HERE ***"
def __init__(self, product, price):
self.product = product
self.price = price
self.numbers = 0
self.balance = 0
def vend(self):
if self.numbers == 0:
return 'Machine is out of stock.'
if self.balance < self.price:
return 'You must deposit ${0} more.'.format(self.price - self.balance)
elif self.balance > self.price:
left = self.balance - self.price
self.balance = 0
self.numbers -= 1
return 'Here is your {0} and ${1} change.'.format(self.product, left)
else:
self.balance = 0
self.numbers -= 1
return 'Here is your {0}.'.format(self.product)
def restock(self, n):
self.numbers += n
return 'Current {0} stock: {1}'.format(self.product, self.numbers)
def deposit(self, money):
if self.numbers == 0:
return 'Machine is out of stock. Here is your ${0}.'.format(money)
else:
self.balance += money
return 'Current balance: ${0}'.format(self.balance)