diff --git a/100+ Python challenging programming exercises for Python 3.md b/100+ Python challenging programming exercises for Python 3.md index c4ba62c4..3a4e4ecf 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 @@ -255,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 diff --git a/100+ Python challenging programming exercises.txt b/100+ Python challenging programming exercises.txt index 97af5aaf..1b80e17a 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,11 @@ def fact(x): return 1 return x * fact(x - 1) -x=int(raw_input()) -print fact(x) +x=int(input()) +print(fact(x)) + +#Explanation: https://www.youtube.com/watch?v=zbfRgC3kukk + #----------------------------------------# #----------------------------------------# @@ -77,12 +80,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 +105,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 +127,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,19 +161,28 @@ 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 Level 2 @@ -188,7 +200,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 +210,7 @@ for row in range(rowNum): for col in range(colNum): multilist[row][col]= row*col -print multilist +print (multilist) #----------------------------------------# #----------------------------------------# @@ -216,9 +228,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 +252,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 +278,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 +300,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 +326,9 @@ 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)) + +MY Solution: print(','.join([str(x) for x in range(1000,3001) if (x % 2 == 0)])) #----------------------------------------# #----------------------------------------# @@ -332,8 +346,21 @@ 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 = raw_input() +s = input() d={"DIGITS":0, "LETTERS":0} for c in s: if c.isdigit(): @@ -342,8 +369,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"]) #----------------------------------------# #----------------------------------------# @@ -361,8 +388,20 @@ 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 = raw_input() +s = input() d={"UPPER CASE":0, "LOWER CASE":0} for c in s: if c.isupper(): @@ -371,8 +410,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 +429,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 +452,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 +480,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 +492,22 @@ while True: netAmount-=amount else: pass -print netAmount +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) + + #----------------------------------------# #----------------------------------------# @@ -501,7 +555,7 @@ for p in items: else: pass value.append(p) -print ",".join(value) +print (",".join(value)) #----------------------------------------# #----------------------------------------# @@ -532,12 +586,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 +614,7 @@ def putNumbers(n): yield j for i in reverse(100): - print i + print (i) #----------------------------------------# #----------------------------------------# @@ -608,7 +662,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 +691,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 +699,7 @@ words = freq.keys() words.sort() for w in words: - print "%s:%d" % (w,freq[w]) + print ("%s:%d" % (w,freq[w])) #----------------------------------------# #----------------------------------------# @@ -662,8 +716,8 @@ Solution: def square(num): return num ** 2 -print square(2) -print square(3) +print (square(2)) +print (square(3)) #----------------------------------------# #----------------------------------------# @@ -679,9 +733,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 +744,8 @@ def square(num): ''' return num ** 2 -print square(2) -print square.__doc__ +print (square(2)) +print (square.__doc__) #----------------------------------------# #----------------------------------------# @@ -715,11 +769,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 +868,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 +893,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 +918,7 @@ def printDict(): d[1]=1 d[2]=2**2 d[3]=3**2 - print d + print(d) printDict() @@ -890,7 +944,7 @@ def printDict(): d=dict() for i in range(1,21): d[i]=i**2 - print d + print (d) printDict() @@ -915,7 +969,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 +993,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 +1015,8 @@ def printList(): li=list() for i in range(1,21): li.append(i**2) - print li + print (li) - printList() #----------------------------------------# @@ -985,7 +1037,7 @@ def printList(): li=list() for i in range(1,21): li.append(i**2) - print li[:5] + print (li[:5]) printList() @@ -1009,8 +1061,7 @@ def printList(): li=list() for i in range(1,21): li.append(i**2) - print li[-5:] - + print (li[-5:]) printList() @@ -1033,9 +1084,8 @@ def printList(): li=list() for i in range(1,21): li.append(i**2) - print li[5:] + print (li[5:]) - printList() @@ -1057,7 +1107,7 @@ def printTuple(): li=list() for i in range(1,21): li.append(i**2) - print tuple(li) + print (tuple(li)) printTuple() @@ -1077,8 +1127,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 +1150,7 @@ for i in tp: li.append(tp[i]) tp2=tuple(li) -print tp2 +print (tp2) @@ -1117,9 +1167,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 +1187,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 +1204,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 +1221,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 +1239,7 @@ Use lambda to define anonymous functions. Solution evenNumbers = filter(lambda x: x%2==0, range(1,21)) -print evenNumbers +print (evenNumbers) #----------------------------------------# @@ -1205,7 +1255,7 @@ Use lambda to define anonymous functions. Solution squaredNumbers = map(lambda x: x**2, range(1,21)) -print squaredNumbers +print (squaredNumbers) @@ -1224,7 +1274,7 @@ Solution class American(object): @staticmethod def printNationality(): - print "America" + print ("America") anAmerican = American() anAmerican.printNationality() @@ -1254,8 +1304,8 @@ class NewYorker(American): anAmerican = American() aNewYorker = NewYorker() -print anAmerican -print aNewYorker +print (anAmerican) +print (aNewYorker) @@ -1280,9 +1330,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 +1358,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 +1392,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 +1433,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 +1483,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 +1511,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 +1541,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 +1558,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 +1569,9 @@ Use unicode() function to convert. Solution: -s = raw_input() +s = input() u = unicode( s ,"utf-8") -print u +print (u) #----------------------------------------# Question: @@ -1554,11 +1605,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 +1644,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 +1683,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 +1726,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 +1766,7 @@ values = [] for i in EvenGenerator(n): values.append(str(i)) -print ",".join(values) +print (",".join(values)) #----------------------------------------# @@ -1750,7 +1801,7 @@ values = [] for i in NumGenerator(n): values.append(str(i)) -print ",".join(values) +print (",".join(values)) #----------------------------------------# @@ -1793,8 +1844,8 @@ Use eval() to evaluate an expression. Solution: -expression = raw_input() -print eval(expression) +expression = input() +print (eval(expression)) #----------------------------------------# @@ -1826,46 +1877,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) - - - - -#----------------------------------------# -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) - - - +print (bin_search(li,11)) +print (bin_search(li,12)) #----------------------------------------# Question: @@ -1881,7 +1894,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 +1902,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 +1909,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 +1918,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 +1925,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 +1942,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 +1961,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 +1977,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 +1994,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 +2011,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 +2030,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 +2047,7 @@ Solution: from timeit import Timer t = Timer("for i in range(100):1+1") -print t.timeit() +print (t.timeit()) #----------------------------------------# Question: @@ -2053,7 +2064,7 @@ Solution: from random import shuffle li = [3,6,7,8] shuffle(li) -print li +print (li) #----------------------------------------# Question: @@ -2070,7 +2081,7 @@ Solution: from random import shuffle li = [3,6,7,8] shuffle(li) -print li +print (li) @@ -2091,7 +2102,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 +2115,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 +2129,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 +2145,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 +2159,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 +2174,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 +2191,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 +2208,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 +2227,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 +2257,8 @@ class Female( Person ): aMale = Male() aFemale= Female() -print aMale.getGender() -print aFemale.getGender() +print (aMale.getGender()) +print (aFemale.getGender()) @@ -2278,10 +2289,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 +2314,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 +2338,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 +2355,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 +2380,7 @@ def solve(numheads,numlegs): numheads=35 numlegs=94 solutions=solve(numheads,numlegs) -print solutions +print (solutions) #----------------------------------------# -