Professional Documents
Culture Documents
Akash Kalita 11802733 Ca2
Akash Kalita 11802733 Ca2
Akash Kalita 11802733 Ca2
OUTPUT-:
Q2. Write a program that computes the net amount of a bank account based
on a transaction. The transaction format is shown as following: D 100 W 200
Here, D means deposit while W means withdrawal. Take the following input in
the form of dictionary: D 300 D 300 W 200 D 100
{"D":300,"D":300,"W":200,"D":100} Then, the output should be: 500
Solution -:
def amount(trans):#This function amount to the trans passed in as a parameter
aamount=0
for i in trans:#For loop for checking debit and withdrawal
if(i[0]=='D'):
aamount = aamount+int(i[2::])
elif(i[0]=='W'):
aamount = aamount-int(i[2::])
return aamount
trans=["D:300","D:300","W:200","D:100"] # dictionary of data
print("amount-:",amount(trans))# return amount
OUTPUT-:
Q3. You are required to write a program to sort the (name, age, height) tuples
by ascending order where name is string, age and height are numbers. Take
inputs from user by a sequence of comma-separated. Then ask options for sort
to follow the following criteria:
1: Sort based on name;
2: Then sort based on age;
3: Then sort by height.
Solution-:
print("Enter number of persons")
t=[]
for w in range(int(input())): #for loop for asking for number of person
print("Enter name,age & height separated by comma")
t.append(tuple(x for x in input().split(',')))#entering data seperated by
comma
print("Enter sort type")
print("1: Sort based on name")
print("2: Sort based on age")
print("3: Sort by height")
choice=int(input()) #switch case for asking condition
if choice==1:
t=sorted(t,key=lambda s: (s[0])) #lambda is used to create anonymous
function
elif choice==2:
t=sorted(t,key=lambda s: (s[1]))
elif choice==3:
t=sorted(t,key=lambda s: (s[2]))
print(t)
OUTPUT-:
BY NAME
BY AGE
BY HEIGHT