Skip to content
Navigation Menu
{{ message }}
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemp.py
More file actions
462 lines (391 loc) · 8.72 KB
/
Copy pathtemp.py
File metadata and controls
462 lines (391 loc) · 8.72 KB
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
# -*- coding: utf-8 -*-
"""
Spyder Editor
This is a temporary script file.
"""
men=['Li','liu','chen']
men
men.append('zhang')
men.insert(4,'liao')
print(men)
men.pop(4)
print(men)
p=['xia','bin']
men.append(p)
men[5][1]
len(men)
name=input('please input the name that you think is handsome')
content=[name,'is','a','pig']
age=input('please input your age:\n')
age=int(age)
print('Your age is',age)
if age>=18:
print('adult')
elif age>=12:
print('teenager')
else:
print('kid')
m=input('please input the weight of a person:\n')
m=int(m)
h=input('please input the height of the corresponding person:\n')
h=float(h)
BMI=m/pow(h,2)
if BMI<=18.5:
print('weak')
elif BMI<=25:
print('normal')
elif BMI<=28:
print('a litte fat')
elif BMI<=32:
print('fat')
else:
print('very fat')
Digital=input('please input the digital you want to sum:\n')
sum=0
Digital=int(Digital)
for x in range(Digital):
sum=sum+x
print(sum)
sum=0
i=1
while i<=100:
sum=sum+i
i=i+1
print(sum)
L=['Bart','Lisa','Adam']
for x in L:
print('Hello,%s'%x)
L=['zhang','liu','liao']
i=0
length=len(L)
while i<length:
print('Hello,%s'%L[i])
i=i+1
d={'chenyao':100,'liguannan':99,'liuyang':99}
d.get('liguannan')
d.pop('liuyang')
s=set([1,2,3,4])
s.add(5)
print(s)
s.remove(1)
print(s)
ss=set([3,4,5,6])
print(s&ss)
print(s|ss)
n=input('please input an integer:\n')
n=int(n)
print('We transform it into a hexadecimal representation:\n',hex(n))
#求解一元二次方程
import math
def quadratic(a,b,c):
if not isinstance(a,(int,float)):
raise TypeError('not a number')
if not isinstance(b,(int,float)):
raise TypeError('not a number')
if not isinstance(c,(int,float)):
raise TypeError('not a number')
delta=b*b-4*a*c
x1=(-1*b+math.sqrt(delta))/(2*a)
x2=(-1*b-math.sqrt(delta))/(2*a)
if delta>0:
print('The Equation has two results\n')
return x1,x2
elif delta==0:
print('The Equation has two equale results\n')
return x1
else:
print('The Equation has no results in the Real range ')
#定义x^n
def power(x,n):
s=1
for i in range(n):
if i<=n:
s=x*s
return s
#递归函数
def fact(n):
if n==1:
return 1
return n*fact(n-1)
def fact_1(n):
s=1
for i in range(n+1):
if i==0:
continue
s=i*s
return s
for i in range(1,10,2):
print(i)
#用递归解决Tower of Hanoi问题
def move(n,a,b,c):
if n==1:
print(a,'--->',c)
else:
move(n-1,a,c,b)
move(1,a,b,c)
move(n-1,b,a,c)
L=[]
for i in range(1,100,2):
L.append(i)
for i in 'hello':
print(i)
#删除字符串首位的空格
def trim(s):
if len(s)==0:
return s
elif s[0]==' ':
return trim(s[1:])
elif s[-1]==' ':
return trim(s[:-1])
else:
return s
#判断一个对象是否可迭代
from collections import Iterable
isinstance('abc',Iterable)
isinstance([1,2,3],Iterable)
isinstance(123,Iterable)
for i,value in enumerate([(1,1),(2,2),3]):
print(i,value)
#用迭代查找list中的最大值和最小值,并返回一个tuple
def findMinAndMax(L):
from collections import Iterable
if isinstance(L,Iterable)==False:
print('not Iterable')
elif len(L)==0:
print('没有输入数据')
else:
t_min=L[0]
t_max=L[0]
for i in L:
if i<t_min:
t_min=i
if i>t_max:
t_max=i
print('最小值和最大值分别是:\n')
return (t_min,t_max)
#List comprehensions
[x for x in range(1,11)]
[x*x for x in range(1,11)]
[x*x for x in range(1,11) if x%2==0]
[x+y for x in [1,2,3] for y in [4,5]]
L=['Hello','World',18,'Apple']
[s.lower() for s in L if isinstance(s,str)==True]
#打印Fibonacci数列的前N个数
def fibnacci(N):
n,a,b=1,0,1
while n<=N:
print(b)
a,b,n=b,a+b,n+1
def fib(max):
n,a,b=0,0,1
while n<max:
yield b
n,a,b=n+1,b,a+b
return 'done'
#杨辉三角
def triangles(max):
L=[[1]]
if max==0:
print('请输入正数')
elif max==1:
print(L[0])
else:
for n in range(max-1):
a=L[n]
ll=[1]+[a[m]+a[m+1] for m in range(len(a)-1)]+[1]
L.append(ll)
for j in L:
print(j)
#用generator生成杨辉三角
def triangles(max):
L=[[1]]
if max==0:
print('请输入正数')
elif max==1:
yield L[0]
else:
for n in range(max-1):
m=L[n][:len(L[n])-1]
n=L[n][1:]
ll=[1]
for i in range(len(m)):
ll.append(m[i]+n[i])
ll.append(1)
L.append(ll)
for j in L:
yield j
n = 0
results = []
for t in triangles(100):
print(t)
results.append(t)
n = n + 1
if n == 10:
break
def add(x,y,f):
return f(x)+f(y)
list1 = [1,6,2,5,3]
list2 = [34,67,13,678,67]
[list1[i]+list2[i] for i in range(len(list1))]
x=map(abs,[-1,-2,-3])
from functools import reduce
def add(x,y):
return x+y
reduce(add,range(101))
#规范输入的姓名
def normalize(name):
return name[0].upper()+name[1:].lower()
L1 = ['adam', 'LISA', 'barT']
L2 = list(map(normalize, L1))
#利用reduce编写一个求积函数
def fn(x,y):
return x*y
def prod(L):
from functools import reduce
return reduce(fn,L)
#将一个字符串转化成浮点数
DIGITS = {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9}
def str2float(s):
def str2num(l):
return DIGITS[l]
for i in range(len(s)):
if s[i]=='.':
po=i
s1,s2=s[:po],s[po+1:]
ss2=[]
print(s2)
for t in range(len(s2)-1,-1,-1):
ss2.append(s2[t])
ss2.append('0')
from functools import reduce
S1=reduce(lambda x, y: x * 10 + y,map(str2num,s1))
S2=reduce(lambda x, y: x * 0.1 + y,map(str2num,ss2))
return S1+S2
#删选素数
def _odd_iter():
n=1
while True:
n=n+2
yield n #用生成器产生奇数序列
def _not_divisible(n):
return lambda x:x%n>0
def primes():
yield 2
it=_odd_iter()
while True:
n=next(it)
yield n
it=filter(_not_divisible(n),it)
for n in primes():
if n<100:
print(n)
else:
break
#用filter删选出回数
def is_palindrome(n):
n=str(n)
nn=[]
for t in n:
nn.append(t)
nb=[]
for i in range(len(n)-1,-1,-1):
nb.append(n[i])
return nn==nb
output=filter(is_palindrome,range(1,1000))
print('1~1000:',list(output))
#分别按照名字和成绩来排序
L = [('Bob', 75), ('Adam', 92), ('Bart', 66), ('Lisa', 88)]
L1=sorted(L,key=lambda t:t[0])
L2=sorted(L,key=lambda t:t[1])
L3=sorted(L,key=lambda t:t[0],reverse=True)
#返回函数
def cal_sum(*args):
ax=0
for i in args:
ax=ax+i
return ax
def lazy_sum(*args):
def sum():
ax=0
for i in args:
ax=ax+i
return ax
return sum
def build(x,y):
return lambda:x*x+y*y
L=list(filter(lambda x:x%2==1,range(1,20)))
#one test
a=int(input('请输入去年的成绩:'))
b=int(input('请输入今年的成绩:'))
r=(b/a-1)*100
t=b/a-1
print('进步了:%.2f' % r,'%')
print('growth rate:%.2f %%' % t)
for i in range(-1,-1*len(L)-1,-1):
print(L[i])
sum([x for x in range(1,101) if x%2==1])
def cal(*nums):
s=1
for i in nums:
s=s*i
return s
def fac(num):
s=1
for i in range(1,num+1):
s=s*i
return s
def fact(num):
if num==1:
return 1
else:
return num*fact(num-1)
def trim(s):
m,n=0,0
for i in range(len(s)):
if s[i]!=' ':
m=i
break
for j in range(len(s)-1,-1,-1):
if s[j]!=' ':
n=j
break
return s[m:n+1]
for x, y in L:
print(x,y)
def triangles(n):
s=[[1]]
if n==1:
yield s
else:
for i in range(1,n):
ss=s[i-1]
ll=[1]+[ss[j]+ss[j+1] for j in range(len(ss)-1)]+[1]
s.append(ll)
for m in s:
yield m
n=1
for z in triangles(10):
if n==5:
print(z)
break
else:
n=n+1
#functional programming
import math
def add_sin(f,*x):
s=0
for i in x:
s=s+f(i)
return s
sum(range(1,101))
print('chenyao'*5)
print('chenyao '*5)
def print_max(a,b):
'''打印两个数中的最大值。
这两个数都应该是整数'''
if a>=b:
print('The maximum is :',a)
else:
print('The maximun is :',b)
print(print_max._doc_)
import spyder-py3\xiaojianren
You can’t perform that action at this time.
