Question

With the code given write python code that prints the probablity of getting a straight in...

With the code given write python code that prints the probablity of getting a straight in poker when you run 10**5 trails.

HELPER CODE:

# We will represent cards as a string, e.g., 'AC' will be Ace of Clubs

# Denominations: 2, ..., 10, 'J' = Jack, 'Q' = Queen, 'K' = King, 'A' = Ace
Denominations = ['2','3','4','5','6','7','8','9','10','J','Q','K','A']

# Suits 'S' = Spades, 'H' = Hearts, 'D' = Diamonds, 'C' = Clubs
Suits = ['C', 'H', 'S', 'D']

# Note that colors are determined by the suits (hearts and diamonds are red, others black,
# so, AC is Black
  
# List comprehensions are a great way to avoid explicit for loops when creating lists

Deck = [(d+s) for d in Denominations for s in Suits] # Note the double for loop

print( Deck )

# Now we can "deal" cards by choosing randomly from the deck

seed(0) # seed makes sure that all your computations start with the same random sequence;
# this not really important, and only necessary for debugging and grading.
def dealCard():
return choice(Deck) # choice randomly chooses an element of a list

print( dealCard() )

# When dealing a hand in cards, the selection of cards is without replacement, that is, cards are removed from
# the deck one by one and not put back. This can be simulated in the choice function by setting the replace
# parameter to False.

seed(0)

def dealHand(withReplacement = False,size = 5):
return choice(Deck,size,withReplacement) # chooses a list of size elements

print( dealHand() )

# extract the denomination and the suit from a card

def denom(c):
return c[0:-1]

def suit(c):
return c[-1]

# The function rank(c) will simply return the position of the card c PLUS 2 in the list 2, 3, ...., K, A. This will be used in an essential
# way in our code below. Although in the diagram given lecture, Ace is below 2, the Ace is actually considered to be ordered
# above the King, for example in determining a straight, under "Ace high rules."

# rank(2) = 2, ...., rank(10) = 10, rank(Jack) = 11, rank(Queen) = 12, rank(King) = 13, rank(Ace) = 14

def rank(c):
return Denominations.index(denom(c))+2

# Now we want to identify various kinds of cards

def isHeart(c):
return ( suit(c) == 'H')

def isDiamond(c):
return ( suit(c) == 'D')

def isClub(c):
return ( suit(c) == 'C')

def isSpade(c):
return ( suit(c) == 'S')

def isRed(c):
return ( isHeart(c) or isDiamond(c) )

def isBlack(c):
return (not isRed(c))

def isFaceCard(c):
return rank(c) >= 11 and rank(c) <= 13

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code to be added to the helper code:

import random
def choice(Deck,size =1,withReplacement=False):
deck = Deck[:]
i = 0
hand=[]
while(i<size):
k =random.randint(1,len(deck)-1)
x = deck[k]
hand.append(x)
if(withReplacement==False):
deck.remove(x)
i = i+1
return hand

def isStraingt(hand):
card_rank = []
for c in hand:
card_rank.append(rank(c))
card_rank.sort()
for i in range(0,len(card_rank)-1):
if(card_rank[i+1]!=(card_rank[i]+1)):
return 0
return 1

sum=0.0
for i in range(1,10**5):
sum = sum + isStraingt(dealHand())
probability = sum/(10**5)
print(probability)

Output:

