You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 100+ Python challenging programming exercises.txt
+66-31Lines changed: 66 additions & 31 deletions
Original file line number
Diff line number
Diff line change
@@ -72,7 +72,9 @@ Question 3
72
72
Level 1
73
73
74
74
Question:
75
-
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.
75
+
With a given integral number n, write a program to generate a dictionary that contains (i, i*i)
76
+
such that is an integral number between 1 and n (both included). and then the program should print
77
+
the dictionary.
76
78
Suppose the following input is supplied to the program:
77
79
8
78
80
Then, the output should be:
@@ -96,7 +98,8 @@ Question 4
96
98
Level 1
97
99
98
100
Question:
99
-
Write a program which accepts a sequence of comma-separated numbers from console and generate a list and a tuple which contains every number.
101
+
Write a program which accepts a sequence of comma-separated numbers from console and generate
102
+
a list and a tuple which contains every number.
100
103
Suppose the following input is supplied to the program:
101
104
34,67,55,33,12,98
102
105
Then, the output should be:
@@ -161,7 +164,8 @@ The output of the program should be:
161
164
18,22,24
162
165
163
166
Hints:
164
-
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)
167
+
If the output received is in decimal form, it should be rounded off to its nearest value
168
+
(for example, if the output received is 26.0, it should be printed as 26)
165
169
In case of input data being supplied to the question, it should be assumed to be a console input.
166
170
167
171
Solution:
@@ -182,7 +186,8 @@ Question 7
182
186
Level 2
183
187
184
188
Question:
185
-
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.
189
+
Write a program which takes 2 digits, X,Y as input and generates a 2-dimensional array.
190
+
The element value in the i-th row and j-th column of the array should be i*j.
186
191
Note: i=0,1.., X-1; j=0,1,¡Y-1.
187
192
Example
188
193
Suppose the following inputs are given to the program:
@@ -191,7 +196,8 @@ Then, the output of the program should be:
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.
199
+
Note: In case of input data being supplied to the question, it should be assumed to be a console
200
+
input in a comma-separated form.
195
201
196
202
Solution:
197
203
input_str = raw_input()
@@ -212,7 +218,8 @@ Question 8
212
218
Level 2
213
219
214
220
Question:
215
-
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.
221
+
Write a program that accepts a comma separated sequence of words as input and prints the words
222
+
in a comma-separated sequence after sorting them alphabetically.
216
223
Suppose the following input is supplied to the program:
217
224
without,hello,bag,world
218
225
Then, the output should be:
@@ -232,7 +239,8 @@ Question 9
232
239
Level 2
233
240
234
241
Question£º
235
-
Write a program that accepts sequence of lines as input and prints the lines after making all characters in the sentence capitalized.
242
+
Write a program that accepts sequence of lines as input and prints the lines after making all
243
+
characters in the sentence capitalized.
236
244
Suppose the following input is supplied to the program:
237
245
Hello world
238
246
Practice makes perfect
@@ -261,7 +269,8 @@ Question 10
261
269
Level 2
262
270
263
271
Question:
264
-
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.
272
+
Write a program that accepts a sequence of whitespace separated words as input and prints
273
+
the words after removing all duplicate words and sorting them alphanumerically.
265
274
Suppose the following input is supplied to the program:
266
275
hello world and practice makes perfect and hello world again
267
276
Then, the output should be:
@@ -282,7 +291,9 @@ Question 11
282
291
Level 2
283
292
284
293
Question:
285
-
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.
294
+
Write a program which accepts a sequence of comma separated 4 digit binary numbers as its input
295
+
and then check whether they are divisible by 5 or not. The numbers that are divisible by 5 are
296
+
to be printed in a comma separated sequence.
286
297
Example:
287
298
0100,0011,1010,1001
288
299
Then the output should be:
@@ -308,7 +319,8 @@ Question 12
308
319
Level 2
309
320
310
321
Question:
311
-
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.
322
+
Write a program, which will find all such numbers between 1000 and 3000 (both included) such that
323
+
each digit of the number is an even number.
312
324
The numbers obtained should be printed in a comma-separated sequence on a single line.
313
325
314
326
Hints:
@@ -357,7 +369,8 @@ Question 14
357
369
Level 2
358
370
359
371
Question:
360
-
Write a program that accepts a sentence and calculate the number of upper case letters and lower case letters.
372
+
Write a program that accepts a sentence and calculate the number of upper case letters and
373
+
lower case letters.
361
374
Suppose the following input is supplied to the program:
362
375
Hello world!
363
376
Then, the output should be:
@@ -409,7 +422,8 @@ Question 16
409
422
Level 2
410
423
411
424
Question:
412
-
Use a list comprehension to square each odd number in a list. The list is input by a sequence of comma-separated numbers.
425
+
Use a list comprehension to square each odd number in a list. The list is input by a
426
+
sequence of comma-separated numbers.
413
427
Suppose the following input is supplied to the program:
414
428
1,2,3,4,5,6,7,8,9
415
429
Then, the output should be:
@@ -428,7 +442,8 @@ Question 17
428
442
Level 2
429
443
430
444
Question:
431
-
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:
445
+
Write a program that computes the net amount of a bank account based a transaction
446
+
log from console input. The transaction log format is shown as following:
432
447
D 100
433
448
W 200
434
449
@@ -467,7 +482,8 @@ Question 18
467
482
Level 3
468
483
469
484
Question:
470
-
A website requires the users to input username and password to register. Write a program to check the validity of password input by users.
485
+
A website requires the users to input username and password to register. Write a program to check
486
+
the validity of password input by users.
471
487
Following are the criteria for checking the password:
472
488
1. At least 1 letter between [a-z]
473
489
2. At least 1 number between [0-9]
@@ -515,7 +531,9 @@ Question 19
515
531
Level 3
516
532
517
533
Question:
518
-
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:
534
+
You are required to write a program to sort the (name, age, height) tuples by ascending
535
+
order where name is string, age and height are numbers. The tuples are input by console.
536
+
The sort criteria is:
519
537
1: Sort based on name;
520
538
2: Then sort based on age;
521
539
3: Then sort by score.
@@ -551,7 +569,8 @@ Question 20
551
569
Level 3
552
570
553
571
Question:
554
-
Define a class with a generator which can iterate the numbers, which are divisible by 7, between a given range 0 and n.
572
+
Define a class with a generator which can iterate the numbers, which are divisible by 7,
573
+
between a given range 0 and n.
555
574
556
575
Hints:
557
576
Consider use yield
@@ -574,7 +593,8 @@ Question 21
574
593
Level 3
575
594
576
595
Question£º
577
-
A robot moves in a plane starting from the original point (0,0). The robot can move toward UP, DOWN, LEFT and RIGHT with a given steps. The trace of robot movement is shown as the following:
596
+
A robot moves in a plane starting from the original point (0,0). The robot can move toward
597
+
UP, DOWN, LEFT and RIGHT with a given steps. The trace of robot movement is shown as the following:
578
598
UP 5
579
599
DOWN 3
580
600
LEFT 3
@@ -622,7 +642,8 @@ Question 22
622
642
Level 3
623
643
624
644
Question:
625
-
Write a program to compute the frequency of the words from the input. The output should output after sorting the key alphanumerically.
645
+
Write a program to compute the frequency of the words from the input. The output should output
646
+
after sorting the key alphanumerically.
626
647
Suppose the following input is supplied to the program:
627
648
New to Python or choosing between Python 2 and Python 3? Read Python 2 or Python 3.
628
649
Then, the output should be:
@@ -677,8 +698,10 @@ Question 24
677
698
Level 1
678
699
679
700
Question:
680
-
Python has many built-in functions, and if you do not know how to use it, you can read document online or find some books. But Python has a built-in document function for every built-in functions.
681
-
Please write a program to print some Python built-in functions documents, such as abs(), int(), raw_input()
701
+
Python has many built-in functions, and if you do not know how to use it, you can read
702
+
document online or find some books. But Python has a built-in document function for every
703
+
built-in functions. Please write a program to print some Python built-in functions documents,
704
+
such as abs(), int(), raw_input()
682
705
And add document for your own function
683
706
684
707
Hints:
@@ -733,7 +756,8 @@ Question:
733
756
Define a function which can compute the sum of two numbers.
734
757
735
758
Hints:
736
-
Define a function with two numbers as arguments. You can compute the sum in the function and return the value.
759
+
Define a function with two numbers as arguments. You can compute the sum in the function
760
+
and return the value.
737
761
738
762
Solution
739
763
def SumFunction(number1, number2):
@@ -774,7 +798,8 @@ printValue(3)
774
798
2.10
775
799
776
800
Question:
777
-
Define a function that can receive two integral numbers in string form and compute their sum and then print it in console.
801
+
Define a function that can receive two integral numbers in string form and compute their sum
802
+
and then print it in console.
778
803
779
804
Hints:
780
805
@@ -809,7 +834,9 @@ printValue("3","4") #34
809
834
810
835
811
836
Question:
812
-
Define a function that can accept two strings as input and print the string with maximum length in console. If two strings have the same length, then the function should print al l strings line by line.
837
+
Define a function that can accept two strings as input and print the string with maximum
838
+
length in console. If two strings have the same length, then the function should print all
839
+
strings line by line.
813
840
814
841
Hints:
815
842
@@ -836,7 +863,8 @@ printValue("one","three")
836
863
2.10
837
864
838
865
Question:
839
-
Define a function that can accept an integer number as input and print the "It is an even number" if the number is even, otherwise print "It is an odd number".
866
+
Define a function that can accept an integer number as input and print the "It is an even number"
867
+
if the number is even, otherwise print "It is an odd number".
840
868
841
869
Hints:
842
870
@@ -857,7 +885,8 @@ checkValue(7)
857
885
2.10
858
886
859
887
Question:
860
-
Define a function which can print a dictionary where the keys are numbers between 1 and 3 (both included) and the values are square of keys.
888
+
Define a function which can print a dictionary where the keys are numbers between
889
+
1 and 3 (both included) and the values are square of keys.
861
890
862
891
Hints:
863
892
@@ -883,7 +912,8 @@ printDict()
883
912
2.10
884
913
885
914
Question:
886
-
Define a function which can print a dictionary where the keys are numbers between 1 and 20 (both included) and the values are square of keys.
915
+
Define a function which can print a dictionary where the keys are numbers between 1 and 20
916
+
(both included) and the values are square of keys.
887
917
888
918
Hints:
889
919
@@ -906,7 +936,8 @@ printDict()
906
936
2.10
907
937
908
938
Question:
909
-
Define a function which can generate a dictionary where the keys are numbers between 1 and 20 (both included) and the values are square of keys. The function should just print the values only.
939
+
Define a function which can generate a dictionary where the keys are numbers between 1 and 20
940
+
(both included) and the values are square of keys. The function should just print the values only.
910
941
911
942
Hints:
912
943
@@ -930,7 +961,8 @@ printDict()
930
961
2.10
931
962
932
963
Question:
933
-
Define a function which can generate a dictionary where the keys are numbers between 1 and 20 (both included) and the values are square of keys. The function should just print the keys only.
964
+
Define a function which can generate a dictionary where the keys are numbers between 1 and 20
965
+
(both included) and the values are square of keys. The function should just print the keys only.
934
966
935
967
Hints:
936
968
@@ -955,7 +987,8 @@ printDict()
955
987
2.10
956
988
957
989
Question:
958
-
Define a function which can generate and print a list where the values are square of numbers between 1 and 20 (both included).
990
+
Define a function which can generate and print a list where the values are square of numbers
991
+
between 1 and 20 (both included).
959
992
960
993
Hints:
961
994
@@ -977,7 +1010,8 @@ printList()
977
1010
2.10
978
1011
979
1012
Question:
980
-
Define a function which can generate a list where the values are square of numbers between 1 and 20 (both included). Then the function needs to print the first 5 elements in the list.
1013
+
Define a function which can generate a list where the values are square of numbers between
1014
+
1 and 20 (both included). Then the function needs to print the first 5 elements in the list.
981
1015
982
1016
Hints:
983
1017
@@ -1001,7 +1035,8 @@ printList()
1001
1035
2.10
1002
1036
1003
1037
Question:
1004
-
Define a function which can generate a list where the values are square of numbers between 1 and 20 (both included). Then the function needs to print the last 5 elements in the list.
1038
+
Define a function which can generate a list where the values are square of numbers between
1039
+
1 and 20 (both included). Then the function needs to print the last 5 elements in the list.
0 commit comments