diff --git a/100+ Python challenging programming exercises.txt b/100+ Python challenging programming exercises.txt index 66c00215..131c1b40 100644 --- a/100+ Python challenging programming exercises.txt +++ b/100+ Python challenging programming exercises.txt @@ -15,16 +15,41 @@ Solution 3. Questions + + + + + + + + + + + + + + + + + + + + + + + + + #----------------------------------------# Question 1 Level 1 Question: -Write a program which will find all such numbers which are divisible by 7 but are not a multiple of 5, -between 2000 and 3200 (both included). +Write a program which will find all such numbers which are divisible by 7 +but are not a multiple of 5, between 2000 and 3200 (both included). The numbers obtained should be printed in a comma-separated sequence on a single line. -Hints: +Hints: Consider use range(#begin, #end) method Solution: @@ -156,7 +181,7 @@ The output of the program should be: 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) -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. Solution: #!/usr/bin/env python @@ -177,12 +202,12 @@ 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 Then, the output of the program should be: -[[0, 0, 0, 0, 0], [0, 1, 2, 3, 4], [0, 2, 4, 6, 8]] +[[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. @@ -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: @@ -617,7 +642,7 @@ Question 22 Level 3 Question: -Write a program to compute the frequency of the words from the input. The output should output after sorting the key alphanumerically. +Write a program to compute the frequency of the words from the input. The output should output after sorting the key alphanumerically. Suppose the following input is supplied to the program: New to Python or choosing between Python 2 and Python 3? Read Python 2 or Python 3. Then, the output should be: @@ -675,7 +700,7 @@ Question: 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. Please write a program to print some Python built-in functions documents, such as abs(), int(), raw_input() And add document for your own function - + Hints: The built-in document method is __doc__ @@ -686,7 +711,7 @@ print raw_input.__doc__ def square(num): '''Return the square value of the input number. - + The input number must be integer. ''' return num ** 2 @@ -710,7 +735,7 @@ Solution: class Person: # Define the class parameter "name" name = "Person" - + def __init__(self, name = None): # self.name is the instance parameter self.name = name @@ -749,7 +774,7 @@ def printValue(n): print str(n) printValue(3) - + #----------------------------------------# Question: @@ -821,7 +846,7 @@ def printValue(s1,s2): else: print s1 print s2 - + printValue("one","three") @@ -843,7 +868,7 @@ def checkValue(n): print "It is an even number" else: print "It is an odd number" - + checkValue(7) @@ -866,7 +891,7 @@ def printDict(): d[2]=2**2 d[3]=3**2 print d - + printDict() @@ -892,7 +917,7 @@ def printDict(): for i in range(1,21): d[i]=i**2 print d - + printDict() @@ -915,9 +940,9 @@ def printDict(): d=dict() for i in range(1,21): d[i]=i**2 - for (k,v) in d.items(): + for (k,v) in d.items(): print v - + printDict() @@ -939,9 +964,9 @@ def printDict(): d=dict() for i in range(1,21): d[i]=i**2 - for k in d.keys(): + for k in d.keys(): print k - + printDict() @@ -964,7 +989,7 @@ def printList(): for i in range(1,21): li.append(i**2) print li - + printList() @@ -987,7 +1012,7 @@ def printList(): for i in range(1,21): li.append(i**2) print li[:5] - + printList() @@ -1011,7 +1036,7 @@ def printList(): for i in range(1,21): li.append(i**2) print li[-5:] - + printList() @@ -1035,7 +1060,7 @@ def printList(): for i in range(1,21): li.append(i**2) print li[5:] - + printList() @@ -1044,7 +1069,7 @@ printList() 2.10 Question: -Define a function which can generate and print a tuple where the value are square of numbers between 1 and 20 (both included). +Define a function which can generate and print a tuple where the value are square of numbers between 1 and 20 (both included). Hints: @@ -1059,7 +1084,7 @@ def printTuple(): for i in range(1,21): li.append(i**2) print tuple(li) - + printTuple() @@ -1068,7 +1093,7 @@ printTuple() 2.10 Question: -With a given tuple (1,2,3,4,5,6,7,8,9,10), write a program to print the first half values in one line and the last half values in one line. +With a given tuple (1,2,3,4,5,6,7,8,9,10), write a program to print the first half values in one line and the last half values in one line. Hints: @@ -1086,7 +1111,7 @@ print tp2 2.10 Question: -Write a program to generate and print another tuple whose values are even numbers in the given tuple (1,2,3,4,5,6,7,8,9,10). +Write a program to generate and print another tuple whose values are even numbers in the given tuple (1,2,3,4,5,6,7,8,9,10). Hints: @@ -1109,7 +1134,7 @@ print tp2 2.14 Question: -Write a program which accepts a string as input to print "Yes" if the string is "yes" or "YES" or "Yes", otherwise print "No". +Write a program which accepts a string as input to print "Yes" if the string is "yes" or "YES" or "Yes", otherwise print "No". Hints: @@ -1239,7 +1264,7 @@ American.printNationality() 7.2 Question: -Define a class named American and its subclass NewYorker. +Define a class named American and its subclass NewYorker. Hints: @@ -1267,7 +1292,7 @@ print aNewYorker 7.2 Question: -Define a class named Circle which can be constructed by a radius. The Circle class has a method which can compute the area. +Define a class named Circle which can be constructed by a radius. The Circle class has a method which can compute the area. Hints: @@ -1294,7 +1319,7 @@ print aCircle.area() 7.2 -Define a class named Rectangle which can be constructed by a length and width. The Rectangle class has a method which can compute the area. +Define a class named Rectangle which can be constructed by a length and width. The Rectangle class has a method which can compute the area. Hints: @@ -2350,7 +2375,7 @@ print list(itertools.permutations([1,2,3])) #----------------------------------------# Question: -Write a program to solve a classic ancient Chinese puzzle: +Write a program to solve a classic ancient Chinese puzzle: We count 35 heads and 94 legs among the chickens and rabbits in a farm. How many rabbits and how many chickens do we have? Hint: @@ -2372,5 +2397,3 @@ solutions=solve(numheads,numlegs) print solutions #----------------------------------------# - - diff --git a/Q001.py b/Q001.py new file mode 100644 index 00000000..62dffb68 --- /dev/null +++ b/Q001.py @@ -0,0 +1,14 @@ +# Question 001 + +#Question: +#Write a program which will find all such numbers which are divisible by 7 +#but are not a multiple of 5, between 2000 and 3200 (both included). +#The numbers obtained should be printed in a comma-separated sequence on a single line. + +testNumber = 2000 +finalAnswerSet = "" + +for testNumber in range(2000,3200,1): + if (testNumber % 7 == 0 and testNumber % 5 != 0): + finalAnswerSet = finalAnswerSet + ",{}".format(testNumber) +print finalAnswerSet \ No newline at end of file diff --git a/Q002.py b/Q002.py new file mode 100644 index 00000000..3340116e --- /dev/null +++ b/Q002.py @@ -0,0 +1,32 @@ +# Question 002 + +#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 + +# This will do the factorial, but is not written as a method +inputNumber = 8 +outputNumber = 1 + +while (inputNumber > 1): + outputNumber = outputNumber * inputNumber + inputNumber -= 1 + +print outputNumber + +# Writing the factorial method +def factorial(inputNumber): + outputNumber = 1 + while (inputNumber > 1): + outputNumber = outputNumber * inputNumber + inputNumber -= 1 + return outputNumber + +# Implementing the factorial method +print "Enter a number to compute the factorial of: " +inputFromUser = int(raw_input()) +print factorial(inputFromUser) \ No newline at end of file diff --git a/Q003.py b/Q003.py new file mode 100644 index 00000000..06855145 --- /dev/null +++ b/Q003.py @@ -0,0 +1,22 @@ +# Question 003 + +#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. +#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} + +# Get the input number +print "Enter a number to create a dictionary up to: " +inputNumber = int(raw_input()) + +# Create a dictionary to hold all the numbers +dictionary = dict() + +# Fill the dictionary with a for loop +for count in range (1, inputNumber + 1): + dictionary[count] = count * count + +# Print the result to the user +print dictionary \ No newline at end of file diff --git a/Q004.py b/Q004.py new file mode 100644 index 00000000..6fcfa69b --- /dev/null +++ b/Q004.py @@ -0,0 +1,26 @@ +# Question 004 + +#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') + +# Read the list of numbers from the user +print "Enter a list of numbers to create a list and tuple of" +inputString = raw_input() + +# Split on the commas +list = inputString.split(',') + +# Create the tuple list +tuple = tuple(list) + +# Print the results to the user +print "\nThe list is:" +print list +print "The tuple is:" +print tuple \ No newline at end of file diff --git a/Q005.py b/Q005.py new file mode 100644 index 00000000..982c7bd9 --- /dev/null +++ b/Q005.py @@ -0,0 +1,32 @@ +# Question 005 + +#Question: +#Define a class which has at least two methods: +#getString: to get a string from console input +#printString: to print the string in upper case. +#Also please include simple test function to test the class methods. + +# Create a class named StringHandling +class StringHandling(object): + # The init will initialize a string variable in the class + def __init__(self): + self.string = "" + # getString(self) will read the input from the user + def getString(self): + print "Enter a string:" + self.string = raw_input() + # printString(self) will print the string in uppercase + def printString(self): + print self.string.upper() + +# Define a test method that expects a StringHandling class type +def test(testingClass): + # Call the getString method + testingClass.getString() + # Call the printString method + testingClass.printString() + +# Create a test object called testObject +testObject = StringHandling() +# Test the testObject with the test method +test(testObject) \ No newline at end of file diff --git a/Q006.py b/Q006.py new file mode 100644 index 00000000..8af3f676 --- /dev/null +++ b/Q006.py @@ -0,0 +1,30 @@ +# Question 006 + +#Question: +#Write a program that calculates and prints the value according to the given formula: +#Q = Square root of [(2 * C * D)/H] +#Following are the fixed values of C and H: +#C is 50. H is 30. +#D is the variable whose values should be input to your program in a comma-separated sequence. +#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 + +# Import the math library +import math + +# Equation: Q = ((2 * C * D)/H) ^ (0.5) +# Constants +c = 50 +h = 30 + +inputString = raw_input() +inputList = [value for value in inputString.split(',')] +outputValues = [] + +for position in inputList: + outputValues = ((2 * c * position)/h) ** 0.5 + +print outputValues diff --git a/README b/README deleted file mode 100644 index e69de29b..00000000