Assignment 8
Assignment 8
#Solution 1
t=(4,7,9,10,2)
pro=1
for i in t:
pro=pro*i
print(pro)
#Solution 2
t1=(3,5,7,9,11)
t2=(4,6,7,9,12,14,7)
t3=(t1+t2)
l1=list(t3)
for i in l1:
if l1.count(i)>1:
for j in range(l1.count(i)-1):
l1.remove(i)
t3=tuple(l1)
print(t3)
#Q 3 and 4 Write a python function that accepts a variable number of arguments and returns a tuple
containing the sum and product of all the arguments.
#Solution 3 and 4
def func(n):
sum=0
product=1
for i in range(n):
sum+=a
product*=a
t=(sum,product)
return t
x=func(n)
print(x)
#Q 5 Write a python program to zip two lists into a list of tuples and then unzip the tuple back into
separate lists.
#Solution 5
l1=[4,6,8,11,19]
l2=[5,7,13,15,29]
l3=[]
for i in zip(l1,l2):
l3.append(i)
print(l3)
l4,l5=zip(*l3)
print(list(l4))
print(list(l5))
#Q 6 write a program to sort tuple of tuples by second item.
#Solution 6
t=((2,3,4),(5,4,3,1),(2,1),(9,8,3,2))
a=t[0][1]
b=t[1][1]
c=t[2][1]
d=t[3][1]
l=[a,b,c,d]
l.sort()
l[l.index(a)]=t[0]
l[l.index(b)]=t[1]
l[l.index(c)]=t[2]
l[l.index(d)]=t[3]
t=tuple(l)
print(t)
#Q 7 Make a taple of list called sandwich_orders and fill it with the names of various sandwiches.
Then make an empty list called finished sandwiches. Loop through the list of sandwich orders and
print a message for each order, such as I made your turn sandwich. As each sandwich is made, move
it to the list of finished sandwiches. After all the sandwiches have been made, print a message listing
each sandwich that was made.
#Solution 7
sandwich_orders=(["tuna","club","tortas","grilled cheese","bacon"])
finished_sandwiches=[]
for i in sandwich_orders:
#Solution 8
print(l)
#Q 9 Write a program to convert a tuple of integers to tuple of strings using list comprehension.
#Solution 9
t=(4,6,9,11,10,89)
l=list(t)
l1=[str(i) for i in l]
t=tuple(l1)
print(t)
#Q 10 Use a nested list comprehension to find all the numbers from 1-100 that are divisible by any
single digit beside 1(2-9).
#Solution 10
print(l)