From 3c9c0ed0245c1a6bdd303634b125e23307622955 Mon Sep 17 00:00:00 2001 From: Noor Faziur Reza Date: Wed, 8 Oct 2014 12:16:41 +0600 Subject: [PATCH 1/5] Update 100+ Python challenging programming exercises.txt --- ...thon challenging programming exercises.txt | 60 +++++++++---------- 1 file changed, 30 insertions(+), 30 deletions(-) diff --git a/100+ Python challenging programming exercises.txt b/100+ Python challenging programming exercises.txt index 66c00215..6111150b 100644 --- a/100+ Python challenging programming exercises.txt +++ b/100+ Python challenging programming exercises.txt @@ -177,7 +177,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 +225,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 +425,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 +568,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: @@ -724,7 +724,7 @@ print "%s name is %s" % (Person.name, nico.name) #----------------------------------------# #----------------------------------------# -Question: +Question 26: Define a function which can compute the sum of two numbers. Hints: @@ -737,7 +737,7 @@ def SumFunction(number1, number2): print SumFunction(1,2) #----------------------------------------# -Question: +Question 27: Define a function that can convert a integer into a string and print it in console. Hints: @@ -752,7 +752,7 @@ printValue(3) #----------------------------------------# -Question: +Question 28: Define a function that can convert a integer into a string and print it in console. Hints: @@ -768,7 +768,7 @@ printValue(3) #----------------------------------------# 2.10 -Question: +Question 29: Define a function that can receive two integral numbers in string form and compute their sum and then print it in console. Hints: @@ -786,7 +786,7 @@ printValue("3","4") #7 2.10 -Question: +Question 30: Define a function that can accept two strings as input and concatenate them and then print it in console. Hints: @@ -803,7 +803,7 @@ printValue("3","4") #34 2.10 -Question: +Question 31: 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. Hints: @@ -830,7 +830,7 @@ printValue("one","three") #----------------------------------------# 2.10 -Question: +Question 32: 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". Hints: @@ -851,7 +851,7 @@ checkValue(7) #----------------------------------------# 2.10 -Question: +Question 33: 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. Hints: @@ -877,7 +877,7 @@ printDict() #----------------------------------------# 2.10 -Question: +Question 34: 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. Hints: @@ -900,7 +900,7 @@ printDict() #----------------------------------------# 2.10 -Question: +Question 35: 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. Hints: @@ -924,7 +924,7 @@ printDict() #----------------------------------------# 2.10 -Question: +Question 36: 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. Hints: @@ -949,7 +949,7 @@ printDict() #----------------------------------------# 2.10 -Question: +Question 37: Define a function which can generate and print a list where the values are square of numbers between 1 and 20 (both included). Hints: @@ -971,7 +971,7 @@ printList() #----------------------------------------# 2.10 -Question: +Question 38: 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. Hints: @@ -995,7 +995,7 @@ printList() #----------------------------------------# 2.10 -Question: +Question 39: 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. Hints: @@ -1019,7 +1019,7 @@ printList() #----------------------------------------# 2.10 -Question: +Question 40: 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 all values except the first 5 elements in the list. Hints: @@ -1043,7 +1043,7 @@ printList() #----------------------------------------# 2.10 -Question: +Question 41: Define a function which can generate and print a tuple where the value are square of numbers between 1 and 20 (both included). Hints: @@ -1067,7 +1067,7 @@ printTuple() #----------------------------------------# 2.10 -Question: +Question 42: 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: @@ -1085,7 +1085,7 @@ print tp2 #----------------------------------------# 2.10 -Question: +Question 43: 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: @@ -1108,7 +1108,7 @@ print tp2 #----------------------------------------# 2.14 -Question: +Question 44: 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: @@ -1127,7 +1127,7 @@ else: #----------------------------------------# 3.4 -Question: +Question 45: Write a program which can filter even numbers in a list by using filter function. The list is: [1,2,3,4,5,6,7,8,9,10]. Hints: @@ -1144,7 +1144,7 @@ print evenNumbers #----------------------------------------# 3.4 -Question: +Question 46: Write a program which can map() to make a list whose elements are square of elements in [1,2,3,4,5,6,7,8,9,10]. Hints: @@ -1160,7 +1160,7 @@ print squaredNumbers #----------------------------------------# 3.5 -Question: +Question 47: Write a program which can map() and filter() to make a list whose elements are square of even number in [1,2,3,4,5,6,7,8,9,10]. Hints: @@ -1180,7 +1180,7 @@ print evenNumbers #----------------------------------------# 3.5 -Question: +Question 48: Write a program which can filter() to make a list whose elements are even number between 1 and 20 (both included). Hints: @@ -1196,7 +1196,7 @@ print evenNumbers #----------------------------------------# 3.5 -Question: +Question 49: Write a program which can map() to make a list whose elements are square of numbers between 1 and 20 (both included). Hints: @@ -1214,7 +1214,7 @@ print squaredNumbers #----------------------------------------# 7.2 -Question: +Question 50: Define a class named American which has a static method called printNationality. Hints: From 0a08850f01292ed4f600e4f45bf26462b762f3e9 Mon Sep 17 00:00:00 2001 From: Noor Faziur Reza Date: Thu, 9 Oct 2014 16:03:47 +0600 Subject: [PATCH 2/5] Update 100+ Python challenging programming exercises.txt --- 100+ Python challenging programming exercises.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/100+ Python challenging programming exercises.txt b/100+ Python challenging programming exercises.txt index 6111150b..07f981d3 100644 --- a/100+ Python challenging programming exercises.txt +++ b/100+ Python challenging programming exercises.txt @@ -1096,7 +1096,7 @@ Use tuple() to generate a tuple from a list. Solution tp=(1,2,3,4,5,6,7,8,9,10) li=list() -for i in tp: +for i in range(0,len(tp)): if tp[i]%2==0: li.append(tp[i]) From 5da72f25ccdb649952368cdb32ca0fabaff3b7c3 Mon Sep 17 00:00:00 2001 From: Noor Faziur Reza Date: Tue, 14 Oct 2014 10:58:50 +0600 Subject: [PATCH 3/5] Update 100+ Python challenging programming exercises.txt --- ...thon challenging programming exercises.txt | 100 +++++++++--------- 1 file changed, 52 insertions(+), 48 deletions(-) diff --git a/100+ Python challenging programming exercises.txt b/100+ Python challenging programming exercises.txt index 07f981d3..4dedca72 100644 --- a/100+ Python challenging programming exercises.txt +++ b/100+ Python challenging programming exercises.txt @@ -1238,7 +1238,7 @@ American.printNationality() 7.2 -Question: +Question 51: Define a class named American and its subclass NewYorker. Hints: @@ -1266,7 +1266,7 @@ print aNewYorker 7.2 -Question: +Question 52: Define a class named Circle which can be constructed by a radius. The Circle class has a method which can compute the area. Hints: @@ -1300,7 +1300,7 @@ Hints: Use def methodName(self) to define a method. -Solution: +Solution 53: class Rectangle(object): def __init__(self, l, w): @@ -1326,7 +1326,7 @@ Hints: To override a method in super class, we can define a method with the same name in the super class. -Solution: +Solution 54: class Shape(object): def __init__(self): @@ -1362,7 +1362,7 @@ Hints: Use raise() to raise an exception. -Solution: +Solution 55: raise RuntimeError('something wrong') @@ -1375,7 +1375,7 @@ Hints: Use try/except to catch exceptions. -Solution: +Solution 56: def throws(): return 5/0 @@ -1397,7 +1397,7 @@ Hints: To define a custom exception, we need to define a class inherited from Exception. -Solution: +Solution 57: class MyError(Exception): """My own exception class @@ -1412,7 +1412,7 @@ class MyError(Exception): error = MyError("something wrong") #----------------------------------------# -Question: +Question 58: Assuming that we have some email addresses in the "username@companyname.com" format, please write program to print the user name of a given email address. Both user names and company names are composed of letters only. @@ -1440,7 +1440,7 @@ print r2.group(1) #----------------------------------------# -Question: +Question 59: Assuming that we have some email addresses in the "username@companyname.com" format, please write program to print the company name of a given email address. Both user names and company names are composed of letters only. @@ -1470,7 +1470,7 @@ print r2.group(2) #----------------------------------------# -Question: +Question 60: Write a program which accepts a sequence of words separated by whitespace as input to print the words composed of digits only. @@ -1496,7 +1496,7 @@ print re.findall("\d+",s) #----------------------------------------# -Question: +Question 61: Print a unicode string "hello world". @@ -1511,7 +1511,7 @@ unicodeString = u"hello world!" print unicodeString #----------------------------------------# -Write a program to read an ASCII string and to convert it to a unicode string encoded by utf-8. +Question 62: Write a program to read an ASCII string and to convert it to a unicode string encoded by utf-8. Hints: @@ -1524,7 +1524,7 @@ u = unicode( s ,"utf-8") print u #----------------------------------------# -Question: +Question 63: Write a special comment to indicate a Python source code file is in unicode. @@ -1535,7 +1535,7 @@ Solution: # -*- coding: utf-8 -*- #----------------------------------------# -Question: +Question 64: Write a program to compute 1/2+2/3+3/4+...+n/n+1 with a given n input by console (n>0). @@ -1563,7 +1563,7 @@ print sum #----------------------------------------# -Question: +Question 65: Write a program to compute: @@ -1599,7 +1599,7 @@ print f(n) #----------------------------------------# -Question: +Question 66: The Fibonacci Sequence is computed based on the following formula: @@ -1641,7 +1641,7 @@ print f(n) #----------------------------------------# -Question: +Question 67: The Fibonacci Sequence is computed based on the following formula: @@ -1683,7 +1683,7 @@ print ",".join(values) #----------------------------------------# -Question: +Question 68: Please write a program using generator to print the even numbers between 0 and n in comma separated form while n is input by console. @@ -1721,7 +1721,7 @@ print ",".join(values) #----------------------------------------# -Question: +Question 69: Please write a program using generator to print the numbers which can be divisible by 5 and 7 between 0 and n in comma separated form while n is input by console. @@ -1756,7 +1756,7 @@ print ",".join(values) #----------------------------------------# -Question: +Question 70: Please write assert statements to verify that every number in the list [2,4,6,8] is even. @@ -1775,7 +1775,7 @@ for i in li: #----------------------------------------# -Question: +Question 71: Please write a program which accepts basic mathematic expression from console and print the evaluation result. @@ -1799,7 +1799,7 @@ print eval(expression) #----------------------------------------# -Question: +Question 72: Please write a binary search function which searches an item in a sorted list. The function should return the index of element to be searched in the list. @@ -1834,7 +1834,7 @@ print bin_search(li,12) #----------------------------------------# -Question: +Question 73: Please write a binary search function which searches an item in a sorted list. The function should return the index of element to be searched in the list. @@ -1869,7 +1869,7 @@ print bin_search(li,12) #----------------------------------------# -Question: +Question 74: Please generate a random float where the value is between 10 and 100 using Python math module. @@ -1885,7 +1885,7 @@ import random print random.random()*100 #----------------------------------------# -Question: +Question 75: Please generate a random float where the value is between 5 and 95 using Python math module. @@ -1902,7 +1902,7 @@ print random.random()*100-5 #----------------------------------------# -Question: +Question 76: Please write a program to output a random even number between 0 and 10 inclusive using random module and list comprehension. @@ -1919,7 +1919,7 @@ print random.choice([i for i in range(11) if i%2==0]) #----------------------------------------# -Question: +Question 77: Please write a program to output a random number, which is divisible by 5 and 7, between 0 and 10 inclusive using random module and list comprehension. @@ -1938,7 +1938,7 @@ print random.choice([i for i in range(201) if i%5==0 and i%7==0]) #----------------------------------------# -Question: +Question 78: Please write a program to generate a list with 5 random numbers between 100 and 200 inclusive. @@ -1954,7 +1954,7 @@ import random print random.sample(range(100), 5) #----------------------------------------# -Question: +Question 79: Please write a program to randomly generate a list with 5 even numbers between 100 and 200 inclusive. @@ -1971,7 +1971,7 @@ print random.sample([i for i in range(100,201) if i%2==0], 5) #----------------------------------------# -Question: +Question 80: Please write a program to randomly generate a list with 5 numbers, which are divisible by 5 and 7 , between 1 and 1000 inclusive. @@ -1988,7 +1988,7 @@ print random.sample([i for i in range(1,1001) if i%5==0 and i%7==0], 5) #----------------------------------------# -Question: +Question 81: Please write a program to randomly print a integer number between 7 and 15 inclusive. @@ -2005,7 +2005,7 @@ print random.randrange(7,16) #----------------------------------------# -Question: +Question 82: Please write a program to compress and decompress the string "hello world!hello world!hello world!hello world!". @@ -2024,7 +2024,7 @@ print t print zlib.decompress(t) #----------------------------------------# -Question: +Question 83: Please write a program to print the running time of execution of "1+1" for 100 times. @@ -2040,7 +2040,7 @@ t = Timer("for i in range(100):1+1") print t.timeit() #----------------------------------------# -Question: +Question 84: Please write a program to shuffle and print the list [3,6,7,8]. @@ -2057,7 +2057,7 @@ shuffle(li) print li #----------------------------------------# -Question: +Question 85: Please write a program to shuffle and print the list [3,6,7,8]. @@ -2076,7 +2076,7 @@ print li #----------------------------------------# -Question: +Question 86: Please write a program to generate all sentences where subject is in ["I", "You"] and verb is in ["Play", "Love"] and the object is in ["Hockey","Football"]. @@ -2096,6 +2096,8 @@ for i in range(len(subjects)): #----------------------------------------# +Question 87: + Please write a program to print the list after removing delete even numbers in [5,6,77,45,22,12,24]. Hints: @@ -2108,7 +2110,7 @@ li = [x for x in li if x%2!=0] print li #----------------------------------------# -Question: +Question 88: By using list comprehension, please write a program to print the list after removing delete numbers which are divisible by 5 and 7 in [12,24,35,70,88,120,155]. @@ -2123,7 +2125,7 @@ print li #----------------------------------------# -Question: +Question 89: By using list comprehension, please write a program to print the list after removing the 0th, 2nd, 4th,6th numbers in [12,24,35,70,88,120,155]. @@ -2139,7 +2141,7 @@ print li #----------------------------------------# -Question: +Question 90: By using list comprehension, please write a program generate a 3*5*8 3D array whose each element is 0. @@ -2152,7 +2154,7 @@ array = [[ [0 for col in range(8)] for col in range(5)] for row in range(3)] print array #----------------------------------------# -Question: +Question 91: By using list comprehension, please write a program to print the list after removing the 0th,4th,5th numbers in [12,24,35,70,88,120,155]. @@ -2170,7 +2172,7 @@ print li #----------------------------------------# -Question: +Question 92: By using list comprehension, please write a program to print the list after removing the value 24 in [12,24,35,24,88,120,155]. @@ -2185,7 +2187,7 @@ print li #----------------------------------------# -Question: +Question 93: With two given lists [1,3,6,78,35,55] and [12,24,35,24,88,120,155], write a program to make a list whose elements are intersection of the above given lists. @@ -2202,6 +2204,8 @@ print li #----------------------------------------# +Question 94: + With a given list [12,24,35,24,88,120,155,88,120,155], write a program to print this list after removing all duplicate values with original order reserved. Hints: @@ -2224,7 +2228,7 @@ print removeDuplicate(li) #----------------------------------------# -Question: +Question 95: Define a class Person and its two child classes: Male and Female. All classes have a method "getGender" which can print "Male" for Male class and "Female" for Female class. @@ -2253,7 +2257,7 @@ print aFemale.getGender() #----------------------------------------# -Question: +Question 96: Please write a program which count and print the numbers of each character in a string input by console. @@ -2286,7 +2290,7 @@ print '\n'.join(['%s,%s' % (k, v) for k, v in dic.items()]) #----------------------------------------# -Question: +Question 97: Please write a program which accepts a string from console and print it in reverse order. @@ -2310,7 +2314,7 @@ print s #----------------------------------------# -Question: +Question 98: Please write a program which accepts a string from console and print the characters that have even indexes. @@ -2334,7 +2338,7 @@ print s #----------------------------------------# -Question: +Question 99: Please write a program which prints all permutations of [1,2,3] @@ -2348,7 +2352,7 @@ import itertools print list(itertools.permutations([1,2,3])) #----------------------------------------# -Question: +Question 100: 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? From 7957d86403fef13b02a2e1d0e48277c660d0f6c4 Mon Sep 17 00:00:00 2001 From: Noor Faziur Reza Date: Sun, 9 Nov 2014 19:43:03 +0600 Subject: [PATCH 4/5] Update 100+ Python challenging programming exercises.txt Just adding another way of solving the problem 92 --- 100+ Python challenging programming exercises.txt | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/100+ Python challenging programming exercises.txt b/100+ Python challenging programming exercises.txt index 4dedca72..c908a44e 100644 --- a/100+ Python challenging programming exercises.txt +++ b/100+ Python challenging programming exercises.txt @@ -2185,6 +2185,11 @@ li = [12,24,35,24,88,120,155] li = [x for x in li if x!=24] print li +another way of removing all repeated elements including original: +from collections import Counter +li = [12,24,35,24,88,120,155] +li1 = [k for k,v in Counter(li).iteritems() if v==1] # counter indexes the list by making it a dictionary, if any item is repeated than it's index would not be 1 +print li1 #----------------------------------------# Question 93: From 2f1ff194cc3ecd1642269cf52aa9c9a1da53d8b4 Mon Sep 17 00:00:00 2001 From: Noor Faziur Reza Date: Sun, 9 Nov 2014 20:12:32 +0600 Subject: [PATCH 5/5] Update 100+ Python challenging programming exercises.txt Adding an expansion of problem 93 --- 100+ Python challenging programming exercises.txt | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/100+ Python challenging programming exercises.txt b/100+ Python challenging programming exercises.txt index c908a44e..99b13c57 100644 --- a/100+ Python challenging programming exercises.txt +++ b/100+ Python challenging programming exercises.txt @@ -2207,6 +2207,14 @@ set1 &= set2 li=list(set1) print li +for intersection of multiple (more than two) lists: + +set1 = set([1,3,6,78,35,55]) +set2 = set([12,24,35,24,88,120,155]) +set3 = set([10,11,13,160,35,98,52]) +set4 = set.intersection(set1,set2,set3) +li = list(set4) +print li #----------------------------------------# Question 94: