-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmy test.py
executable file
·312 lines (265 loc) · 7.76 KB
/
my test.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
# def generate_subsets():
# """
# >>> subsets = generate_subsets()
# >>> for _ in range(3):
# ... print(next(subsets))
# ...
# [[]]
# [[], [1]]
# [[], [1], [2], [1, 2]]
# """
# # x = [[]]
# # yield x
# # y = 0
# # while True:
# # y += 1
# # yield x + [[y] + _ for _ in subsets]
# subsets = [[]]
# n = 1
# while True:
# yield subsets
# subsets = subsets + [s + [n] for s in subsets]
# n += 1
# subsets = generate_subsets()
# for _ in range(4):
# print(next(subsets))
# def sum_paths_gen(t):
# """
# >>> t1 = tree(5)
# >>> next(sum_paths_gen(t1))
# 5
# >>> t2 = tree(1, [tree(2, [tree(3), tree(4)]), tree(9)])
# >>> sorted(sum_paths_gen(t2))
# [6, 7, 10]
# """
# if is_leaf :
# yield label(t)
# for b in branches(t):
# for s in sum_paths_gen(b):
# yield s + label(b)
# def collect_words(t):
# """Return a list of all the words contained in the tree where the value of each node in
# the tree is an individual letter. Words terminate at the leaf of a tree.
# >>> collect_words(greetings)
# ['hi', 'hello', 'hey']
# """
# if is_leaf(t):
# return [label(t)]
# words = []
# for branch in branches(t):
# words += [label(t) + word for word in collect_words(branch)]
# return words
# print('a' + 'b')
# def make_withdraw(balance, password):
# """Return a password-protected withdraw function.
# >>> w = make_withdraw(100, 'hax0r')
# >>> w(25, 'hax0r')
# 75
# >>> error = w(90, 'hax0r')
# >>> error
# 'Insufficient funds'
# >>> error = w(25, 'hwat')
# >>> error
# 'Incorrect password'
# >>> new_bal = w(25, 'hax0r')
# >>> new_bal
# 50
# >>> w(75, 'a')
# 'Incorrect password'
# >>> w(10, 'hax0r')
# 40
# >>> w(20, 'n00b')
# 'Incorrect password'
# >>> w(10, 'hax0r')
# "Frozen account. Attempts: ['hwat', 'a', 'n00b']"
# >>> w(10, 'l33t')
# "Frozen account. Attempts: ['hwat', 'a', 'n00b']"
# >>> type(w(10, 'l33t')) == str
# True
# """
# "*** YOUR CODE HERE ***"
# i = 0
# def withdraw(amount, password_input):
# # nonlocal i
# lst = []
# if len(lst) == 3 :
# return "Frozen account. Attempts: " + str(lst)
# if password_input != password:
# lst += [password_input]
# # i += 1
# # return
# # return "Frozen account. Attempts: " + str(lst)
# return 'Incorrect password' , withdraw
# nonlocal balance
# if password == password_input:
# if balance < amount:
# return 'Insufficient funds'
# balance -= amount
# return balance
# return withdraw
# def repeated(t, k):
# """Return the first value in iterator T that appears K times in a row. Iterate through the items such that
# if the same iterator is passed into repeated twice, it continues in the second call at the point it left off
# in the first.
# >>> s = iter([10, 9, 10, 9, 9, 10, 8, 8, 8, 7])
# >>> repeated(s, 2)
# 9
# >>> s2 = iter([10, 9, 10, 9, 9, 10, 8, 8, 8, 7])
# >>> repeated(s2, 3)
# 8
# >>> s = iter([3, 2, 2, 2, 1, 2, 1, 4, 4, 5, 5, 5])
# >>> repeated(s, 3)
# 2
# >>> repeated(s, 3)
# 5
# >>> s2 = iter([4, 1, 6, 6, 7, 7, 8, 8, 2, 2, 2, 5])
# >>> repeated(s2, 3)
# 2
# """
# assert k > 1
# "*** YOUR CODE HERE ***"
# cnt = 0
# flag = 0
# tmp = next(t)
# for i in t :
# if i == tmp and flag == 0 :
# cnt += 2
# flag = 1
# if cnt == k :
# return tmp
# if i == tmp and flag == 1:
# cnt += 1
# if cnt == k :
# return tmp
# tmp = i
# s = iter([10, 9, 10, 9, 9, 10, 8, 8, 8, 7])
# repeated(s, 2)
# def permutations(seq):
# if len(seq) == 1:
# yield seq
# # if len(seq) == 1:
# # yield seq
# else:
# for element in permutations([x for x in seq if x != seq[0]]):
# for k in range(len(element) + 1):
# yield element[:k] + [seq[0]] + element[k:]
# print(sorted(permutations([1, 2, 3])))
# from tkinter import N
# def hailstone(n):
# """
# >>> for num in hailstone(10):
# ... print(num)
# ...
# 10
# 5
# 16
# 8
# 4
# 2
# 1
# """
# "*** YOUR CODE HERE ***"
# yield n
# while True:
# if n % 2 == 0:
# n //= 2
# # yield n
# yield n
# elif n % 2 == 1 and n != 1:
# n = 3 * n + 1
# yield n
# for num in hailstone(10):
# print(num)
# print(89466489648964896489654)
# class Car(object):
# num_wheels = 4
# gas = 30
# headlights = 2
# size = 'Tiny'
# def __init__(self, make, model):
# self.make = make
# self.model = model
# self.color = 'No color yet. You need to paint me.'
# self.wheels = Car.num_wheels
# self.gas = Car.gas
# def paint(self, color):
# self.color = color
# return self.make + ' ' + self.model + ' is now ' + color
# def drive(self):
# if self.wheels < Car.num_wheels or self.gas <= 0:
# return 'Cannot drive!'
# self.gas -= 10
# return self.make + ' ' + self.model + ' goes vroom!'
# def pop_tire(self):
# if self.wheels > 0:
# self.wheels -= 1
# def fill_gas(self):
# self.gas += 20
# return 'Gas level: ' + str(self.gas)
# deneros_car = Car('Tesla', 'Model S')
# Car.drive()
# disc 07
# disc 07
# disc 07
# class Email:
# """Every email object has 3 instance attributes: the
# message, the sender name, and the recipient name.
# """
# def __init__(self, msg, sender_name, recipient_name):
# pass
# self.msg = msg
# self.sender_name = sender_name
# self.recipient_name = recipient_name
# class Server:
# """Each Server has an instance attribute clients, which
# is a dictionary that associates client names with
# client objects.
# """
# def __init__(self):
# self.clients = {}
# def send(self, email):
# """Take an email and put it in the inbox of the client
# it is addressed to.
# """
# client = self.clients[email.recipient_name]
# clinet.receive(email)
# def register_client(self, client, client_name):
# """Takes a client object and client_name and adds it
# to the clients instance attribute.
# """
# client = self.clients[client_name]
# class Client:
# """Every Client has instance attributes name (which is
# used for addressing emails to the client), server
# (which is used to send emails out to other clients), and
# inbox (a list of all emails the client has received).
# """
# def __init__(self, server, name):
# self.inbox = []
# self.server = server
# self.name = name
# self.server.register_client(self, self.name)
# def compose(self, msg, recipient_name):
# """Send an email with the given message msg to the
# given recipient client.
# """
# email = Email(msg, self.name, recipient_name)
# self.server.send(email)
# def receive(self, email):
# """Take an email and add it to the inbox of this
# client.
# """
# self.inbox.append(email)
class A:
def f(self):
return 2
def g(self, obj, x):
if x == 0:
return A.f(obj)
return obj.f() + self.g(self, x - 1)
class B(A):
def f(self):
return 4
x, y = A(), B()
print(x.f())
print(B.f())