Question 2 (20 points) Design a well-documented, Python program myCards.py to si
ID: 3752492 • Letter: Q
Question
Question 2 (20 points) Design a well-documented, Python program myCards.py to simulate shuffling 52 playing cards. In the simulation, the shuffled cards are turned up from the top until the first ace appears. Have your simulation estlmate how many cards are required, on average, to reveal the first ace by conducting the simulation 1,000,000 times. UNSHUFFLED DECK OF CARDS 13 26 39 12 25 27 38 40 SHUFFLED DECK OF CARDS Trial 1 13 Trial 2 Tenth Card, First Ace 39 Sixth Card, First Ace 0 39 13 26 AceAcece Ace 49th Card, First Ace AVERAGE POSITION OF FIRST ACE (10+6+491/3 In the simulation above, using three trials, the average position of the first ace is (10-6+493. On average, probability theory suggests that the first ace, of any suit, will be seen at 48/5 position within the deck Hint: One way of simulating a shuffled deck is by using the function sample within the random module. Here shuffleDeck random.sample(range(52), 52), is a list of shuffled cardsExplanation / Answer
import random
import numpy as np
# List of card types to identify the type of cards
card_type = ['Clubs', 'Diamonds', 'Hearts', 'Spades']
# Number of card to identify the number of card
card_number = ['Two', 'Three', 'Four',
'Five', 'Six', 'Seven', 'Eight', 'Nine', 'Ten', 'Jack', 'Queen', 'King', 'Ace']
# This function will return the shuffeled deck and shuffeled deck type
#
# @param
# @return
# List of tuples
# The first element in a tuple contains the card_number and second element in tuple is card_type
# Card number will be from 1-13, wehre 1 - 9 are normal cards and
# 10 = Jack,
# 11 = Queen,
# 12 = King,
# 13 = Ace
# Card type will be from 0-4, where 0 = Clubs, 1 = Diamonds, 2 = Hearts, 3 = Spades
def getShuffeledDeck():
shuffle_deck = random.sample(range(52), 52)
shuffle_deck_type = [item % 4 for item in shuffle_deck]
shuffle_deck = [(item % 13) + 1 for item in shuffle_deck]
print(shuffle_deck.count(13))
return zip(shuffle_deck, shuffle_deck_type)
# This function will start trial and return the position where the ace has been found.
#
# @param
# trialNumber:int = The trial number or simulation number
#
# @return
# The position at which the ace has been found
def startTrial(trialNumber: int):
print('Starting trial:', trialNumber)
print('Getting shuffeled deck')
# Get the shuffeled deck of cards
shuffeled_deck = getShuffeledDeck()
print('Started opening deck')
# Counter to count the position of ACE or number of cards already been opened
i = 0
for card in shuffeled_deck:
# -------------------------------------------------------------------------------------
# Uncomment this line if you want to see the card type and number which has been opened
# print(card_number[card[0]-1], 'of', card_type[card[1]])
# -------------------------------------------------------------------------------------
# Increase the counter as cards get opened
i += 1
# If card number if 13, i.e, card is ACE return the counter or position of ACE
if (card[0] == 13):
print('Got ace at card number', i)
return i
# This function will run the simulaton @simulationCyles times and print the average
# position of getting ACE in a shuffeled deck.
#
# @param
# simulationCycles:int = Number of types you want to run simulations
#
# @return: None
def startSimulation(simulationCycles: int):
# Numpy array that contains position of ACE found every time the simulation run
positons = np.zeros(simulationCycles)
# Run simulation from 0 to simulationCycles time, and get the position of ACE and store it
# to positions numpy array
for cycle in range(simulationCycles):
positons[cycle] = startTrial(cycle + 1)
print('After cycles', simulationCycles)
# Calculate the average position by adding the elements in positions array and dividing by
# number of elements in positions array
averagePosition = np.sum(positons) / simulationCycles
print('On average', averagePosition,
'Cards are required to get first ace after', simulationCycles, 'cycles')
# Trigger the simulation and see the output in console
startSimulation(1000000)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.