Write a function to compare poker hands.
Write a function to compare poker hands.
UPDATE: Solution
I’ve posted a video explanation of one way to solve this problem. I hope that it’s helpful! It’s a little longer than I’d planned on, but I wanted to be thorough in walking through the whole problem-solving process: problem, pseudocode, Python. The solution code is here: poker.py. And, uh, like and subscribe? 🙂
Background
In the game of poker, there are a variety of possible hands that can be formed from five playing cards. These hands can be ranked according to this ordered list such that, e.g., any flush (all cards have the same suit) beats any straight (cards in ascending order, e.g., 4-5-6-7-8). Within each category of hand, hands can be ordered by their best card, e.g., a pair of aces is better than a pair of tens.
Objective
You must write a Python function called compare_hands
that will compare two
poker hands by accepting two arguments.
Each of these arguments will be a 5-tuple of strings.
Each string will contain a card value and a card suit separated by a space.
For example, A D
represents the ace of diamonds and 10 C
represents the
ten of clubs.
Details
The possible card values are, in descending order:
String | Card value |
---|---|
|
ace |
|
king |
|
queen |
|
jack |
|
10 |
|
9 |
|
8 |
|
7 |
|
6 |
|
5 |
|
4 |
|
3 |
|
2 |
The four possible suits are (in alphabetical order):
String |
Suit |
|
clubs |
|
diamods |
|
hearts |
|
spades |
Your compare_hands
function should return a 1 if the first hand passed into it
beats the second hand, -1 if the second hand beats the first and 0 if they
tie.
Submit your work to Gradescope in a Python module entitled poker
.
Remember, assignments are individual work: you must complete the assignment
yourself.