Question

Learning objectives 1. To implement decisions using if statements 2. To write statements using the boolean...

Learning objectives
1. To implement decisions using if statements
2. To write statements using the boolean primitive data type.
3. To compare strings and/or characters.
4. To write loops using while or for.
5. To write functions
Representing playing cards and hands of cards
An individual playing card is represented as a string of two characters:
• the first character is from "23456789TJQKA" and represents the rank, i.e., the number or
value of the card. (Note that 10 is encoded as letter T to make all card ranks to be single letters)
• the second character is from "cdhs" and represents the suit (clubs, diamonds, hearts and spades
respectively).
For example, "Jd" would be the jack of diamonds, and "4s" would be the four of spades.
A 'hand' is made up of five cards and is given as string encoding all those cards consecutively. For
example, "Kh3h7s8h2h" represents a five-card hand that happens to have one spade and four hearts.
Note that the cards can be listed inside the string in any order, not necessarily sorted by suit or
rank. The suits and ranks are also case sensitive, with the rank always given as a digit or an uppercase
letter, and the suit always given as a lowercase letter from the four possible letters cdhs.
What to write:
Write a Python program, a1.py, where the name of the file is exactly that. The file has a function
evaluate(hand) that identifies and returns what kind of poker hand is represented by the 10-character
string hand. There are six (and only six) types of hands that your function should recognize:
(1) four of a kind -- four cards have the same rank (it is not possible to have five cards with the same
rank)
(2) full house -- two cards have one same rank and three cards have another same rank
(3) flush -- all five cards have the same suit
(4) three of a kind -- three cards (not more) have the same rank
(5) pair -- two cards (not more) have the same rank
(6) <highest rank> high -- none of (1) - (5) applies, so you return just the highest rank in the hand
followed by the word 'high'. For example, '9 high' or 'K high'. Rank increases from left to right
in "23456789TJQKA".

Your program can assume that each input is a legal five-card poker hand from one of the possibilities
listed above, so your program does not need to recognize and recover from illegal inputs.
Also write some code that tries out your evaluate(hand) function with examples from all six cases.
An example run of the program might look like the following, with the values of hand in italics, followed
by the string that evaluate(hand) returns:
Qs7s2s4s5s
flush
7h8hKsTs8s
pair
2h4d2d4s4c
full house
KsKhKc8sKd
four of a kind
3s9hTh9s9d
three of a kind
2s8hThQs9d
Q high

NOTE: MAINLY NEED HELP CODING "flush" part,or recognizing when all suits are same. Thank you.

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

def high(rank):            #if any of the 5 types hand is not true     

    if "A" in rank:         #then method high will return a highest rank card from hand

        return "A"          #it works in decending order of rank(checks from highest(A) rank to lowest rank(6))

    elif "K" in rank:

        return "K"

    elif "Q" in rank:

        return "Q"

    elif "J" in rank:

        return "J"

    elif "T" in rank:

        return "T"

    elif "9" in rank:

        return "9"

    elif "8" in rank:

        return "8"

    elif "7" in rank:

        return "7"

    else:

        return "6"

def count(item,l):      #count method accepts perticular rank or suit and corrosponding list of rank or suits

    c=0                 #and provides a count of rank or suit in a hand, here item means rank or suit and l is corrosponding list.

    for i in l:         #it traverse the list from 1st element to last

        if i==item:     #if similar rank or suit found.

            c+=1        #then count will increment by 1.

    return c            #at the end it will return total count.

def evaluate(hand):

    rank = []            

    suit = []

    i = 0               

    j = 1

    f1=0                #f1 & f2 are flags initially zero, both will become 1 if statement of line 46,49 is true.

    f2=0

    while (i < 10):             #it will create a list of all ranks of hand

        rank.append(hand[i])    #append method works only with list, if condition is true then it will insert that element into list

        i += 2                  #i increments by 2 because we have rank on 0,2,4,6,8 index of hand.

    

    while (j < 10):             #it will create a list of all suits of hand

        suit.append(hand[j])

        j += 2                  #i increments by 2 because we have suit on 1,3,5,7,9 index of hand.

    

    for i in range(0,5):           #this loop checks every rank in list

        if count(rank[i],rank)==4:  #if any rank having count equals to 4

            return "four of a kind"  #then it will return four of a kind

    for i in range(0, 5):

        if count(rank[i],rank)==2:  #if any rank having count equals to 2

            f1=1                    #flag1 will become 1

    for i in range(0, 5):

        if count(rank[i], rank) == 3:   #if any rank having count equals to 3

            f2=1                        #flag1 will become 1

    

    if f1==1 and f2==1:             #if flag1 and flag2 equals to 1 then

        return "Full House"         #it will return full house

    for i in range(0, 5):           

        if count(suit[i],suit)==5:  #if any suit having count equals to 5

            return "Flush"          #then it will return flush

    for i in range(0, 5):

        if count(rank[i],rank)==3:

            return "three of a kind"

    for i in range(0, 5):

        if count(rank[i],rank)==2:

            return "pair"

    else:

        return high(rank), 'high'   #if any of above will not true the it will return highest ranked card from hand

ha=input("Enter a hand:")

hand=list(ha)

l1=[]                   

l2=[]

i=0

j=1

while(i<10):                #this code explained above

    l1.append(hand[i])

    i+=2

while(j<10):

    l2.append(hand[j])

    j+=2

rank="23456789TJQKA"        #string of all possible ranks

suit="cdhs"                 #string of all possible suits.

