Python Problems
P42) From a pack of 14 cards, having numbers 1 to 10 alongwith Jack, King, Queen, Ace; choose two
cards. Find the percent of times King and Queen both gets chosen for 10000 simulations.
import random
cards = list(range(1, 11))
cards = cards + ['QUEEN', 'KING', 'JACK', 'ACE']
success = 0
no_of_sims = 10000
for i in range(no_of_sims):
choice = random.sample(cards, 4)
if ('QUEEN' in choice) and ('KING' in choice):
success += 1
p = success / no_of_sims * 100
print(f"Percentage = {p}")
P43) Game of Cricket.
import random
scores = [1, 2, 3, 4, 6, 'OUT']
score, ball = 0, 0
play = input("Welcome to the GAME OF CRICKET. Press Y to play! ")
if play != 'Y':
exit()
while True:
ch = random.choice(scores)
ball += 1
if ch == 'OUT':
print("\nYou are OUT!!!")
print(f"Your score is {score} in {ball} balls")
break
else:
score += ch
print(f"\nYou have scored {ch}")
print(f"TOTAL SCORE: {score}")
next_ball = input("\nNext Ball? (Y/N) ").upper()
if next_ball == 'N':
print("\nYou have forfeited")
print(f"Your score is {score} in {ball} balls")
break
P44) Two players roll two dices simultaneously, and the player with higher number wins. Perform the
simulation.
import random
player1_roll = random.randrange(1, 7)
player2_roll = random.randrange(1, 7)
print(f"Player 1 rolled: {player1_roll}")
print(f"Player 2 rolled: {player2_roll}")
if player1_roll > player2_roll:
print("Player 1 wins!")
elif player1_roll < player2_roll:
print("Player 2 wins!")
else:
print("It's a tie!")
P45) For 10000 simulations, let dices roll simultaneously, and calculate the percentage for which the
sum of two dice scores comes as 6.
import random
num_simulations = 10000
success_count = 0
for _ in range(num_simulations):
dice_roll_one = random.randrange(1, 7)
dice_roll_two = random.randrange(1, 7)
dice_roll = dice_roll_two + dice_roll_one
if dice_roll == 6:
success_count += 1
percentage = success_count / num_simulations * 100
print(f"The percentage of getting six is approximately {percentage}")
P46) Roman to Integer
x = input()
nos = []
for letter in x:
if letter.upper() == 'I':
nos.append(1)
elif letter.upper() == 'V':
nos.append(5)
elif letter.upper() == 'X':
nos.append(10)
elif letter.upper() == 'L':
nos.append(50)
elif letter.upper() == 'C':
nos.append(100)
elif letter.upper() == 'D':
nos.append(500)
elif letter.upper() == 'M':
nos.append(1000)
res = 0
for i in range(len(nos)):
if i == len(nos) - 1:
res = res + nos[i]
elif nos[i] >= nos[i+1]:
res = res + nos[i]
elif nos[i] < nos[i+1]:
nos[i+1] = nos[i+1] - nos[i]
print(res)
P47) Print a random prime number between 1 to N.
import random
N = int(input("Enter the value of N: "))
prime_list = []
for num in range(2, N + 1):
is_prime = True
for i in range(2, num):
if num % i == 0:
is_prime = False
break
if is_prime:
prime_list.append(num)
print(f"A random prime number in the range {2} to {N} is
{random.choice(prime_list)}")
P48) Count the duplicate numbers in a list.
# Example list
input_list = [1, 2, 3, 4, 5, 1, 2, 1, 1, 7, 8, 3]
counted_numbers = []
unique_count = 0
seen_values = []
for itr in range(len(input_list)):
count = 1
for itr_2 in range(itr+1, len(input_list)):
if (input_list[itr] == input_list[itr_2]) and (input_list[itr] not
in counted_numbers):
count += 1
if count != 1:
counted_numbers.append(input_list[itr])
print(f"The number {input_list[itr]} is present {count} times")
P49) Print unique elements in two lists.
# Example lists
list1 = [1, 2, 3]
list2 = ['a', 'b', 'c', 1, 2]
combinations = []
for item in list1:
if item in list2:
combinations.append(item)
print("All unique combinations:")
for combination in combinations:
print(combination)
P50) Write a program to calculate the sum of series up to n term. For example, if n =5 the series will
become 2 + 22 + 222 + 2222 + 22222 = 24690
n = int(input("Enter the value of n: "))
series_sum = 0
term = 2
for i in range(n):
series_sum += term
term = (term * 10) + 2
print(f"Sum of the series up to {n} terms: {series_sum}")
P51) Write a program to calculate the sum of the first n terms of the harmonic series: 1 + 1/2 + 1/3 +
1/4 + ... 1/n
n = int(input("Enter the value of n: "))
total_sum = 0
for i in range(1, n + 1):
total_sum += 1 / i
print(f"Sum of the first {n} terms of the harmonic series: {total_sum}")
P52) Write a program to calculate the sum of the first n terms of the alternating series: 1 - 2 + 3 - 4 +
... – n
n = int(input("Enter the value of n: "))
total_sum = 0
for i in range(1, n + 1):
if i % 2 == 0:
total_sum -= i
else:
total_sum += i
print(f"Sum of the first {n} terms of the alternating series: {total_sum}")
P53) Write a program to calculate the sum of the square roots of the natural numbers up to the n-th
term: √1 + √2 + √3 + ... + √n
n = int(input("Enter the value of n: "))
total_sum = 0
for i in range(1, n + 1):
total_sum += i ** 0.5
print(f"Sum of the first {n} terms of the square root series: {total_sum}")
P54) Generate 3 random integers between 1 and N which is divisible by 5.
import random
def generate_random_divisible_by_5(N, count):
divisible_numbers = []
while len(divisible_numbers) < count:
num = random.randrange(1, N+1)
if num % 5 == 0:
divisible_numbers.append(num)
return divisible_numbers
N = int(input("Enter the upper limit N: "))
count = 3 # Change this value to generate a different number of integers
random_divisible_numbers = generate_random_divisible_by_5(N, count)
print(f"3 random integers between 1 and {N} divisible by 5:
{random_divisible_numbers}")