Question

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 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) 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".

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

a1.py :

def hand_type(cards):
    rank_names = "23456789TJQKA"
    rank_occurs = []         #for storing occurrence of different ranks
    for rank in rank_names:     #looping through the rank and adding occurrence of ranks
        rank_occurs.append(list(cards).count(rank))
    if 4 in rank_occurs:     
        return "four of a kind"            #if a rank occurs 4 times
    elif 2 in rank_occurs and 3 in rank_occurs:     
        return "full house"         #if a rank occurs 2 times and another rank 3 times
    elif 5 in rank_occurs:
        return "flush"           #if a rank occurs 5 times
    elif 3 in rank_occurs and [4, 5] not in rank_occurs:
        return "three of a kind"         #if a rank occurs not more then 3 times
    elif 2 in rank_occurs and [3, 4, 5] not in rank_occurs:
        return "pair"        #if a rank occurs not more then 2 times
    else:
        dic = {}
        for x in range(0, 5):
            dic[cards[2*x]] = rank_names.find(cards[2*x])
        return "{} high".format(max(dic, key=dic.get))       #returning the highest rank


print(hand_type("Kh3h3sAh5h"))     # example hand of cards
Add a comment
Know the answer?
Add Answer to:
Python Programming: An individual playing card is represented as a string of two characters: • the...
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
  • 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...

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

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

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

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

  • bblem deals with playing cards. The Card API is given below: public class Card ( suit...

    bblem deals with playing cards. The Card API is given below: public class Card ( suit is "Clubs", "Diamonds", "Bearts", or "Spades" Gene=ination s 2", , "10" יי", ,"פ" ,"8" ,"ר" , "6" ,"5י ,-4" ,"ני- * or "A * value is the value of the card number if the card denominat, *is between 2 and 10; 11 for J, 12 for Q, 13 for K, 14 for A public Card (String suit, string denomination){} 1/returns the suit (Clubs, Diamonds,...

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

  • java programming 3. Back to contract bridge. The shape of the hand (that is, how "balanced"...

    java programming 3. Back to contract bridge. The shape of the hand (that is, how "balanced" or "unbalanced" it is) is urposes as its raw point count. Write a method often just as important for the hand evaluation p public String bridgeHandShape(String hand) that creates and returns a four-element string that tells how many spades, hearts, diamonds and clubs, in this order, there are in the hand. This result string should contain these four counts in order separated by commas,...

  • Count the number of different functions with the given domain, target and additional properties. -------------------------------------------------------- This...

    Count the number of different functions with the given domain, target and additional properties. -------------------------------------------------------- This question refers to a standard deck of playing cards. If you are unfamiliar with playing cards, there is an explanation in Section 11.1 under the heading "Standard playing cards." A five-card hand is just a subset of 5 cards from a deck of 52 cards. (d) How many five-card hands have four cards of the same rank? (e) A "full house" is a five-card...

  • 19. A Card Game 19. A Card Game Three students are playing a card game. They...

    19. A Card Game 19. A Card Game Three students are playing a card game. They decide to choose the first person to play by each selectinga card from the 52-card deck and look- ing for the highest card in value and suit. They rank the suits from lowest to highest: clubs, diamonds, hearts, and spades. a. If the card is replaced in the deck after each student chooses, how many possible configurations of the three choices are possible? b....

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