f1=0

f2=0

for i in l1:                #this loop will check every element in list of rank

    if i in rank:           #if rank is one of from rank string

        f1=1                #then flag1 will 1

    else:

        f1=0

for i in l2:                 #this loop will check every element in list of suit

    if i in suit:            #if rank is one of from suit string

        f2=1                 #then flag2 will 1

    else:

        f2=0

if(f1==1 and f2==1):        #if both flags are 1 then

    print(evaluate(hand))   #evaluate method will be called

else:                         

    print("Enter a valid hand:")

Add a comment
Know the answer?
Add Answer to:
Learning objectives 1. To implement decisions using if statements 2. To write statements using the boolean...
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
  • Python Programming: An individual playing card is represented as a string of two characters: • the...

    Python Programming: An individual playing card is represented as a string of two characters: • the first character is from "23456789TJQKA" and represents the rank, i.e., the number or value of the card. (Note that 10 is encoded as letter T to make all card ranks to be single letters) • the second character is from "cdhs" and represents the suit (clubs, diamonds, hearts and spades respectively). For example, "Jd" would be the jack of diamonds, and "4s" would be...

  • This has to be done in Haskell only. Write a program that: Given 2 poker hands...

    This has to be done in Haskell only. Write a program that: Given 2 poker hands (5 cards) in list of tuples form (ex. hand1 = [(1,0),(2,0),(3,0),(4,0),(5,0)]). Each tuple represents a card where first number of a tuple is its value (1=Ace, 2=2,...,10=10, 11=Jack, 12=Queen, 13=King) and second number its suit (0=Clubs, 1=Diamonds, 2=Hearts, 3=Spades) so (1,0) will be Ace of Clubs, or (5,2) will be 5 of Hearts, etc. Rule website: https://www.fgbradleys.com/et_poker.asp These hands are generated by an end...

  • This activity needs to be completed in the Python language. This program will simulate part of...

    This activity needs to be completed in the Python language. This program will simulate part of the game of Poker. This is a common gambling game comparing five-card hands against each other with the value of a hand related to its probability of occurring. This program will simply be evaluating and comparing hands, and will not worry about the details of dealing and betting. Here follow the interesting combinations of cards, organized from most common to least common: Pair --...

  • discrete structure Recall that a standard deck of 52 cards has 4 suits (hearts, diamonds, spades, and clubs), each of w...

    discrete structure Recall that a standard deck of 52 cards has 4 suits (hearts, diamonds, spades, and clubs), each of which has 13 ranks: 2-10, Jack, Queen, King, and Ace (in order from lowest to highest). Order of cards in a hand does not matter (a) (10 points) A full house is 3 cards of one rank and 2 of another rank. How many full houses are there in a 5-card hand if either the pair or the 3 of...

  • 1. Find the probabilities of Poker hands not covered in class. Recall in a Poker game,...

    1. Find the probabilities of Poker hands not covered in class. Recall in a Poker game, 5 cards are drawn uniformly at random from a deck of 52 cards. Each deck has 13 denominations ({A, 2, 3, . . . , 10, J Q, K)) and 4 suits (4,◇,0,6 ). In what follows consecutive denominations are But (J,Q,K, A, 21 is not considered consecutive. Find the probabilities of a. Straight Flush (5 cards of consecutive denomination, all of the same...

  • Need help writing this program! Thanks in advance. Project4 Specifications: Determine Poker Hand A poker hand...

    Need help writing this program! Thanks in advance. Project4 Specifications: Determine Poker Hand A poker hand can be stored in a two-dimensional array. The statement: Dim aryintCards(4,14) as Integer (See AryCardExample.xlsx excel spreadsheet) declares an array which the first subscript 1-4 are the four suits and the second subscript ranges over the card denominations: 1-14: Ace, 2, ..., 10, Jack, Queen, King, Ace (repeated). First subscript of 0 is Total of each denomination. Second subscript of 0 is Total of...

  • A standard poker deck of 52 cards has four suits, (symbols C, H, S, and D) and thirteen ranks (sy...

    A standard poker deck of 52 cards has four suits, (symbols C, H, S, and D) and thirteen ranks (symbols A, 2, 3, 4, 5, 6, 7, 8, 9, T, J, Q, and K). Every card in the deck has both a value and a suit.1 A poker hand is any set of 5 cards from the standard poker deck. There are some special hands in poker, and these have ranks (i.e. some are better, some are worse). From best...

  • 3 Suppose a deck of playing cards has 52 cards, represented by the set C : C = {1C, 2C, . . . , 1...

    3 Suppose a deck of playing cards has 52 cards, represented by the set C : C = {1C, 2C, . . . , 13C, 1D, 2D, . . . , 13D, 1H, 2H, . . . 13H, 1S, 2S, . . . , 13S}. 2 Here C, D, H, or S stands for the suit of a card: ”clubs”, ”diamonds”, ”hearts”, or ”spades”. Suppose five cards are drawn at random from the deck, with all possibilities being equally likely....

  • 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....

  • 2.2.21. Let A be the set of five-card hands dealt from a 52-card poker deck, where...

    2.2.21. Let A be the set of five-card hands dealt from a 52-card poker deck, where the denominations of the five cards are all consecutive—for example, (7 of hearts, 8 of spades, 9 of spades, 10 of hearts, jack of diamonds). Let Bbe the set of five-card hands where the suits of the five cards are all the same. How many outcomes are in the event A ∩ B? the final answer is 40

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