diff --git a/100+ Python challenging programming exercises.txt b/100+ Python challenging programming exercises.txt index 66c00215..51cdda64 100644 --- a/100+ Python challenging programming exercises.txt +++ b/100+ Python challenging programming exercises.txt @@ -2,9 +2,17 @@ 1. Level description Level Description -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. -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. -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. +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. + +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. + +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. 2. Problem template @@ -41,7 +49,7 @@ Question 2 Level 1 Question: -Write a program which can compute the factorial of a given numbers. +Write a program which can compute the factorial of a given number. The results should be printed in a comma-separated sequence on a single line. Suppose the following input is supplied to the program: 8 @@ -49,16 +57,18 @@ 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. +In case of input data being supplied to the question, it should be assumed +to be a console input with a defined variable. Solution: +x=8 def fact(x): if x == 0: return 1 return x * fact(x - 1) -x=int(raw_input()) -print fact(x) +print(fact(x)) + #----------------------------------------# #----------------------------------------# @@ -66,9 +76,10 @@ Question 3 Level 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. +With a given integer number n, write a program to generate a dictionary that contains the key, value pairs (i, i*i) +where i is between 1 and n (both included) and then the program should print the complete dictionary. Suppose the following input is supplied to the program: -8 +n = 8 Then, the output should be: {1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64} @@ -77,12 +88,13 @@ In case of input data being supplied to the question, it should be assumed to be Consider use dict() Solution: -n=int(raw_input()) -d=dict() -for i in range(1,n+1): +n = 8 +d = dict() +for i in range(1, n+1): d[i]=i*i -print d +print(d) + #----------------------------------------# #----------------------------------------# @@ -90,9 +102,11 @@ Question 4 Level 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 +Write a program which accepts a sequence of comma-separated numbers in a string format from the console and then +generate a list and a tuple which contains every number in the string. Suppose the following input is supplied to the program: + +numbers = 34,67,55,33,12,98 + Then, the output should be: ['34', '67', '55', '33', '12', '98'] ('34', '67', '55', '33', '12', '98') @@ -102,11 +116,13 @@ In case of input data being supplied to the question, it should be assumed to be tuple() method can convert list to tuple Solution: -values=raw_input() -l=values.split(",") -t=tuple(l) -print l -print t +numbers = 34,67,55,33,12,98 +l = numbers.split(",") +t = tuple(l) +print(l) +print(t) + + #----------------------------------------# #----------------------------------------# @@ -123,19 +139,28 @@ Hints: Use __init__ method to construct some parameters Solution: -class InputOutString(object): - def __init__(self): - self.s = "" +class UpperCase: + + def __init__(self, text): + self._text = text + + def text(self): + return self._text + + def upper(self): + print(self._text.upper()) - def getString(self): - self.s = raw_input() + def output(self): + print(self._text) - def printString(self): - print self.s.upper() +To access these methods from the REPL : +>>> from foo import * +>>> u = UpperCase("this is your text") +>>> u.output() +>>> this is your text +>>> u.upper() +>>> THIS IS YOUR TEXT -strObj = InputOutString() -strObj.getString() -strObj.printString() #----------------------------------------# #----------------------------------------# @@ -177,7 +202,7 @@ Level 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. -Note: i=0,1.., X-1; j=0,1,��Y-1. +Note: i=0,1.., X-1; j=0,1,¡­Y-1. Example Suppose the following inputs are given to the program: 3,5 @@ -225,7 +250,7 @@ print ','.join(items) Question 9 Level 2 -Question�� +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 @@ -425,7 +450,7 @@ 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. Suppose the following input is supplied to the program: D 300 @@ -568,13 +593,13 @@ for i in reverse(100): Question 21 Level 3 -Question�� +Question£º 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: UP 5 DOWN 3 LEFT 3 RIGHT 2 -�� +¡­ The numbers after the direction are steps. Please write a program to compute the distance from current position after a sequence of movement and original point. If the distance is a float, then just print the nearest integer. Example: If the following tuples are given as input to the program: diff --git a/files.py b/files.py new file mode 100644 index 00000000..9d283a1c --- /dev/null +++ b/files.py @@ -0,0 +1,12 @@ +__author__ = 'Eoghan' + +import sys + +def main(filename): + f = open(filename, mode='rt', encoding='utf-8') + for line in f: + print(line) + f.close() + +if __name__ == '__main__': + main(sys.argv[1]) diff --git a/newclass.py b/newclass.py new file mode 100644 index 00000000..dd672371 --- /dev/null +++ b/newclass.py @@ -0,0 +1,14 @@ +__author__ = 'Eoghan' + +class UpperCase: + + def __init__(self, text): + self._text = text + + def upper(self): + print(self._text.upper()) + + def output(self): + print(self._text) + +