-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path100+ Python .txt
More file actions
3782 lines (2848 loc) · 92.3 KB
/
100+ Python .txt
File metadata and controls
3782 lines (2848 loc) · 92.3 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
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
100+ Python challenging programming exercises
100+Python挑战性编程练习
1. Level description
1.级别描述
Level 1 Beginner means someone who has just gone through an introductory Python course. He can solve some problems with 1 or 2 Python classes or functions. Normally, the answers could directly be found in the textbooks.
第一级初学者是指刚刚完成Python入门课程的人。他可以用一两个Python类或函数来解决一些问题。通常,答案可以直接在教科书中找到。
Level 2 Intermediate means someone who has just learned Python, but already has a relatively strong programming background from before. He should be able to solve problems which may involve 3 or 3 Python classes or functions. The answers cannot be directly be found in the textbooks.
2级中级是指刚刚学习Python,但之前已经有了相对较强的编程背景的人。他应该能够解决可能涉及3个或3个Python类或函数的问题。答案不能直接在教科书中找到。
Level 3 Advanced. He should use Python to solve more complex problem using more rich libraries functions and data structures and algorithms. He is supposed to solve the problem using several Python standard packages and advanced techniques.
3级高级。他应该使用Python来解决更复杂的问题,使用更丰富的库函数、数据结构和算法。他应该使用几个Python标准包和高级技术来解决这个问题。
2. Problem template
2.问题模板
#----------------------------------------#
#----------------------------------------#
Question
问题
Hints
提示
Solution
解决方案
3. Questions
3.问题
#----------------------------------------#
#----------------------------------------#
Question 1
问题1
Level 1
级别1
Question:
问题:
Write a program which will find all such numbers which are divisible by 7 but are not a multiple of 5,
编写一个程序,找出所有能被7整除但不是5的倍数的数字,
between 2000 and 3200 (both included).
2000至3200(均包括在内)。
The numbers obtained should be printed in a comma-separated sequence on a single line.
获得的数字应以逗号分隔的顺序打印在一行上。
Hints:
提示:
Consider use range(#begin, #end) method
考虑使用range(#begin,#end)方法
#本人答案
for i in range(2000, 3201):
if i % 7 == 0 and i % 5 != 0:
print(i, end=',')
#参考答案
l=[]
for i in range(2000, 3201):
if (i%7==0) and (i%5!=0):
l.append(str(i))
print ','.join(l)
#----------------------------------------#
#----------------------------------------#
Question 2
问题2
Level 1
级别1
Question:
问题:
Write a program which can compute the factorial of a given numbers.
编写一个程序,可以计算给定数字的阶乘。
The results should be printed in a comma-separated sequence on a single line.
结果应以逗号分隔的顺序打印在一行上。
Suppose the following input is supplied to the program:
假设向程序提供了以下输入:
8
Then, the output should be:
那么,输出应该是:
40320
Hints:
提示:
In case of input data being supplied to the question, it should be assumed to be a console input.
如果输入数据被提供给问题,则应假设它是控制台输入。
# 本人答案
num=1
n=int(input("请输入一个数字:"))
for i in range(1,n+1):
num=num*i
print(num)
# 参考答案
def fact(x):
if x == 0:
return 1
return x * fact(x - 1)
x=int(raw_input())
print fact(x)
#----------------------------------------#
#----------------------------------------#
Question 3
问题3
Level 1
级别1
Question:
问题:
With a given integral number n, write a program to generate a dictionary that contains (i, i*i) such that is an integral number between 1 and n (both included). and then the program should print the dictionary.
对于给定的整数n,编写一个程序来生成一个包含(i,i*i)的字典。该字典是1和n之间的整数(两者都包括在内)然后程序应该打印字典。
Suppose the following input is supplied to the program:
假设向程序提供了以下输入:
8
Then, the output should be:
那么,输出应该是:
{1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64}
Hints:
提示:
In case of input data being supplied to the question, it should be assumed to be a console input.
如果输入数据被提供给问题,则应假设它是控制台输入。
Consider use dict()
考虑使用dict()
#本人答案
n=int(input("请输入一个数字:"))
a={}
for i in range(1,n+1):
a[i]=i*i
print(a)
Solution:
n=int(raw_input())
d=dict()
for i in range(1,n+1):
d[i]=i*i
print d
#----------------------------------------#
#----------------------------------------#
Question 4
问题4
Level 1
级别1
Question:
问题:
Write a program which accepts a sequence of comma-separated numbers from console and generate a list and a tuple which contains every number.
编写一个程序,从控制台接受逗号分隔的数字序列,并生成一个包含每个数字的列表和元组。
Suppose the following input is supplied to the program:
假设向程序提供了以下输入:
34,67,55,33,12,98
Then, the output should be:
那么,输出应该是:
['34', '67', '55', '33', '12', '98']
('34', '67', '55', '33', '12', '98')
Hints:
提示:
tuple() method can convert list to tuple
tuple()方法可以将列表转换为元组
#本人答案
a=input()
b=tuple(a.split (','))
c=a.split(',')
print(b,c)
Solution:
values=raw_input()
l=values.split(",")
t=tuple(l)
print l
print t
#----------------------------------------#
#----------------------------------------#
Question 5
问题5
Level 1
级别1
Question:
问题:
Define a class which has at least two methods:
定义一个至少有两个方法的类:
getString: to get a string from console input
getString:从控制台输入中获取字符串
printString: to print the string in upper case.
printString:打印大写字符串。
Also please include simple test function to test the class methods.
另外,请包含简单的测试函数来测试类方法。
Hints:
提示:
Use __init__ method to construct some parameters
使用__init__方法构造一些参数
#本人答案
class stringa:
def __init__(self):
self.str1= ""
def getstring(self):
self.str1=input("请输入字符串:")
def printstring(self):
print(self.str1.upper())
s=stringa()
s.getstring()
s.printstring()
Solution:
class InputOutString(object):
def __init__(self):
self.s = ""
def getString(self):
self.s = raw_input()
def printString(self):
print self.s.upper()
strObj = InputOutString()
strObj.getString()
strObj.printString()
#----------------------------------------#
#----------------------------------------#
Question 6
问题6
Level 2
2级
Question:
问题:
Write a program that calculates and prints the value according to the given formula:
编写一个程序,根据给定的公式计算并打印值:
Q = Square root of [(2 * C * D)/H]
Q=[(2*C*D)/H]**0.5
Following are the fixed values of C and H:
以下是C和H的固定值,
C is 50. H is 30.
C是50。H是30。
D is the variable whose values should be input to your program in a comma-separated sequence.
D是变量,其值应以逗号分隔的顺序输入到程序中。
Example
例子
Let us assume the following comma separated input sequence is given to the program:
让我们假设程序有以下逗号分隔的输入序列:
100,150,180
The output of the program should be:
程序的输出应该是:
18,22,24
Hints:
提示:
If the output received is in decimal form, it should be rounded off to its nearest value (for example, if the output received is 26.0, it should be printed as 26)
如果接收到的输出是十进制形式的,则应四舍五入到最接近的值(例如,如果收到的输出是26.0,则应打印为26)
In case of input data being supplied to the question, it should be assumed to be a console input.
如果输入数据被提供给问题,则应假设它是控制台输入。
#本人答案
def task(d):
c=50
h=30
q=int(((2*c*d)/h)**0.5)
return q
a=list((input("输入数字").split(",")))
for i in a:
print(task(int(i)),end=",")
Solution:
#!/usr/bin/env python
import math
c=50
h=30
value = []
items=[x for x in raw_input().split(',')]
for d in items:
value.append(str(int(round(math.sqrt(2*c*float(d)/h)))))
print ','.join(value)
#----------------------------------------#
#----------------------------------------#
Question 7
问题7
Level 2
2级
Question:
问题:
Write a program which takes 2 digits, X,Y as input and generates a 2-dimensional array. The element value in the i-th row and j-th column of the array should be i*j.
编写一个程序,以2位数字X、Y为输入,生成一个二维数组。数组第i行和第j列中的元素值应为i*j。
Example
例子
Suppose the following inputs are given to the program:
假设程序有以下输入:
3,5
Then, the output of the program should be:
那么,程序的输出应该是:
[[0, 0, 0, 0, 0], [0, 1, 2, 3, 4], [0, 2, 4, 6, 8]]
Hints:
提示:
Note: In case of input data being supplied to the question, it should be assumed to be a console input in a comma-separated form.
注意:如果向问题提供输入数据,则应假设它是逗号分隔形式的控制台输入。
#本人答案
a,b=map(int,input("a,b=").split(','))
c=[]
for i in range(a):
d=[]
for j in range(b):
d.append(i*j)
c.append(d)
print(c)
Solution:
input_str = raw_input()
dimensions=[int(x) for x in input_str.split(',')]
rowNum=dimensions[0]
colNum=dimensions[1]
multilist = [[0 for col in range(colNum)] for row in range(rowNum)]
for row in range(rowNum):
for col in range(colNum):
multilist[row][col]= row*col
print multilist
#----------------------------------------#
#----------------------------------------#
Question 8
问题8
Level 2
2级
Question:
问题:
Write a program that accepts a comma separated sequence of words as input and prints the words in a comma-separated sequence after sorting them alphabetically.
编写一个程序,接受逗号分隔的单词序列作为输入,并在按字母顺序排序后以逗号分隔的顺序打印单词。
Suppose the following input is supplied to the program:
假设向程序提供了以下输入:
without,hello,bag,world
Then, the output should be:
那么,输出应该是:
bag,hello,without,world
#本人答案
str_list=input("输入单词序列(Input word sequence):").split(',')
str_list.sort()
print(','.join(str_list))
Solution:
items=[x for x in raw_input().split(',')]
items.sort()
print ','.join(items)
#----------------------------------------#
#----------------------------------------#
Question 9
问题9
Level 2
2级
Question£º
问题:
Write a program that accepts sequence of lines as input and prints the lines after making all characters in the sentence capitalized.
编写一个程序,接受行序列作为输入,并在将句子中的所有字符大写后打印行。
Suppose the following input is supplied to the program:
假设向程序提供了以下输入:
Hello world
Practice makes perfect
Then, the output should be:
那么,输出应该是:
HELLO WORLD
PRACTICE MAKES PERFECT
#本人答案
a=[]
while 1==1:
s=input()
if s:
a.append(s.upper())
else:
break
for k in a:
print(k)
Solution:
lines = []
while True:
s = raw_input()
if s:
lines.append(s.upper())
else:
break;
for sentence in lines:
print sentence
#----------------------------------------#
#----------------------------------------#
Question 10
问题10
Level 2
2级
Question:
问题:
Write a program that accepts a sequence of whitespace separated words as input and prints the words after removing all duplicate words and sorting them alphanumerically.
编写一个程序,接受一系列空格分隔的单词作为输入,并在删除所有重复单词并按字典序排序后打印单词。
Suppose the following input is supplied to the program:
假设向程序提供了以下输入:
hello world and practice makes perfect and hello world again
你好世界,熟能生巧,再次你好世界
Then, the output should be:
那么,输出应该是:
again and hello makes perfect practice world
再次,hello创造了完美的实践世界
Hints:
提示:
In case of input data being supplied to the question, it should be assumed to be a console input.
如果输入数据被提供给问题,则应假设它是控制台输入。
We use set container to remove duplicated data automatically and then use sorted() to sort the data.
我们使用set容器自动删除重复的数据,然后使用sorted()对数据进行排序。
#本人答案
a=list(set(input().split(" ")))
a.sort()
print(' '.join(a))
Solution:
s = raw_input()
words = [word for word in s.split(" ")]
print " ".join(sorted(list(set(words))))
#----------------------------------------#
#----------------------------------------#
Question 11
问题11
Level 2
2级
Write a program which accepts a sequence of comma separated 4 digit binary numbers as its input and then check whether they are divisible by 5 or not. The numbers that are divisible by 5 are to be printed in a comma separated sequence.
编写一个程序,接受逗号分隔的4位二进制数序列作为输入,然后检查它们是否能被5整除。可被5整除的数字将以逗号分隔的顺序打印。
Example:
例子:
0100,0011,1010,1001
Then the output should be:
那么输出应该是:
1010
Notes: Assume the data is input by console.
注:假设数据是通过控制台输入的。
#本人答案
a=input().split(",")
c=[]
for i in a:
b=int(i,2)
if b%5==0:
c.append(i)
print(','.join(c))
Solution:
value = []
items=[x for x in raw_input().split(',')]
for p in items:
intp = int(p, 2)
if not intp%5:
value.append(p)
print ','.join(value)
#----------------------------------------#
#----------------------------------------#
Question 12
问题12
Leve2
2级
Question:
问题:
Write a program, which will find all such numbers between 1000 and 3000 (both included) such that each digit of the number is an even number.
编写一个程序,它将找到1000到3000之间的所有的每一位都是偶数的数字(包括1000和3000)。
The numbers obtained should be printed in a comma-separated sequence on a single line.
获得的数字应以逗号分隔的顺序打印在一行上。
Hints:
提示:
In case of input data being supplied to the question, it should be assumed to be a console input.
如果输入数据被提供给问题,则应假设它是控制台输入。
说实话我认为这应该是一道一级的题目
To be honest, I think this should be a first level question
#本人答案
num=[]
for i in range(1000,3001):
a=i
while a>0:
b=a%10
a//=10
if b%2!=0:
break
else:
num.append(str(i))
print(','.join(num))
Solution:
values = []
for i in range(1000, 3001):
s = str(i)
if (int(s[0])%2==0) and (int(s[1])%2==0) and (int(s[2])%2==0) and (int(s[3])%2==0):
values.append(s)
print ",".join(values)
#----------------------------------------#
#----------------------------------------#
Question 13
问题13
Level 2
2级
Question:
问题:
write a program that accepts a sentence and calculate the number of letters and digits.
编写一个程序,接受一个句子并计算字母和数字的数量。
Suppose the following input is supplied to the program:
假设向程序提供了以下输入:
hello world! 123
Then, the output should be:
那么,输出应该是:
LETTERS 10
DIGITS 3
#本人答案
str1=input()
a={'LETTERS':0 ,'DIGITS':0}
for i in str1:
if i.isalpha():
a['LETTERS']+=1
elif i.isdigit():
a['DIGITS']+=1
print('LETTERS',a['LETTERS'])
print('DIGITS',a['DIGITS'])
Solution:
s = raw_input()
d={"DIGITS":0, "LETTERS":0}
for c in s:
if c.isdigit():
d["DIGITS"]+=1
elif c.isalpha():
d["LETTERS"]+=1
else:
pass
print "LETTERS", d["LETTERS"]
print "DIGITS", d["DIGITS"]
#----------------------------------------#
#----------------------------------------#
Question 14
问题14
Level 2
2级
Question:
问题:
Write a program that accepts a sentence and calculate the number of upper case letters and lower case letters.
编写一个程序,接受一个句子并计算大写字母和小写字母的数量。
Suppose the following input is supplied to the program:
假设向程序提供了以下输入:
Hello world!
Then, the output should be:
那么,输出应该是:
UPPER CASE 1
LOWER CASE 9
#本人答案
str1=input("")
a={'UPPER CASE':0 ,'LOWER CASE':0}
for i in str1:
if i.isupper():
a['UPPER CASE']+=1
elif i.islower():
a['LOWER CASE']+=1
print('UPPER CASE',a['UPPER CASE'])
print('LOWER CASE',a['LOWER CASE'])
Solution:
s = raw_input()
d={"UPPER CASE":0, "LOWER CASE":0}
for c in s:
if c.isupper():
d["UPPER CASE"]+=1
elif c.islower():
d["LOWER CASE"]+=1
else:
pass
print "UPPER CASE", d["UPPER CASE"]
print "LOWER CASE", d["LOWER CASE"]
#----------------------------------------#
#----------------------------------------#
Question 15
问题15
Level 2
2级
Question:
问题:
Write a program that computes the value of a+aa+aaa+aaaa with a given digit as the value of a.
编写一个程序,用给定的数字作为a的值来计算a+aa+aaa+aaaa的值。
Suppose the following input is supplied to the program:
假设向程序提供了以下输入:
9
Then, the output should be:
那么,输出应该是:
11106
#本人答案
num=int(input())
res=num+num*11+num*111+num*1111
print(res)
Solution:
a = raw_input()
n1 = int( "%s" % a )
n2 = int( "%s%s" % (a,a) )
n3 = int( "%s%s%s" % (a,a,a) )
n4 = int( "%s%s%s%s" % (a,a,a,a) )
print n1+n2+n3+n4
#----------------------------------------#
#----------------------------------------#
Question 16
问题16
Level 2
2级
Question:
问题:
Use a list comprehension to square each odd number in a list. The list is input by a sequence of comma-separated numbers.
使用列表推导式法将列表中的每个奇数平方。列表由逗号分隔的数字序列输入。
Suppose the following input is supplied to the program:
假设向程序提供了以下输入:
1,2,3,4,5,6,7,8,9
Then, the output should be:
那么,输出应该是:
1,3,5,7,9
我的老师告诉我使用列表推导式或许可以使代码变得更加简洁,但是一个好的代码追求的应该让人可以读得懂,而不是最少的行数。
My teacher told me that using list dcomprehension may make the code more concise, but good code should strive for readability, not the minimum number of lines.
#本人的答案(使用列表推导式)
a = [i for i in input().split(',') if int(i) % 2 != 0]
print(','.join(a))
#本人的答案(不使用列表推导式)
a=input().split(',')
b=[]
for i in a:
if int(i)%2!=0:
b.append(i)
print(','.join(b))
Solution:
values = raw_input()
numbers = [x for x in values.split(",") if int(x)%2!=0]
print ",".join(numbers)
#----------------------------------------#
#----------------------------------------#
Question 17
问题17
Level 2
2级
Question:
问题:
Write a program that computes the net amount of a bank account based a transaction log from console input. The transaction log format is shown as following:
编写一个程序,根据控制台输入的交易日志计算银行账户的净额。交易日志格式如下:
D 100
W 200
D means deposit while W means withdrawal.
D表示存款,W表示取款。
Suppose the following input is supplied to the program:
假设向程序提供了以下输入:
D 300
D 300
W 200
D 100
Then, the output should be:
那么,输出应该是:
500
#本人答案
money=0
while True:
s=input()
if not s:
break
a,b=s.split()
if a=='D':
money+=int(b)
elif a=='W':
money-=int(b)
else:
break
print(money)
Solution:
netAmount = 0
while True:
s = raw_input()
if not s:
break
values = s.split(" ")
operation = values[0]
amount = int(values[1])
if operation=="D":
netAmount+=amount
elif operation=="W":
netAmount-=amount
else:
pass
print netAmount
#----------------------------------------#
#----------------------------------------#
Question 18
问题18
Level 3
级别3
A website requires the users to input username and password to register. Write a program to check the validity of password input by users.
网站要求用户输入用户名和密码进行注册。编写一个程序来检查用户输入的密码的有效性。
Following are the criteria for checking the password:
以下是检查密码的标准:
1. At least 1 letter between [a-z]
1.[a-z]之间至少有一个小写字母
2. At least 1 number between [0-9]
2.[0-9]之间至少有一个数字
1. At least 1 letter between [A-Z]
1.[A-Z]之间至少有一个大写字母
3. At least 1 character from [$#@]
3.[$#@]中至少有1个字符
4. Minimum length of transaction password: 6
4.交易密码最小长度:6
5. Maximum length of transaction password: 12
5.交易密码最大长度:12
Your program should accept a sequence of comma separated passwords and will check them according to the above criteria. Passwords that match the criteria are to be printed, each separated by a comma.
您的程序应接受逗号分隔的密码序列,并将根据上述标准进行检查。将打印符合标准的密码,每个密码用逗号分隔。
Example
例子
If the following passwords are given as input to the program:
如果程序输入了以下密码:
ABd1234@1,a F1#,2w3E*,2We3345
Then, the output of the program should be:
那么,程序的输出应该是:
ABd1234@1
#本人答案
str1=input().split(',')
str2=[]
for i in str1:
if len(i)<6 or len(i)>12:
continue
if not i.isupper:
continue
if not i.islower:
continue
if not i.isdigit:
continue
if not any(c in '$#@' for c in i):
continue
str2.append(i)
print(','.join(str2))
Solutions:
import re
value = []
items=[x for x in raw_input().split(',')]
for p in items:
if len(p)<6 or len(p)>12:
continue
else:
pass
if not re.search("[a-z]",p):
continue
elif not re.search("[0-9]",p):
continue
elif not re.search("[A-Z]",p):
continue
elif not re.search("[$#@]",p):
continue
elif re.search("\s",p):
continue
else:
pass
value.append(p)
print ",".join(value)
#----------------------------------------#
#----------------------------------------#
Question 19
问题19
Level 3
级别3
You are required to write a program to sort the (name, age, height) tuples by ascending order where name is string, age and height are numbers. The tuples are input by console. The sort criteria is:
您需要编写一个程序,按升序对(name、age、height)元组进行排序,其中name是字符串,age和height是数字。元组由控制台输入。排序标准为:
1: Sort based on name;
1:按名称排序;
2: Then sort based on age;
2:然后按年龄排序;
3: Then sort by score.
3:然后按分数排序。
The priority is that name > age > score.
优先顺序是姓名>年龄>分数。
If the following tuples are given as input to the program:
如果将以下元组作为程序的输入:
Tom,19,80
John,20,90
Jony,17,91
Jony,17,93
Json,21,85
Then, the output of the program should be:
那么,程序的输出应该是:
[('John', '20', '90'), ('Jony', '17', '91'), ('Jony', '17', '93'), ('Json', '21', '85'), ('Tom', '19', '80')]
Hints:
提示:
We use itemgetter to enable multiple sort keys.
我们使用itemgetter来启用多个排序键。
#本人答案
list1=[]
while True:
s=input()
if not s:
break
tup=tuple(s.split(','))
list1.append(tup)
list1.sort(key=lambda x: (x[0], int(x[1]),int(x[2])))
print(list1)
Solutions:
from operator import itemgetter, attrgetter
l = []
while True:
s = raw_input()
if not s:
break
l.append(tuple(s.split(",")))
print sorted(l, key=itemgetter(0,1,2))
#----------------------------------------#
#----------------------------------------#
Question 20
问题20
Level 3
级别3
Question:
问题:
Define a class with a generator which can iterate the numbers, which are divisible by 7, between a given range 0 and n.