Lab Task 10: Programming Exercises
Lab Task 10: Programming Exercises
Lab Task 10: Programming Exercises
PROGRAMMING EXERCISES:
Question 1.
Write a program which will add your best five students name in a set. You will use a loop to
insert names in set.
Program:
best_stds=set({})
for i in range(5):
best_stds.add(input("Enter Name of your best student: "))
print("The set of best students is:",best_stds)
Result:
Enter Name of your best student: farzeen
Enter Name of your best student: maha
Enter Name of your best student: darakhshan
Enter Name of your best student: maria
Enter Name of your best student: izma
The set of best students is: {'maria', 'maha', 'farzeen', 'izma', 'darakhshan'}
Question 2
Write a program which will remove 2 friends who left NED.
Program:
stds={"farzeen","maha","maria","maryam","darakhshan","izma","fizza","ali"}
stds_left={"maryam","ali"}
new=stds.difference(stds_left)
print("The students who joined NED",stds)
print("The students who left NED",stds_left)
print("The students who are now in NED",new)
Result:
The students who joined NED {'darakhshan', 'fizza', 'maha', 'maryam', 'maria',
'farzeen', 'ali', 'izma'}
The students who left NED {'ali', 'maryam'}
The students who are now in NED {'darakhshan', 'fizza', 'maha', 'maria', 'farzeen',
'izma'}
Question 3.
Write a program which will add your best dishes and then pop one by one until the set is empty.
Program:
best_dishes={"biryani","pizza","chowmein","burger","nihari"}
print(best_dishes)
for i in range(len(best_dishes)):
best_dishes.pop()
print(best_dishes)
Result:
{'pizza', 'nihari', 'burger', 'chowmein', 'biryani'}
{'nihari', 'burger', 'chowmein', 'biryani'}
{'burger', 'chowmein', 'biryani'}
{'chowmein', 'biryani'}
{'biryani'}
set()
Question 4.
Write a program which will store number of items in a set after each purchasing the items will
be pop from the set and compare its price at the end program will give you the total amount of
items have been sold. Also find the max amount and minimum amount of items sold.
Program:
items={"shirt","pasta","biryani","iron","cold drink"}
d1={}
price_list=[]
for i in range(len(items)):
x=items.pop()
print("Enter price for",x)
d1[x]=int(input())
amount=0
for j in d1.values():
price_list.append(j)
amount+=j
print("*********BILL**********")
print("{:<15}{:<15}".format("ITEMS","PRICE"))
for a,b in d1.items():
print("{:<15}{:<15}".format(a,b))
print("Total Amount= Rs.",amount)
print("Maximun Price=",max(price_list))
print("Minimum Price=",min(price_list))
print("THANKS FOR VISITING")
Result:
Enter price for pasta
250
Enter price for iron
1500
Enter price for cold drink
100
Enter price for biryani
200
Enter price for shirt
1000
*********BILL**********
ITEMS PRICE
pasta 250
iron 1500
cold drink 100
biryani 200
shirt 1000
Total Amount= Rs. 3050
Maximun Price= 1500
Minimum Price= 100
THANKS FOR VISITING
Question 5.
Write a program which will compare two sets, Set A and Set B. Both the sets have some students who
love to play one is hockey and other one is cricket. 10 of them play both. Now using sets find how many
of them are playing cricket only, if universal set is 40, students who play hockey are 21.
Program:
universal=set(range(1,41))
h=set(range(1,22))
both=set(range(1,11))
oc=set(universal.difference(h))
print("Total players =",len(universal))
print("Players who play only hockey =",len(h.difference(both)))
print("Players who play only cricket =",len(oc))
print("Players who play both =",len(both))
Result:
Total players = 40
Players who play only hockey = 11
Players who play only cricket = 19
Players who play both = 10
Question 6:
A pet store keeps track of the purchases of customers over a four-hour period. The store manager
classifies purchases as containing a dog product, a cat product, a fish product, or product for a
different kind of pet. She found.
a. 83 purchased a dog product
b. 101 purchased a cat product
c. 30 purchased a fish product
d. 31 purchased a dog and a cat product
e. 8 purchased a dog and a fish product
f. 10 purchased a cat and a fish product
g. 6 purchased a dog, a cat and a fish product
h. 34 purchased a product for a pet other than a dog, cat or a fish.
i. How many purchases were for a dog product only? Dog Cat
ii. How many purchases were for cat product only?
iii. How many purchases for a dog or a fish product?
iv. How many purchases were there in total?
Fish
Program:
dog=set(range(1,84))
cat=set(range(47,148))
fish=set(range(72,78))|(set(range(138,154)))|set(range(1,9))
other=set(range(155,189))
onlydog=dog-(cat|fish)
onlycat=cat-(dog|fish)
onlyfish=fish-(dog|cat)
total=dog|fish|cat|other
onlycatndog=(cat&dog)-fish
onlycatnfish=(cat&fish)-dog
onlydognfish=(dog&fish)-cat
allthree=cat&fish&dog
print("{:^40}".format("THE GIVEN DATA"))
data={"dog product":len(dog),"cat product":len(cat),"fish product":len(fish),
"a cat and a dog product":len(onlycatndog),"a dog and a fish
product":len(onlydognfish),
"a cat and a fish product":len(onlycatnfish),"a cat, a fish and a dog
product":len(allthree),
"a product other than a cat, dog and a fish product":len(other)}
for i,j in data.items():
print("People who purchased",i,"=",j)
print("{:^40}".format("ANSWERS"))
print("People who purshased only dog product:",len(onlydog))
print("People who purshased only cat product:",len(onlycat))
print("People who purshased a dog or a fish product:",len(onlyfish|onlydog))
print("Total purchases=",len(total))
Result:
THE GIVEN DATA
People who
purchased dog product = 83
People who
purchased cat product = 101
People who
purchased fish product = 30
People who
purchased a cat and a dog product = 31
People who
purchased a dog and a fish product = 8
People who
purchased a cat and a fish product = 10
People who
purchased a cat, a fish and a dog product = 6
People who
purchased a product other than a cat, dog and a fish product = 34
ANSWERS
People who purshased only dog product: 38
People who purshased only cat product: 54
People who purshased a dog or a fish product: 44
Total purchases= 187
Question 7:
Solve the following problem of real world.
A camp of international students has 110 students, as shown in the diagram. The diagram will
elaborate that all the students speak some kind of a language. We need to find out how many that
speak none of them out of 110 students.
Find how many students speak
a. English and Spanish but not French?
b. Neither English, Spanish, nor French?
c. French, but neither English nor Spanish?
d. Only one of the three languages?
e. Exactly two of the three languages?
Program:
students=set(range(1,111))
eng=set(range(1,76))
span=set(range(1,14))|set(range(56,95))
fren=set(range(86,106))|set(range(1,14))|set(range(14,31))
onlyengnspan=(eng&span)-fren
onlyengnfren=(eng&fren)-span
onlyfrennspan=(fren&span)-eng
neither=students-(eng|span|fren)
allthree=eng&fren&span
onlyfren=fren-(eng|span)
onlyspan=span-(eng|fren)
onlyeng=eng-(span|fren)
print("{:^40}".format("GIVEN DATA"))
data={"only English":len(onlyeng),"only Spanish":len(onlyspan),"only
French":len(onlyfren),
"only English and Spanish":len(onlyengnspan),"only English and
French":len(onlyengnfren),
"only French and Spanish":len(onlyfrennspan),"all English, Spanish and
French":len(allthree)}
print("Total Students =",len(students))
for i,j in data.items():
print("Students who speak",i,"=",j)
print("{:^40}".format("ANSWERS"))
print("Students who speak English and Spanish but not French:",len(onlyengnspan))
print("Students who speak neither English, Spanish nor French:",len(neither))
print("Students who speak French, but neither English nor Spanish:",len(onlyfren))
print("Students who speak only one of the three
languages:",len(onlyfren|onlyeng|onlyspan))
print("Students who speak exactly two of the three
languages:",len((onlyengnfren|onlyfrennspan|onlyengnspan)-allthree))
Result:
GIVEN DATA
Total Students = 110
Students who speak only English = 25
Students who speak only Spanish = 10
Students who speak only French = 11
Students who speak only English and Spanish = 20
Students who speak only English and French = 17
Students who speak only French and Spanish = 9
Students who speak all English, Spanish and French = 13
ANSWERS
Students who speak English and Spanish but not French: 20
Students who speak neither English, Spanish nor French: 5
Students who speak French, but neither English nor Spanish: 11
Students who speak only one of the three languages: 46
Students who speak exactly two of the three languages: 46