Uploaded by Tanishq Mishra

201B027 AkashSingh AP lab4

advertisement
NAME
: AKASH SINGH
ENROLL NO : 2 0 1 B 0 2 7
BATCH
: BX1 ( B1 )
Department of Computer Science and Engineering
Jaypee University of Engineering and Technology
18B17CI373 – Advanced Programming Lab-I
Lab Exercise 4
1. Write the output corresponding to the following statements. Assume that the statements
are executed in the order in which they are written.
a. tup1 = (5,10,15,20,25)
b. print(len(tup1))
c. print(tup1[4])
d. print(tup1[5])
e. print(tup1[4:5])
f. tup1[2] = 12
g. print(tup1)
h. tup1=tup1+(8,9)
Solution 1 :
a. >>> tup1= (5,10,15,20,25)
b. >>> print(len(tupl))
5
c. >>> print(tup1[4])
25
d.>>> print(tupl[5])
error : IndexError: tuple index out of range
e. >>> print(tup1[4:5])
(25,)
f. >>> tup1[2] = 12
error : TypeError: 'tuple' object does not support item assignment
g. >>> print(tup1)
(5, 10, 15, 20, 25)
h. >>> tup1 = tup1 +(8,9)
>>> print(tup1)
(5, 10, 15, 20, 25, 8, 9)
__________________________________________________
2. Pure Gems Store sells different varieties of gems to its customers. Emerald, Ivory, Jasper,
Ruby, Garnet and their prices are 1760, 2119, 1599, 3920, 3999 respectively.
Write a Python program to calculate the bill amount to be paid by a customer based on the
list of gems and quantity purchased. Any purchase with a total bill amount above Rs.30000
is entitled for 5% discount. If any gem required by the customer is not available in the store,
then consider total bill amount to be -1.
Assume that quantity required by the customer for any gem will always be greater than 0.
Perform case-sensitive comparison wherever applicable.
Code : 2
price = (1760, 2119, 1599, 3920, 3999)
gems_price = ("Emerald", "Ivory", "Jasper", "Ruby", "Garnet")
choice_gem = input("enter the gems : ").split(',')
total_bill = 0
check = False
check = all(item in gems_price for item in choice_gem)
if check == False:
total_bill = -1
print(f"total bill amount : {total_bill}")
exit()
else:
for i in choice_gem:
quantity = int(input(f"enter the quantity of {i} : "))
total_bill = total_bill + price[gems_price.index(i)]*quantity
if total_bill > 30000:
total_bill = total_bill - total_bill/20
print(f"total bill amount : {total_bill}")
Output :
__________________________________________________
3. Write a python function to check whether three given numbers can form the sides of a
triangle.
Hint: Three numbers can be the sides of a triangle if none of the number is greater than or
equal to the sum of the other two numbers.
Code : 3
sides = input("enter the three sides seperated by ',' : ").split(',')
total = 0
del sides[3:len(sides)]
for i in range(0,3) :
sides[i] = int(sides[i])
total = total + sides[i]
if max(sides) <= (total - max(sides)) :
print(f"{sides[0]} , {sides[1]} , and {sides[2]} can form triangle .")
else:
print(f"{sides[0]} , {sides[1]} , and {sides[2]} can not form triangle .")
Output :
__________________________________________________
4. Write a python program to solve a classic ancient Chinese puzzle.
If there are 35 heads and 94 legs among the chickens and rabbits in a farm. How many
rabbits and how many chickens do we have?
Sample Input
Expected Output
heads-150 legs-400
100
heads-3 legs-11
No solution
heads-3 legs-12
0
3
heads-5 legs-10
5
0
50
Code : 4
heads, legs = map(int, input(
"enter heads and legs seperated by ', ' : ").split(','))
rabbit, chicks, = 0, 0
if (legs % 2 == 0 and heads<=legs/2):
rabbit = legs - 2*heads
rabbit = rabbit//2
chicks = heads - rabbit
print(f"chicks = {chicks} and rabbits = {rabbit}")
else:
print("No solution")
Output :
__________________________________________________
5. https://practice.geeksforgeeks.org/problems/magical-string3653/1/?difficulty[]=1&page=1&sortBy=newest&query=difficulty[]-1page1sortBynewest (Write and submit the
python program.)
Code : 5
name = input("Enter the string : ")
magical_name = list(name)
for i in range(0, len(magical_name)):
if magical_name[i] == " ":
continue
else:
magical_name[i] = chr(219-ord(magical_name[i]))
magical_name = "".join(magical_name)
print(f"magical string of \"{name}\" will be \"{magical_name}\"")
Output :
__________________________________________________
6. https://www.codechef.com/problems/TRAVELPS (Write and submit the python program.)
Code : 6
test_case = int(input())
while test_case != 0:
n, a, b = map(int, input().split())
s = input()
one, zero = 0, 0
for i in range(0, n):
if s[i] == "0":
zero += 1
elif s[i] == "1":
one += 1
print(zero*a + one*b)
test_case -= 1
Output :
__________________________________________________
_______________THE END________________
__________________________________________________
Download