-
Notifications
You must be signed in to change notification settings - Fork 0
/
hello.py
310 lines (241 loc) · 5.33 KB
/
hello.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
# -*- coding: utf-8 -*-
import math
from functools import reduce
"""
names = ['Michael', 'Bob', 'Ethan']
for name in names:
print(name)
"""
"""
sum = 0
for x in range(101):
sum = sum + x
print(sum)
"""
"""
sum = 0
n = 99
while n > 0:
sum = sum + n
n = n-2
print(sum)
"""
"""
d = {'Michael': 95, 'Bob': 75, 'Ethan': 95, 'test': (1, [2, 3])}
print('d',d)
print(d['test'])
print('Thmoas' in d)
"""
"""
s = set([1, 2, 3])
print(s)
"""
"""
def my_abs(x):
if not isinstance(x, (int, float)):
raise TypeError('bad operand type')
if x >= 0:
return x
else:
return -x
print(my_abs(-100))
def nop():
# 做占位符
pass
"""
"""
def move(x, y, step, angle=0):
nx = x + step * math.cos(angle)
ny = y - step * math.sin(angle)
return nx, ny
a, b = move(100, 100, 60, math.pi / 6)
print(a, b)
r = move(100, 100, 60, math.pi / 6)
print('r=', r)
"""
"""
def power(x, n):
s = 1
while n > 0:
n = n - 1
s = s * x
return s
print('5*5=', power(5, 2))
"""
def calc(*numbers):
sum = 0
for n in numbers:
sum = sum + n * n
return sum
"""
nums = [1, 2, 3]
print(calc(*nums))
"""
"""
def product(x, *y):
# 计算多个数的乘积
s = 1
if not y:
s = s * x
else:
s = s * x
for k in y:
s = s * k
return s
"""
"""
print('product(2,5,6)', product(2, 5, 6))
print('product(1,5) =', product(1, 5))
"""
"""
def fact(n):
return fact_iter(n, 1)
def fact_iter(num, product):
if num == 1:
return product
return fact_iter(num - 1, num * product)
print(fact(4))
"""
"""
def move(n, a, b, c):
# 汉诺塔问题
if n == 1:
print(a, '--->', c)
else:
move(n-1, a, c, b)
print(a, '--->', c)
move(n-1, b, a, c)
move(3, 'A', 'B', 'C')
"""
"""
# 关键字参数
def person(name, age, **kw):
print('name:', name, 'age:', age, 'other', kw)
extra = {'city': 'Beijing', 'job': 'Engineer'}
person('Ethan', 20, city=extra['city'])
person('Ethan', 20, **extra)
"""
"""
# 命名关键字参数
def person(name, age, *, city, job):
print(name, age, city, job)
person('Jack', 24, city='Beijing', job='Engineer')
"""
# 切片
L = ['Michael', 'Sarah', 'Tracy', 'Bob', 'Jack']
# print(L[-2:-1])
# L2 = list(range(100))
# print(L2[:10:2])
def trim(s):
# 去除字符串首尾空格,递归函数
if len(s) > 0:
if s[0] == '':
return trim(s[1:])
elif s[-1] == '':
return trim(s[:-1])
return s
# print(' 123abc 去除首尾空格', trim(" 123abc "))
"""
d = {'a': 1, 'b': 2, 'c': 3}
print('d.items():', d.items())
for key, v in d.items():
print(key + ':' + str(v))
"""
def findMinAndMax(l):
if len(l) == 0:
return (None, None)
max = min = l[0]
for i in L:
if isinstance(i, int or float):
if i > max:
max = i
elif i < min:
min = i
else:
raise TypeError
return min, max
# 使用列表生成式
# print('[1,45,59,0,89,48151]',findMinAndMax([1,45,59,0,89,48151]))
# print('生成x*x的列表', [x * x for x in range(1,11) if x % 2 == 0] )
# print('两层循环生成全排列',[m + n for m in 'ABC' for n in 'XYZ'])
import os
# print('列出当前目录下的所有文件和目录名',[d for d in os.listdir('.')])
d = {'a': '1', 'b': '2', 'c': '3'}
# print('使用两个变量生成字符串列表',[k +'='+ v for k, v in d.items()])
Lstring = ['Hello', 'World', 'IBM', 'Apple']
# print('将list中的所有字符串变成小写',[s.lower() for s in Lstring])
Llist = ['Hello', 'World', 18, 'Apple', None]
# print('将list中的字符串小写输出,其它类型数据去除',[l.lower() for l in Llist if isinstance(l, str)])
g = (x * x for x in range(10))
# print((x * x for x in g))
# print(next(g))
def printgenerator():
g = (x * x for x in range(10))
for n in g:
print('值', n)
# print(printgenerator())
def fib(max):
# max为数值个数
# 斐波拉契数列
n, a, b = 0, 0, 1
while n < max:
# print(b)
yield b
a, b = b, a+b
n = n + 1
return 'done'
# print('fib(6)',fib(6))
def printfFib():
for n in fib(6):
print(n)
# print('fib(6)打印值',printfFib())
def f(x):
return x * x
r = map(f, [1,2,3,4,5,6])
# print(r)
# print(list(r))
def add(x, y):
return x +y
a = reduce(add, [1, 3, 5, 7, 9])
# print(a)
def count():
fs=[]
for i in range(1, 4):
def f():
return i * i
fs.append(f)
return fs
# f1,f2,f3,= count()
# print(f1())
# print(f3())
def count():
def f(j):
def g():
return j*j
return g
fs =[]
for i in range(1, 4):
fs.append(f(i))
return fs
# f1,f2,f3,= count()
# print(f1())
# print(f2())
# print(f3())
def createCounter():
i = [0]
def counter():
i[0]+=1
return i[0]
return counter
def test_createCounter():
counterA = createCounter()
print(counterA(), counterA(), counterA(), counterA(), counterA()) # 1 2 3 4 5
counterB = createCounter()
if [counterB(), counterB(), counterB(), counterB()] == [1, 2, 3, 4]:
print('测试通过!')
else:
print('测试失败!')
# print(test_createCounter())
class Hello(object):
def hello(self, name='world'):
print('Hello, %s.' % name)