
There are Three Level in This Challenging Program. Beginners, Intermidate and Advanced, Before Reading this post, I recommend you View Python Challenging Programming Exercises Part 1 for better Understanding.
There are Three Level in This Challenging Program. Beginners, Intermidate and Advanced, Before Reading this post, I recommend you View Python Challenging Programming Exercises Part 1 for better Understanding.
Hints: Consider use yield
Solution:
def putNumbers(n):
i = 0
while i
Hints In case of input data being supplied to the question, it should be assumed to be a console input.
Solution:
freq = {} # frequency of words in text
line = input()
for word in line.split():
freq[word] = freq.get(word,0)+1
words = freq.keys()
for w in words:
print("%s:%d" % (w,freq[w]))
Hints: Using the ** operator
Solution:
def square(num):
return num ** 2
print(square(2))
print(square(3))
Hints: The built-in document method is __doc__
Solution:
print(abs.__doc__)
print(int.__doc__)
print(input.__doc__)
def square(num):
'''Return the square value of the input number.
The input number must be integer.
'''
return num ** 2
print(square(2))
print(square.__doc__)
Hints: Define a instance parameter, need add it in __init__ method You can init a object with construct parameter or set the value later
Solution:
class Person:
# Define the class parameter "name"
name = "Person"
def __init__(self, name = None):
# self.name is the instance parameter
self.name = name
vivek = Person("Vivek")
print("%s name is %s" % (Person.name, vivek.name))
aayush = Person()
aayush.name = "Aayush"
print("%s name is %s" % (Person.name, aayush.name))
Hints: Define a function with two numbers as arguments. You can compute the sum in the function and return the value.
Solution:
def SumFunction(number1, number2):
return number1+number2
print(SumFunction(1,2))
Hints: Use str() to convert a number to string.
Solution:
def printValue(n):
print(str(n))
printValue(3)
Hints: Use int() to convert a string to integer.
Solution:
def printValue(s1,s2):
print(int(s1)+int(s2))
printValue("3","4")
Hints: Use + to concatenate the strings
Solution:
def printValue(s1,s2):
print(s1+s2)
printValue("3","4")Hints: Use len() function to get the length of a string
Solution:
def printValue(s1,s2):
len1 = len(s1)
len2 = len(s2)
if len1>len2:
print(s1)
elif len2>len1:
print(s2)
else:
print(s1)
print(s2)
printValue("one","three")Hints: 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")
else:
print("It is an odd number")
checkValue(int(input()))
Hints: Use dict[key]=value pattern to put entry into a dictionary.
Use ** operator to get power of a number.
Solution:
def printDict():
d=dict()
d[1]=1
d[2]=2**2
d[3]=3**2
print(d)
printDict()
Hints: Use dict[key]=value pattern to put entry into a dictionary.
Use ** operator to get power of a number.
Use range() for loops.
Solution:
def printDict():
d=dict()
for i in range(1,21):
d[i]=i**2
print(d)
printDict()
Hints: Use dict[key]=value pattern to put entry into a dictionary.
Use ** operator to get power of a number.
Use range() for loops.
Use keys() to iterate keys in the dictionary. Also we can use item() to get key/value pairs.
Solution:
def printDict():
d=dict()
for i in range(1,21):
d[i]=i**2
for (k,v) in d.items():
print(v)
printDict()
Hints: Use dict[key]=value pattern to put entry into a dictionary.
Use ** operator to get power of a number.
Use range() for loops.
Use keys() to iterate keys in the dictionary. Also we can use item() to get key/value pairs.
Solution:
def printDict():
d=dict()
for i in range(1,21):
d[i]=i**2
for k in d.keys():
print(k)
printDict()
Thanks for Reading Share this Post
Sign in to join the discussion and post comments.
Sign in