Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

SWARAJ PANDEY

21162121036
BDA
SEM 4

Practical 5

1. (Simulation: coupon collector’s problem) Coupon Collector is a classic statistics problem with
many practical applications. The problem is to pick objects from a set of objects repeatedly and find
out how many picks are needed for all the objects to be picked at least once. A variation of the
problem is to pick cards from a shuffled deck of 52 cards repeatedly and find out how many picks are
needed before you see one of each suit. Assume a picked card is placed back in the deck before
picking another. Write a program to simulate the number of picks needed to get four cards, one
from each suit and display the four cards picked (it is possible a card may be picked twice). Here is a
sample run of the program:
Queen of Spades
5 of Clubs
Queen of Hearts
4 of Diamonds
Number of picks: 12

CODE:-
import random
j=0
C=False
S=False
H=False
D=False
c_picked=[0]*4
each_of_all_suit=[]
c_rank=['A','K','Q','J','2','3','4','5','6','7','8','9','10']
c_suit=['Heart','Club','Diamond','Spade']
while True:
r = random.randint(0,52)
if r//13 ==0:
j=j+1
C=True
t=r%13
c_picked[0]=r
if C==True and D==True and H==True and S==True:
break
else:
continue
elif r//13 ==1:
j=j+1
D=True
t=r%13
c_picked[1]=r
if C==True and D==True and H==True and S==True:
break
else:
continue
SWARAJ PANDEY
21162121036
BDA
SEM 4

elif r//13 ==2:


j=j+1
H=True
t=r%13
c_picked[2]=r
if C==True and D==True and H==True and S==True:
break
else:
continue
elif r//13 ==3:
j=j+1
S=True
t=r%13
c_picked[3]=r
if C==True and D==True and H==True and S==True:
break
else:
continue
print(c_picked)
t=c_picked[0]%13
each_of_all_suit.append(c_rank[t]+" of "+c_suit[c_picked[0]//13])
t=c_picked[1]%13
each_of_all_suit.append(c_rank[t]+" of "+c_suit[c_picked[1]//13])
t=c_picked[2]%13
each_of_all_suit.append(c_rank[t]+" of "+c_suit[c_picked[2]//13])
t=c_picked[3]%13
each_of_all_suit.append(c_rank[t]+" of "+c_suit[c_picked[3]//13])
print(each_of_all_suit)
print("No of Picks: ",j)

OUTPUT:-
SWARAJ PANDEY
21162121036
BDA
SEM 4

2. A scientist is implementing an algorithm that passes through the list. On each pass, successive
neighboring pairs are compared. If a pair is in decreasing order, its values are swapped; otherwise,
the values remain unchanged. Gradually values bubble their way to the top and the larger values
sink to the bottom. Help the scientist to test program that reads in ten numbers, invokes the
function, and displays the resultant numbers.
CODE:-
def SortList(l,no):
for i in range(no-1):
for j in range(no-1):
if l[j] > l[j+1]:
temp = l[j]
l[j] = l[j+1]
l[j+1] = temp
return l
n = int(input("Enter how many elements you have to enter: "))
l1 = []
for i in range(n):
elem = int(input())
l1.append(elem)
print("UnSorted List: ",l1)
res = SortList(l1,n)
print("Sorted List: ",res)
OUTPUT:-
SWARAJ PANDEY
21162121036
BDA
SEM 4

3. Given a matrix with N rows and M columns, the task is to check if the matrix is a Binary Matrix. A
binary matrix is a matrix in which all the elements are either 0 or 1.
Input Format: The first line of the input contains two integer number N and M which represents the
number of rows and the number of columns respectively, separated by a space. From the second
line, take N lines input with each line containing M integer elements with each element separated
by a space (not compulsory).
Output Format: Print yes or no; accordingly
CODE:-
m, n = input('Enter rows and columns using " " as delimiter: ').split(' ')
matrixList = []
for i in range(int(m)):
columnList = []
userValue = input('Enter list element A[' + str(i) + '][]: ').split('
')[:int(n)]
for j in range(len(userValue)):
columnList.append(int(userValue[j]))
matrixList.append(columnList)
counterVar = 0
for i in range(int(m)):
for j in range(int(n)):
if (matrixList[i][j] == 0) or (matrixList[i][j] == 1):
counterVar += 1
print('\nMatrix:')
for i in range(int(m)):
for j in range(int(n)):
print(str(matrixList[i][j]) + '\t', end='')
SWARAJ PANDEY
21162121036
BDA
SEM 4

print()
if counterVar == int(m) * int(n):
print('\nBinary Matrix:', True)
else:
print('\nBinary Matrix:', False)
OUTPUT:-

You might also like