From ef83ca54075d7492746a940828bed1667a8749b3 Mon Sep 17 00:00:00 2001 From: Eoghan Nolan Date: Tue, 16 Jun 2015 22:48:36 +0100 Subject: [PATCH 1/4] Changes to questions 1 through 4 inclusive to modify the answers to be Python3 correct. --- ...thon challenging programming exercises.txt | 56 ++++++++++++------- 1 file changed, 36 insertions(+), 20 deletions(-) diff --git a/100+ Python challenging programming exercises.txt b/100+ Python challenging programming exercises.txt index 66c00215..3a1a2263 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,17 @@ 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 +75,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 +87,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 +101,12 @@ 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. +Write a program which accepts a sequence of comma-separated numbers in a string format from console and then generates +a list and a tuple which contains every number in the string. Suppose the following input is supplied to the program: -34,67,55,33,12,98 + +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 = values.split(",") +t = tuple(l) +print(l) +print(t) + + #----------------------------------------# #----------------------------------------# From 27708b657467dd34cd6276015fd33fdc43c8e1ff Mon Sep 17 00:00:00 2001 From: Eoghan Nolan Date: Thu, 18 Jun 2015 17:54:05 +0100 Subject: [PATCH 2/4] Update 100+ Python challenging programming exercises.txt --- ...thon challenging programming exercises.txt | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/100+ Python challenging programming exercises.txt b/100+ Python challenging programming exercises.txt index 3a1a2263..f1182019 100644 --- a/100+ Python challenging programming exercises.txt +++ b/100+ Python challenging programming exercises.txt @@ -57,7 +57,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 with a defined variable. +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 @@ -101,9 +102,8 @@ Question 4 Level 1 Question: -Write a program which accepts a sequence of comma-separated numbers in a string format from console and then generates -a list and a tuple which contains every number in the string. -Suppose the following input is supplied to the program: +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 @@ -117,7 +117,7 @@ tuple() method can convert list to tuple Solution: numbers = 34,67,55,33,12,98 -l = values.split(",") +l = numbers.split(",") t = tuple(l) print(l) print(t) @@ -193,7 +193,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 @@ -241,7 +241,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 @@ -441,7 +441,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 @@ -584,13 +584,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: From 5eca1493f9e8e25c8c4ea5de3ed3396e65214a4a Mon Sep 17 00:00:00 2001 From: Eoghan Nolan Date: Fri, 26 Jun 2015 10:41:14 +0100 Subject: [PATCH 3/4] First commit of file for python programming --- files.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 files.py 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]) From 47f767f85cc770bf40bb40ba94d5397ae44d2d9a Mon Sep 17 00:00:00 2001 From: Eoghan Nolan Date: Fri, 26 Jun 2015 15:50:27 +0100 Subject: [PATCH 4/4] Updated question 5 Class and Methods --- ...thon challenging programming exercises.txt | 29 ++++++++++++------- newclass.py | 14 +++++++++ 2 files changed, 33 insertions(+), 10 deletions(-) create mode 100644 newclass.py diff --git a/100+ Python challenging programming exercises.txt b/100+ Python challenging programming exercises.txt index f1182019..51cdda64 100644 --- a/100+ Python challenging programming exercises.txt +++ b/100+ Python challenging programming exercises.txt @@ -139,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() #----------------------------------------# #----------------------------------------# 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) + +