From e42baf2b5aa2f96941d4b4b10533cb084bb3fda3 Mon Sep 17 00:00:00 2001 From: Kamrul Ahsan Date: Tue, 17 Sep 2019 11:42:47 +0600 Subject: [PATCH 1/9] 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 97af5aaf..1c5cf555 100644 --- a/100+ Python challenging programming exercises.txt +++ b/100+ Python challenging programming exercises.txt @@ -172,7 +172,7 @@ print ','.join(value) #----------------------------------------# #----------------------------------------# -Question 7 +Question 7 ******************************************************* (tough) Level 2 Question: From bff8a0f2b4a4caec7bdb7a2af72b9e47250a8cb2 Mon Sep 17 00:00:00 2001 From: Kamrul Ahsan Date: Thu, 13 Feb 2020 13:08:33 +0600 Subject: [PATCH 2/9] Update 100+ Python challenging programming exercises.txt --- ...thon challenging programming exercises.txt | 362 ++++++++---------- 1 file changed, 164 insertions(+), 198 deletions(-) diff --git a/100+ Python challenging programming exercises.txt b/100+ Python challenging programming exercises.txt index 1c5cf555..b868ac29 100644 --- a/100+ Python challenging programming exercises.txt +++ b/100+ Python challenging programming exercises.txt @@ -33,7 +33,7 @@ for i in range(2000, 3201): if (i%7==0) and (i%5!=0): l.append(str(i)) -print ','.join(l) +print( ','.join(l)) #----------------------------------------# #----------------------------------------# @@ -57,8 +57,8 @@ def fact(x): return 1 return x * fact(x - 1) -x=int(raw_input()) -print fact(x) +x=int(input()) +print(fact(x)) #----------------------------------------# #----------------------------------------# @@ -77,12 +77,12 @@ In case of input data being supplied to the question, it should be assumed to be Consider use dict() Solution: -n=int(raw_input()) +n=int(input()) d=dict() for i in range(1,n+1): d[i]=i*i -print d +print(d) #----------------------------------------# #----------------------------------------# @@ -102,11 +102,11 @@ 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() +values=str(input()) l=values.split(",") t=tuple(l) -print l -print t +print(l) +print(t) #----------------------------------------# #----------------------------------------# @@ -124,16 +124,16 @@ Use __init__ method to construct some parameters Solution: class InputOutString(object): - def __init__(self): + def __init__(self,s): self.s = "" def getString(self): - self.s = raw_input() + self.s = str(input()) def printString(self): - print self.s.upper() + print(self.s.upper()) -strObj = InputOutString() +strObj = InputOutString(object) strObj.getString() strObj.printString() #----------------------------------------# @@ -158,21 +158,30 @@ 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. -Solution: +Solution1: #!/usr/bin/env python import math c=50 h=30 value = [] -items=[x for x in raw_input().split(',')] +items=[x for x in input().split(',')] for d in items: value.append(str(int(round(math.sqrt(2*c*float(d)/h))))) -print ','.join(value) +print (','.join(value)) + #----------------------------------------# +Solution 2: +import math +c=50 +h=30 +d=int(input("Enter D: ") +Q = int(2*c*d/h) +print(round(math.sqrt(Q))) + #----------------------------------------# -Question 7 ******************************************************* (tough) +Question 7 Level 2 Question: @@ -188,7 +197,7 @@ 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. Solution: -input_str = raw_input() +input_str = input() dimensions=[int(x) for x in input_str.split(',')] rowNum=dimensions[0] colNum=dimensions[1] @@ -198,7 +207,7 @@ for row in range(rowNum): for col in range(colNum): multilist[row][col]= row*col -print multilist +print (multilist) #----------------------------------------# #----------------------------------------# @@ -216,9 +225,9 @@ Hints: In case of input data being supplied to the question, it should be assumed to be a console input. Solution: -items=[x for x in raw_input().split(',')] -items.sort() -print ','.join(items) +items = input("Input comma separated sequence of words") +words = [word for word in items.split(",")] +print(",".join(sorted(list(set(words))))) #----------------------------------------# #----------------------------------------# @@ -240,14 +249,14 @@ In case of input data being supplied to the question, it should be assumed to be Solution: lines = [] while True: - s = raw_input() + s = input() if s: lines.append(s.upper()) else: - break; + break for sentence in lines: - print sentence + print (sentence) #----------------------------------------# #----------------------------------------# @@ -266,9 +275,9 @@ In case of input data being supplied to the question, it should be assumed to be We use set container to remove duplicated data automatically and then use sorted() to sort the data. Solution: -s = raw_input() +s = input() words = [word for word in s.split(" ")] -print " ".join(sorted(list(set(words)))) +print (" ".join(sorted(list(set(words))))) #----------------------------------------# #----------------------------------------# @@ -288,13 +297,13 @@ In case of input data being supplied to the question, it should be assumed to be Solution: value = [] -items=[x for x in raw_input().split(',')] +items=[x for x in input().split(',')] for p in items: intp = int(p, 2) if not intp%5: value.append(p) -print ','.join(value) +print(','.join(value)) #----------------------------------------# #----------------------------------------# @@ -314,7 +323,7 @@ for i in range(1000, 3001): s = str(i) if (int(s[0])%2==0) and (int(s[1])%2==0) and (int(s[2])%2==0) and (int(s[3])%2==0): values.append(s) -print ",".join(values) +print (",".join(values)) #----------------------------------------# #----------------------------------------# @@ -333,7 +342,7 @@ Hints: In case of input data being supplied to the question, it should be assumed to be a console input. Solution: -s = raw_input() +s = input() d={"DIGITS":0, "LETTERS":0} for c in s: if c.isdigit(): @@ -342,8 +351,8 @@ for c in s: d["LETTERS"]+=1 else: pass -print "LETTERS", d["LETTERS"] -print "DIGITS", d["DIGITS"] +print ("LETTERS", d["LETTERS"]) +print ("DIGITS", d["DIGITS"]) #----------------------------------------# #----------------------------------------# @@ -362,7 +371,7 @@ Hints: In case of input data being supplied to the question, it should be assumed to be a console input. Solution: -s = raw_input() +s = input() d={"UPPER CASE":0, "LOWER CASE":0} for c in s: if c.isupper(): @@ -371,8 +380,8 @@ for c in s: d["LOWER CASE"]+=1 else: pass -print "UPPER CASE", d["UPPER CASE"] -print "LOWER CASE", d["LOWER CASE"] +print ("UPPER CASE", d["UPPER CASE"]) +print ("LOWER CASE", d["LOWER CASE"]) #----------------------------------------# #----------------------------------------# @@ -390,12 +399,12 @@ Hints: In case of input data being supplied to the question, it should be assumed to be a console input. Solution: -a = raw_input() +a = input() n1 = int( "%s" % a ) n2 = int( "%s%s" % (a,a) ) n3 = int( "%s%s%s" % (a,a,a) ) n4 = int( "%s%s%s%s" % (a,a,a,a) ) -print n1+n2+n3+n4 +print (n1+n2+n3+n4) #----------------------------------------# #----------------------------------------# @@ -413,9 +422,9 @@ Hints: In case of input data being supplied to the question, it should be assumed to be a console input. Solution: -values = raw_input() +values = input() numbers = [x for x in values.split(",") if int(x)%2!=0] -print ",".join(numbers) +print (",".join(numbers)) #----------------------------------------# Question 17 @@ -441,7 +450,7 @@ In case of input data being supplied to the question, it should be assumed to be Solution: netAmount = 0 while True: - s = raw_input() + s = input() if not s: break values = s.split(" ") @@ -453,7 +462,7 @@ while True: netAmount-=amount else: pass -print netAmount +print (netAmount) #----------------------------------------# #----------------------------------------# @@ -501,7 +510,7 @@ for p in items: else: pass value.append(p) -print ",".join(value) +print (",".join(value)) #----------------------------------------# #----------------------------------------# @@ -532,12 +541,12 @@ from operator import itemgetter, attrgetter l = [] while True: - s = raw_input() + s = input() if not s: break l.append(tuple(s.split(","))) -print sorted(l, key=itemgetter(0,1,2)) +print (sorted(l, key=itemgetter(0,1,2))) #----------------------------------------# #----------------------------------------# @@ -560,7 +569,7 @@ def putNumbers(n): yield j for i in reverse(100): - print i + print (i) #----------------------------------------# #----------------------------------------# @@ -608,7 +617,7 @@ while True: else: pass -print int(round(math.sqrt(pos[1]**2+pos[0]**2))) +print (int(round(math.sqrt(pos[1]**2+pos[0]**2)))) #----------------------------------------# #----------------------------------------# @@ -637,7 +646,7 @@ In case of input data being supplied to the question, it should be assumed to be Solution: freq = {} # frequency of words in text -line = raw_input() +line = input() for word in line.split(): freq[word] = freq.get(word,0)+1 @@ -645,7 +654,7 @@ words = freq.keys() words.sort() for w in words: - print "%s:%d" % (w,freq[w]) + print ("%s:%d" % (w,freq[w])) #----------------------------------------# #----------------------------------------# @@ -662,8 +671,8 @@ Solution: def square(num): return num ** 2 -print square(2) -print square(3) +print (square(2)) +print (square(3)) #----------------------------------------# #----------------------------------------# @@ -679,9 +688,9 @@ Hints: The built-in document method is __doc__ Solution: -print abs.__doc__ -print int.__doc__ -print raw_input.__doc__ +print (abs.__doc__) +print (int.__doc__) +print (raw_input.__doc__) def square(num): '''Return the square value of the input number. @@ -690,8 +699,8 @@ def square(num): ''' return num ** 2 -print square(2) -print square.__doc__ +print (square(2)) +print (square.__doc__) #----------------------------------------# #----------------------------------------# @@ -715,11 +724,11 @@ class Person: self.name = name jeffrey = Person("Jeffrey") -print "%s name is %s" % (Person.name, jeffrey.name) +print ("%s name is %s" % (Person.name, jeffrey.name)) nico = Person() nico.name = "Nico" -print "%s name is %s" % (Person.name, nico.name) +print ("%s name is %s" % (Person.name, nico.name)) #----------------------------------------# #----------------------------------------# @@ -814,12 +823,12 @@ def printValue(s1,s2): len1 = len(s1) len2 = len(s2) if len1>len2: - print s1 + print (s1) elif len2>len1: - print s2 + print (s2) else: - print s1 - print s2 + print (s1) + print (s2) printValue("one","three") @@ -839,9 +848,9 @@ Use % operator to check if a number is even or odd. Solution def checkValue(n): if n%2 == 0: - print "It is an even number" + print ("It is an even number") else: - print "It is an odd number" + print ("It is an odd number") checkValue(7) @@ -864,7 +873,7 @@ def printDict(): d[1]=1 d[2]=2**2 d[3]=3**2 - print d + print(d) printDict() @@ -890,7 +899,7 @@ def printDict(): d=dict() for i in range(1,21): d[i]=i**2 - print d + print (d) printDict() @@ -915,7 +924,7 @@ def printDict(): for i in range(1,21): d[i]=i**2 for (k,v) in d.items(): - print v + print (v) printDict() @@ -939,8 +948,7 @@ def printDict(): for i in range(1,21): d[i]=i**2 for k in d.keys(): - print k - + print (k) printDict() @@ -962,9 +970,8 @@ def printList(): li=list() for i in range(1,21): li.append(i**2) - print li + print (li) - printList() #----------------------------------------# @@ -985,7 +992,7 @@ def printList(): li=list() for i in range(1,21): li.append(i**2) - print li[:5] + print (li[:5]) printList() @@ -1009,8 +1016,7 @@ def printList(): li=list() for i in range(1,21): li.append(i**2) - print li[-5:] - + print (li[-5:]) printList() @@ -1033,9 +1039,8 @@ def printList(): li=list() for i in range(1,21): li.append(i**2) - print li[5:] + print (li[5:]) - printList() @@ -1057,7 +1062,7 @@ def printTuple(): li=list() for i in range(1,21): li.append(i**2) - print tuple(li) + print (tuple(li)) printTuple() @@ -1077,8 +1082,8 @@ Solution tp=(1,2,3,4,5,6,7,8,9,10) tp1=tp[:5] tp2=tp[5:] -print tp1 -print tp2 +print (tp1) +print (tp2) #----------------------------------------# @@ -1100,7 +1105,7 @@ for i in tp: li.append(tp[i]) tp2=tuple(li) -print tp2 +print (tp2) @@ -1117,9 +1122,9 @@ Use if statement to judge condition. Solution s= raw_input() if s=="yes" or s=="YES" or s=="Yes": - print "Yes" + print ("Yes") else: - print "No" + print ("No") @@ -1137,7 +1142,7 @@ Use lambda to define anonymous functions. Solution li = [1,2,3,4,5,6,7,8,9,10] evenNumbers = filter(lambda x: x%2==0, li) -print evenNumbers +print (evenNumbers) #----------------------------------------# @@ -1154,7 +1159,7 @@ Use lambda to define anonymous functions. Solution li = [1,2,3,4,5,6,7,8,9,10] squaredNumbers = map(lambda x: x**2, li) -print squaredNumbers +print (squaredNumbers) #----------------------------------------# 3.5 @@ -1171,7 +1176,7 @@ Use lambda to define anonymous functions. Solution li = [1,2,3,4,5,6,7,8,9,10] evenNumbers = map(lambda x: x**2, filter(lambda x: x%2==0, li)) -print evenNumbers +print (evenNumbers) @@ -1189,7 +1194,7 @@ Use lambda to define anonymous functions. Solution evenNumbers = filter(lambda x: x%2==0, range(1,21)) -print evenNumbers +print (evenNumbers) #----------------------------------------# @@ -1205,7 +1210,7 @@ Use lambda to define anonymous functions. Solution squaredNumbers = map(lambda x: x**2, range(1,21)) -print squaredNumbers +print (squaredNumbers) @@ -1224,7 +1229,7 @@ Solution class American(object): @staticmethod def printNationality(): - print "America" + print ("America") anAmerican = American() anAmerican.printNationality() @@ -1254,8 +1259,8 @@ class NewYorker(American): anAmerican = American() aNewYorker = NewYorker() -print anAmerican -print aNewYorker +print (anAmerican) +print (aNewYorker) @@ -1280,9 +1285,9 @@ class Circle(object): def area(self): return self.radius**2*3.14 - -aCircle = Circle(2) -print aCircle.area() +r=int(input("Enter the radius: ")) +aCircle = Circle(r) +print (aCircle.area()) @@ -1308,9 +1313,10 @@ class Rectangle(object): def area(self): return self.length*self.width - -aRectangle = Rectangle(2,10) -print aRectangle.area() +l=int(input("Enter the length: ")) +w=int(input("Enter the width: ")) +aRectangle = Rectangle(l,w) +print (aRectangle.area()) @@ -1341,9 +1347,9 @@ class Square(Shape): def area(self): return self.length*self.length - -aSquare= Square(3) -print aSquare.area() +a=int(input("Enter a: ")) +aSquare= Square(a) +print (aSquare.area()) @@ -1382,11 +1388,11 @@ def throws(): try: throws() except ZeroDivisionError: - print "division by zero!" + print ("Division by zero!") except Exception, err: - print 'Caught an exception' + print ('Caught an exception') finally: - print 'In finally block for cleanup' + print ('In finally block for cleanup') #----------------------------------------# @@ -1432,10 +1438,10 @@ Use \w to match letters. Solution: import re -emailAddress = raw_input() +emailAddress = input() pat2 = "(\w+)@((\w+\.)+(com))" r2 = re.match(pat2,emailAddress) -print r2.group(1) +print(r2.group(1)) #----------------------------------------# @@ -1460,10 +1466,10 @@ Use \w to match letters. Solution: import re -emailAddress = raw_input() +emailAddress = input() pat2 = "(\w+)@(\w+)\.(com)" r2 = re.match(pat2,emailAddress) -print r2.group(2) +print (r2.group(2)) @@ -1490,8 +1496,8 @@ Use re.findall() to find all substring using regex. Solution: import re -s = raw_input() -print re.findall("\d+",s) +s = input() +print (re.findall("\d+",s)) #----------------------------------------# @@ -1507,7 +1513,7 @@ Use u'strings' format to define unicode string. Solution: unicodeString = u"hello world!" -print unicodeString +print(unicodeString) #----------------------------------------# Write a program to read an ASCII string and to convert it to a unicode string encoded by utf-8. @@ -1518,9 +1524,9 @@ Use unicode() function to convert. Solution: -s = raw_input() +s = input() u = unicode( s ,"utf-8") -print u +print (u) #----------------------------------------# Question: @@ -1554,11 +1560,11 @@ Use float() to convert an integer to a float Solution: -n=int(raw_input()) +n=int(input()) sum=0.0 for i in range(1,n+1): sum += float(float(i)/(i+1)) -print sum +print (round(sum,2)) #----------------------------------------# @@ -1593,8 +1599,8 @@ def f(n): else: return f(n-1)+100 -n=int(raw_input()) -print f(n) +n=int(input()) +print (f(n)) #----------------------------------------# @@ -1632,8 +1638,8 @@ def f(n): elif n == 1: return 1 else: return f(n-1)+f(n-2) -n=int(raw_input()) -print f(n) +n=int(input()) +print (f(n)) #----------------------------------------# @@ -1675,9 +1681,9 @@ def f(n): elif n == 1: return 1 else: return f(n-1)+f(n-2) -n=int(raw_input()) +n=int(input()) values = [str(f(x)) for x in range(0, n+1)] -print ",".join(values) +print (",".join(values)) #----------------------------------------# @@ -1715,7 +1721,7 @@ values = [] for i in EvenGenerator(n): values.append(str(i)) -print ",".join(values) +print (",".join(values)) #----------------------------------------# @@ -1750,7 +1756,7 @@ values = [] for i in NumGenerator(n): values.append(str(i)) -print ",".join(values) +print (",".join(values)) #----------------------------------------# @@ -1793,43 +1799,8 @@ Use eval() to evaluate an expression. Solution: -expression = raw_input() -print eval(expression) - - -#----------------------------------------# -Question: - -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. - - -Hints: -Use if/elif to deal with conditions. - - -Solution: - -import math -def bin_search(li, element): - bottom = 0 - top = len(li)-1 - index = -1 - while top>=bottom and index==-1: - mid = int(math.floor((top+bottom)/2.0)) - if li[mid]==element: - index = mid - elif li[mid]>element: - top = mid-1 - else: - bottom = mid+1 - - return index - -li=[2,5,7,9,11,17,222] -print bin_search(li,11) -print bin_search(li,12) - - +expression = input() +print (eval(expression)) #----------------------------------------# @@ -1861,11 +1832,8 @@ def bin_search(li, element): return index li=[2,5,7,9,11,17,222] -print bin_search(li,11) -print bin_search(li,12) - - - +print (bin_search(li,11)) +print (bin_search(li,12)) #----------------------------------------# Question: @@ -1881,7 +1849,7 @@ Use random.random() to generate a random float in [0,1]. Solution: import random -print random.random()*100 +print (random.random()*100) #----------------------------------------# Question: @@ -1889,7 +1857,6 @@ Question: Please generate a random float where the value is between 5 and 95 using Python math module. - Hints: Use random.random() to generate a random float in [0,1]. @@ -1897,7 +1864,7 @@ Use random.random() to generate a random float in [0,1]. Solution: import random -print random.random()*100-5 +print (random.random()*100-5) #----------------------------------------# @@ -1906,7 +1873,6 @@ Question: Please write a program to output a random even number between 0 and 10 inclusive using random module and list comprehension. - Hints: Use random.choice() to a random element from a list. @@ -1914,7 +1880,7 @@ Use random.choice() to a random element from a list. Solution: import random -print random.choice([i for i in range(11) if i%2==0]) +print (random.choice([i for i in range(11) if i%2==0])) #----------------------------------------# @@ -1931,7 +1897,7 @@ Use random.choice() to a random element from a list. Solution: import random -print random.choice([i for i in range(201) if i%5==0 and i%7==0]) +print (random.choice([i for i in range(201) if i%5==0 and i%7==0])) @@ -1950,7 +1916,7 @@ Use random.sample() to generate a list of random values. Solution: import random -print random.sample(range(100), 5) +print (random.sample(range(100), 5)) #----------------------------------------# Question: @@ -1966,7 +1932,7 @@ Use random.sample() to generate a list of random values. Solution: import random -print random.sample([i for i in range(100,201) if i%2==0], 5) +print (random.sample([i for i in range(100,201) if i%2==0], 5)) #----------------------------------------# @@ -1983,7 +1949,7 @@ Use random.sample() to generate a list of random values. Solution: import random -print random.sample([i for i in range(1,1001) if i%5==0 and i%7==0], 5) +print (random.sample([i for i in range(1,1001) if i%5==0 and i%7==0], 5)) #----------------------------------------# @@ -2000,7 +1966,7 @@ Use random.randrange() to a random integer in a given range. Solution: import random -print random.randrange(7,16) +print (random.randrange(7,16)) #----------------------------------------# @@ -2019,8 +1985,8 @@ Solution: import zlib s = 'hello world!hello world!hello world!hello world!' t = zlib.compress(s) -print t -print zlib.decompress(t) +print (t) +print (zlib.decompress(t)) #----------------------------------------# Question: @@ -2036,7 +2002,7 @@ Solution: from timeit import Timer t = Timer("for i in range(100):1+1") -print t.timeit() +print (t.timeit()) #----------------------------------------# Question: @@ -2053,7 +2019,7 @@ Solution: from random import shuffle li = [3,6,7,8] shuffle(li) -print li +print (li) #----------------------------------------# Question: @@ -2070,7 +2036,7 @@ Solution: from random import shuffle li = [3,6,7,8] shuffle(li) -print li +print (li) @@ -2091,7 +2057,7 @@ for i in range(len(subjects)): for j in range(len(verbs)): for k in range(len(objects)): sentence = "%s %s %s." % (subjects[i], verbs[j], objects[k]) - print sentence + print (sentence) #----------------------------------------# @@ -2104,7 +2070,7 @@ Solution: li = [5,6,77,45,22,12,24] li = [x for x in li if x%2!=0] -print li +print (li) #----------------------------------------# Question: @@ -2118,7 +2084,7 @@ Solution: li = [12,24,35,70,88,120,155] li = [x for x in li if x%5!=0 and x%7!=0] -print li +print (li) #----------------------------------------# @@ -2134,7 +2100,7 @@ Solution: li = [12,24,35,70,88,120,155] li = [x for (i,x) in enumerate(li) if i%2!=0] -print li +print (li) #----------------------------------------# @@ -2148,7 +2114,7 @@ Use list comprehension to make an array. Solution: array = [[ [0 for col in range(8)] for col in range(5)] for row in range(3)] -print array +print (array) #----------------------------------------# Question: @@ -2163,7 +2129,7 @@ Solution: li = [12,24,35,70,88,120,155] li = [x for (i,x) in enumerate(li) if i not in (0,4,5)] -print li +print (li) @@ -2180,7 +2146,7 @@ Solution: li = [12,24,35,24,88,120,155] li = [x for x in li if x!=24] -print li +print (li) #----------------------------------------# @@ -2197,7 +2163,7 @@ set1=set([1,3,6,78,35,55]) set2=set([12,24,35,24,88,120,155]) set1 &= set2 li=list(set1) -print li +print (li) #----------------------------------------# @@ -2216,10 +2182,10 @@ def removeDuplicate( li ): seen.add( item ) newli.append(item) - return newli + return (newli) li=[12,24,35,24,88,120,155,88,120,155] -print removeDuplicate(li) +print (removeDuplicate(li)) #----------------------------------------# @@ -2246,8 +2212,8 @@ class Female( Person ): aMale = Male() aFemale= Female() -print aMale.getGender() -print aFemale.getGender() +print (aMale.getGender()) +print (aFemale.getGender()) @@ -2278,10 +2244,10 @@ Use dict.get() method to lookup a key with default value. Solution: dic = {} -s=raw_input() +s=input() for s in s: dic[s] = dic.get(s,0)+1 -print '\n'.join(['%s,%s' % (k, v) for k, v in dic.items()]) +print ('\n'.join(['%s,%s' % (k, v) for k, v in dic.items()])) #----------------------------------------# @@ -2303,9 +2269,9 @@ Use list[::-1] to iterate a list in a reverse order. Solution: -s=raw_input() +s=input() s = s[::-1] -print s +print (s) #----------------------------------------# @@ -2327,9 +2293,9 @@ Use list[::2] to iterate a list by step 2. Solution: -s=raw_input() +s=input() s = s[::2] -print s +print (s) #----------------------------------------# @@ -2344,9 +2310,10 @@ Use itertools.permutations() to get permutations of list. Solution: import itertools -print list(itertools.permutations([1,2,3])) +print (list(itertools.permutations([1,2,3]))) #----------------------------------------# + Question: Write a program to solve a classic ancient Chinese puzzle: @@ -2368,8 +2335,7 @@ def solve(numheads,numlegs): numheads=35 numlegs=94 solutions=solve(numheads,numlegs) -print solutions +print (solutions) #----------------------------------------# - From efe78507c4663f36ca5043f9dffa74468bce86ee Mon Sep 17 00:00:00 2001 From: Kamrul Ahsan Date: Thu, 13 Feb 2020 16:06:08 +0600 Subject: [PATCH 3/9] Update 100+ Python challenging programming exercises.txt --- 100+ Python challenging programming exercises.txt | 2 ++ 1 file changed, 2 insertions(+) diff --git a/100+ Python challenging programming exercises.txt b/100+ Python challenging programming exercises.txt index b868ac29..cff080fd 100644 --- a/100+ Python challenging programming exercises.txt +++ b/100+ Python challenging programming exercises.txt @@ -324,6 +324,8 @@ for i in range(1000, 3001): if (int(s[0])%2==0) and (int(s[1])%2==0) and (int(s[2])%2==0) and (int(s[3])%2==0): values.append(s) print (",".join(values)) + +MY Solution: print(','.join([str(x) for x in range(1000,3001) if (x % 2 == 0)])) #----------------------------------------# #----------------------------------------# From cb030b67de5ff1004de2d120ef0207b3ae5a468d Mon Sep 17 00:00:00 2001 From: Kamrul Ahsan Date: Thu, 20 Feb 2020 16:47:40 +0600 Subject: [PATCH 4/9] Update 100+ Python challenging programming exercises.txt --- 100+ Python challenging programming exercises.txt | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/100+ Python challenging programming exercises.txt b/100+ Python challenging programming exercises.txt index cff080fd..c9dd96e5 100644 --- a/100+ Python challenging programming exercises.txt +++ b/100+ Python challenging programming exercises.txt @@ -465,6 +465,21 @@ while True: else: pass print (netAmount) + +My solution: +x = 0 +y = 0 +while True: + inp = input() + if inp == '': + break + if inp.split()[0] == 'D': + x += int(inp.split()[1]) + if inp.split()[0] == 'W': + y += int(inp.split()[1]) +print(x-y) + + #----------------------------------------# #----------------------------------------# From 59c0073687a601d00a8e4f505cf67f0e7d96a82e Mon Sep 17 00:00:00 2001 From: Kamrul Ahsan Date: Wed, 8 Apr 2020 18:34:40 +0600 Subject: [PATCH 5/9] Update 100+ Python challenging programming exercises.txt --- 100+ Python challenging programming exercises.txt | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/100+ Python challenging programming exercises.txt b/100+ Python challenging programming exercises.txt index c9dd96e5..a875152f 100644 --- a/100+ Python challenging programming exercises.txt +++ b/100+ Python challenging programming exercises.txt @@ -343,6 +343,19 @@ DIGITS 3 Hints: In case of input data being supplied to the question, it should be assumed to be a console input. +My solution: +digi = {'LETTERS': 0, 'DIGITS': 0} +np = 'hello world! 123' +count = 0 +for n in np: + if n.isdigit(): + digi['DIGITS'] += 1 + elif n.isalpha(): + digi['LETTERS'] += 1 +print('LETTERS ' + str(digi['LETTERS'])) +print('DIGITS ' + str(digi['DIGITS'])) + + Solution: s = input() d={"DIGITS":0, "LETTERS":0} From 437de71825daf73c6e6faa555da45e6a73d3b683 Mon Sep 17 00:00:00 2001 From: Kamrul Ahsan Date: Thu, 9 Apr 2020 20:06:32 +0600 Subject: [PATCH 6/9] Update 100+ Python challenging programming exercises.txt --- 100+ Python challenging programming exercises.txt | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/100+ Python challenging programming exercises.txt b/100+ Python challenging programming exercises.txt index a875152f..8f112ddf 100644 --- a/100+ Python challenging programming exercises.txt +++ b/100+ Python challenging programming exercises.txt @@ -385,6 +385,18 @@ LOWER CASE 9 Hints: In case of input data being supplied to the question, it should be assumed to be a console input. +My solution: +pq = {"UPPER CASE": 0, "LOWER CASE": 0} + +txt = 'Hello world!' +for p in txt: + if p.isupper(): + pq["UPPER CASE"] += 1; + elif p.islower(): + pq["LOWER CASE"] += 1; + +print(pq) + Solution: s = input() d={"UPPER CASE":0, "LOWER CASE":0} From d35e31e079fba7d04c8f8fdc15459e26b240bb41 Mon Sep 17 00:00:00 2001 From: Kamrul Ahsan Date: Sat, 6 Jun 2020 13:17:57 +0600 Subject: [PATCH 7/9] Update 100+ Python challenging programming exercises.txt --- 100+ Python challenging programming exercises.txt | 3 +++ 1 file changed, 3 insertions(+) diff --git a/100+ Python challenging programming exercises.txt b/100+ Python challenging programming exercises.txt index 8f112ddf..1b80e17a 100644 --- a/100+ Python challenging programming exercises.txt +++ b/100+ Python challenging programming exercises.txt @@ -59,6 +59,9 @@ def fact(x): x=int(input()) print(fact(x)) + +#Explanation: https://www.youtube.com/watch?v=zbfRgC3kukk + #----------------------------------------# #----------------------------------------# From 7d0681119ea445bcdd456d15a97b76a61759de98 Mon Sep 17 00:00:00 2001 From: "Abdul Hannan (Kamrul)" Date: Tue, 17 Aug 2021 08:49:18 +0600 Subject: [PATCH 8/9] Update 100+ Python challenging programming exercises for Python 3.md --- ...enging programming exercises for Python 3.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/100+ Python challenging programming exercises for Python 3.md b/100+ Python challenging programming exercises for Python 3.md index c4ba62c4..93f969df 100644 --- a/100+ Python challenging programming exercises for Python 3.md +++ b/100+ Python challenging programming exercises for Python 3.md @@ -176,6 +176,23 @@ for d in items: print(','.join(value)) ``` + +My solution: +``` +import math +d = input() + +d1 = d.split(',') + +lst = [] +for i in d1: + q = math.sqrt((2*50*int(i))/30) + lst.append(str(round(q))) + +print(','.join(lst)) +``` + + ### Question 7 Level 2 From 84c3b7467f38c05f30f3a943ab8e26233a1d0537 Mon Sep 17 00:00:00 2001 From: "Abdul Hannan (Kamrul)" Date: Thu, 26 Aug 2021 17:49:17 +0600 Subject: [PATCH 9/9] Update 100+ Python challenging programming exercises for Python 3.md --- ...thon challenging programming exercises for Python 3.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/100+ Python challenging programming exercises for Python 3.md b/100+ Python challenging programming exercises for Python 3.md index 93f969df..3a4e4ecf 100644 --- a/100+ Python challenging programming exercises for Python 3.md +++ b/100+ Python challenging programming exercises for Python 3.md @@ -272,6 +272,14 @@ for sentence in lines: print(sentence) ``` +My solution: +``` +inp = input() +ll = str(inp).split() +pq = [l.upper() for l in ll] +print(' '.join(pq)) +``` + ### Question 10 Level 2