Functions and Dictionaries Assignment
Functions and Dictionaries Assignment
[13]:
# Q1. Write a function called display menu that displays the menu to the user and allows the user to make a choice.
def displayMenu():
print('a. Message')
print('b. Contact')
print('c. Games')
if option == 'a':
message()
contact()
games()
else :
print('Incorrect choice')
def message():
print(name,':')
print(' ',text)
def contact():
phonedict = {}
if choice == 'a':
phonedict.update({name : number})
print(key,':',phonedict[key])
else :
if nam in phonedict:
del phonedict[nam]
else :
else :
print('Incorrect choice!')
def games():
print('Games trial version has been finished, buy now to play them!')
displayMenu()
a. Message
b. Contact
c. Games
Rahul :
In [20]:
# Q2. Purchasing someting online on the internet. Write a function to take price of the item and a boolean varialble indicating
# that you are a member and returns the final discounted value.
def cal():
if num:
member = True
else :
member = False
if member:
else :
cal()
In [3]:
# Q3. Write a function to shift the numbers circularly by some integer k(where k<n). The function should take a list and k as
def userInput():
factor = int(input('Enter the integer \'k\' to shift the elements (k < elements): '))
direc = input('Enter the direction in which to be shifted (r -> right/l -> left) : ')
shift(lst,factor,direc)
def shift(lst,factor,direc):
if direc == 'r':
userInput()
Original list : ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
shifted by 4 to the right: ['6', '7', '8', '9', '0', '1', '2', '3', '4', '5']
In [5]:
# Q4. Write a function for checking speed of drivers. This function should have one parameter : speed.
def func(speed):
print('OK')
else :
print('Points :',points)
print('License suspended!')
func(speed)
Points : 16
License suspended!
In [2]:
# Q5. Write a function that returns the sum of multiples of 3,5,7 between 0 and limit(parameter).
lst = []
def cal(limit):
sum = 0
for x in range(limit+1):
if (x % 3 == 0) or (x % 5 == 0) or (x % 7 == 0):
lst.append(x)
sum += x
return sum
print('Sum of : ',lst,'=',cal(limit))
In [5]:
# Q6. Write a function named right_justify.
def right_justify(s):
print(s.center(70))
right_justify(string)
string
In [7]:
# Q7. Write a function to check if a given number is a Curzon number.
def isCurzon(n):
if (2 ** n + 1) % (2 * n + 1) == 0:
return True
else:
return False
Enter a number : 5
In [2]:
# Q8. Write a function that takes an integer(radius of circle) and returns the difference of areas of the squares.
def difference(r):
bigSquareArea = (2*r)**2
smallSquareArea = 2*(r**2)
In [3]:
# Q9. Write a function to take no. of balls bowled and return how many overs have been completed.
def overs(balls):
overs(balls)
In [5]:
# Q10. Financial institution provides professional services to banks.
def billableDays(days):
bonus = 32 * 0
else:
return bonus
In [2]:
# Q11. Take a paragraph from user. Calculate frequency of vowels store it in dictionary.
def userInput():
lines = []
while True:
sen = input()
lines.append(sen)
if sen == '.':
break
for x in lines:
vowelDic['a'] += x.count('a')
vowelDic['e'] += x.count('e')
vowelDic['i'] += x.count('i')
vowelDic['o'] += x.count('o')
vowelDic['u'] += x.count('u')
print(vowelDic)
userInput()
Enter a paragraph :
In [4]:
# Q12. Take numbers from user until he says 'over'.If number is odd add it in dictionary 'Odd' else add in 'Even'.
def evenOdd():
even = {}
odd = {}
while True:
if n == 'over':
break
elif int(n) % 2 == 0:
lst = values(int(n))
even.update({n:lst})
elif int(n) % 2 != 0:
lst = values(int(n))
odd.update({n:lst})
else :
print('Invalid input!')
def values(n):
lst = []
lst.append(n*n)
lst.append(n*n*n)
return lst
evenOdd()
Even dicionary :-
Odd dictionary :-
In [3]:
# Q13. Given a list of numbers create a new list such that first and last numbers are added and stored as first number and so
def construct(lst):
l = len(lst)
newLst = []
if l % 2 == 0:
for x in range(l):
newLst.append(lst[x] + lst[l-(x+1)])
else :
for x in range(l):
if x == (l//2):
newLst.append(lst[x])
else:
newLst.append(lst[x] + lst[l-(x+1)])
lst = list(map(int,input().split()))
construct(lst)
1 2 3 4 5
In [10]:
# Q14. The letters a,b,d,e,g,o,p and q all have something in common.
def countInLowerCase(sen):
withHoles = 0
withoutHoles = 0
for x in 'abdegopq':
withHoles += sen.count(x)
for x in 'cfghijklmnrstuvwxyz':
withoutHoles += sen.count(x)
def countInUpperCase(sen):
withHoles = 0
withoutHoles = 0
for x in 'ABDOPQR':
withHoles += sen.count(x)
for x in 'CEFGHIJKLMNSTUVWYZ':
withoutHoles += sen.count(x)
def wordList(sen):
print()
count = 0
for y in 'abdegopqABDOPQR':
if x.count(y) == 2:
print(x)
break
elif y in x:
count += 1
if count == 2:
print(x)
break
countInLowerCase(sen.lower())
countInUpperCase(sen.upper())
wordList(sen)
are
buddy
In [3]:
# Q15. Sally invited 17 guests to a dance party. She assigned each guest a number from 2 to 18, keeping 1 for herself.
def combinations(n):
mainlst = []
finallst = []
numlst = []
perfectSq = [4,9,16,25]
for x in range(1,n+1):
for y in range(1,n+1):
numlst.append((x,y))
mainlst.append(numlst)
numlst = []
print(mainlst)
combinations(18)
[[(1, 3), (1, 8), (1, 15)], [(2, 7), (2, 14)], [(3, 1), (3, 6), (3, 13)], [(4, 5), (4, 12)], [(5, 4), (5, 11)], [(6, 3), (6, 10)], [(7, 2), (7, 9), (7, 18)], [(8, 1), (8, 17)],
[(9, 7), (9, 16)], [(10, 6), (10, 15)], [(11, 5), (11, 14)], [(12, 4), (12, 13)], [(13, 3), (13, 12)], [(14, 2), (14, 11)], [(15, 1), (15, 10)], [(16, 9)], [(17, 8)], [(18, 7)]]
In [12]:
# Q16. Using int operatiors '+','-','*','/','**' and numbers 2,3,4,5 find an expression which evaluates to 26.
operatorLst = ['+','-','*','/','**']
numLst = ['2','3','4','5']
def combine(oplst,nmlst):
opcomb = list(combinations(oplst,3))
nmcomb = list(permutations(nmlst))
inner = True
for x in nmcomb:
for y in opcomb:
ans = eval(string)
if ans == 26:
inner = False
break
if inner == False:
break
combine(operatorLst,numLst)