Add a comment
Know the answer?
Add Answer to:
With the code given write python code that prints the probablity of getting a straight in...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write in Java! Do NOT write two different programs for Deck and Card, it should be...

    Write in Java! Do NOT write two different programs for Deck and Card, it should be only one program not 2 separate ones!!!!!! !!!!!!!!!!!!!!!Use at least one array defined in your code and two array lists defined by the operation of your code!!!!!!!!!!!!!!!!!!!!! The array should be 52 elements and contain a representation of a standard deck of cards, in new deck order. (This is the order of a deck of cards new from the box.) The 2 Array lists...

  • C++ Your solution should for this assignment should consist of five (5) files: Card.h (class specification...

    C++ Your solution should for this assignment should consist of five (5) files: Card.h (class specification file) Card.cpp (class implementation file) DeckOfCards.h (class specification file) DeckOfCards.cpp (class implementation file) 200_assign6.cpp (application program) NU eelLS Seven UT Diamonds Nine of Hearts Six of Diamonds For your sixth programming assignment you will be writing a program to shuffle and deal a deck of cards. The program should consist of class Card, class DeckOfCards and an application program. Class Card should provide: a....

  • There are 52 cards in a deck. 26 are red, and 26 are black. The 52...

    There are 52 cards in a deck. 26 are red, and 26 are black. The 52 cards make up four suits (hearts, diamonds, spades, clubs). There are 13 of each suit (ace-10, jack, queen, king). Essentially it is a fair deck of cards. a) What is the probability of drawing the 10 of clubs or a king, and then a spade? b) What is the probability of drawing a 7 or a heart, and then a 10 of hearts or...

  • how many diamonds are in a deck of cards?

    A deck of cards contains 52 cards. They are divided into four suits: spades, diamonds, clubs and hearts. Each suit has 13 cards: ace through 10, and three picture cards: Jack, Queen, and King. Two suits are red in color: hearts and diamonds. Two suits are black in color: clubs and spades.Use this information to compute the probabilities asked for below and leave them in fraction form. All events are in the context that three cards are dealt from a...

  • A standard 52-card deck has four 13-card suits: diamonds, hearts, 13-card suit contains cards numbered f...

    A standard 52-card deck has four 13-card suits: diamonds, hearts, 13-card suit contains cards numbered f probability of drawing a black king of hearts clubs, and spades. The diamonds and hearts are red, and the clubs and spades are black Each from 2 to 10, a jack, a queen, a king, and an ace. An experiment consists of drawing 1 card from the standard deck. Find the The probability of choosing a black king of hearts is ype an integer...

  • A standard deck of cards consists of four suits (clubs, diamonds, hearts, and spades), with each...

    A standard deck of cards consists of four suits (clubs, diamonds, hearts, and spades), with each suit containing 13 cards (ace, two through ten, jack, queen, and king) for a total of 52 cards in all. How many 7-card hands will consist of exactly 3 kings and 2 queens?

  • 1. A standard deck of cards has 52 cards with 4 suits that include 13 clubs,...

    1. A standard deck of cards has 52 cards with 4 suits that include 13 clubs, 13 diamonds, 13 hearts, and 13 spades. Each suit includes an ace, a king, a queen, and a jack, and numbers 2 through 10. Assume that I choose one card from the deck. a. What is the probability that the card is either a queen or a 4? b. What is the probability that the card is not a queen and it is not...

  • I am having problem understanding this problem. please explain it explicitly. its a discrete comp...

    I am having problem understanding this problem. please explain it explicitly. its a discrete computer science problem. thanks Exercises 27-32 concern a 5-card hand from a standard 52-card deck. A standard deck has 13 cards from each of 4 suits (clubs, diamonds, hearts, spades). The 13 cards have face value 2 through10, jack, queen, king, or ace Each face value is a "kind" of card. The jack, queen, and king are "face cards. 27. How many hands contain 4 queens?...

  • Answer the following questions and use Excel to show your work. A standard deck of playing...

    Answer the following questions and use Excel to show your work. A standard deck of playing cards consists of 52 cards. The cards in each deck consist of 4 suits, namely spades (♠), clubs (♣), diamonds (♦), and hearts (♥). Each suit consists of 13 cards, namely ace, king, queen, jack, 10, 9, 8, 7, 6, 5, 4, 3, and 2. In the game of poker, a royal flush consists of the ace, king, queen, jack, and 10 of the...

  • determine: 1. P(A and B) 2. P(A or B) 3. P( B|A ) Suppose one card...

    determine: 1. P(A and B) 2. P(A or B) 3. P( B|A ) Suppose one card is selected at random from an ordinary deck of 52 playing cards. A standard deck of cards contains for suits: red hearts, red diamonds, black clubs, black spades. Each suite contains 13 cards: Ace, 2,3,4,5,6,7,8, 9, 10, Jack, Queen, King. The Jack, Queen, and King are also called Face Cards. Let A = event a jack is selected B = event a spade is...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT