4.7 KiB
id, title, challengeType, dashedName
| id | title | challengeType | dashedName |
|---|---|---|---|
| 69f90c1329a94b37e2a2086c | Challenge 293: Best Hand | 29 | challenge-293 |
--description--
Given an array of five strings representing playing cards, return the name of the best hand.
- Each card is represented as a two-character string: the rank followed by the suit,
"2h"for example.- Ranks, from low to high, are:
"2","3","4","5","6","7","8","9","T","J","Q","K", and"A". - Suits are:
"h","d","c", and"s".
- Ranks, from low to high, are:
- Aces (
"A") can be used as high or low in a straight.
The hands, in order from worst to best, are:
| Name | Description |
|---|---|
"High Card" |
No pair or better |
"Pair" |
Two of one rank |
"Two Pair" |
Two of one rank and two of another |
"Three of a Kind" |
Three of one rank |
"Straight" |
Five ranks in a row |
"Flush" |
Five of the same suit |
"Full House" |
Three of one rank, and two of another |
"Four of a Kind" |
Four of one rank |
"Straight Flush" |
Five ranks in a row of the same suit |
"Royal Flush" |
"A", "K", "Q", "J", "T" of the same suit |
Return the name of the best hand.
--hints--
get_best_hand(["7s", "7h", "7d", "2c", "5h"]) should return "Three of a Kind".
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_best_hand(["7s", "7h", "7d", "2c", "5h"]), "Three of a Kind")`)
}})
get_best_hand(["Ks", "Kh", "Kd", "4s", "4h"]) should return "Full House".
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_best_hand(["Ks", "Kh", "Kd", "4s", "4h"]), "Full House")`)
}})
get_best_hand(["2h", "5h", "7h", "9h", "Jh"]) should return "Flush".
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_best_hand(["2h", "5h", "7h", "9h", "Jh"]), "Flush")`)
}})
get_best_hand(["As", "Ah", "Ad", "Ac", "Kh"]) should return "Four of a Kind".
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_best_hand(["As", "Ah", "Ad", "Ac", "Kh"]), "Four of a Kind")`)
}})
get_best_hand(["Ts", "Th", "9d", "9c", "8h"]) should return "Two Pair".
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_best_hand(["Ts", "Th", "9d", "9c", "8h"]), "Two Pair")`)
}})
get_best_hand(["9c", "8c", "7c", "6c", "5c"]) should return "Straight Flush".
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_best_hand(["9c", "8c", "7c", "6c", "5c"]), "Straight Flush")`)
}})
get_best_hand(["As", "Kh", "Jd", "8c", "5h"]) should return "High Card".
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_best_hand(["As", "Kh", "Jd", "8c", "5h"]), "High Card")`)
}})
get_best_hand(["As", "2h", "3d", "4c", "5h"]) should return "Straight".
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_best_hand(["As", "2h", "3d", "4c", "5h"]), "Straight")`)
}})
get_best_hand(["Ts", "Th", "7c", "6d", "5h"]) should return "Pair".
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_best_hand(["Ts", "Th", "7c", "6d", "5h"]), "Pair")`)
}})
get_best_hand(["As", "Ks", "Qs", "Js", "Ts"]) should return "Royal Flush".
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_best_hand(["As", "Ks", "Qs", "Js", "Ts"]), "Royal Flush")`)
}})
--seed--
--seed-contents--
def get_best_hand(cards):
return cards
--solutions--
def get_best_hand(cards):
rank_order = "23456789TJQKA"
ranks = [c[0] for c in cards]
suits = [c[1] for c in cards]
is_flush = len(set(suits)) == 1
rank_indices = sorted(rank_order.index(r) for r in ranks)
def is_straight(indices):
is_regular = all(indices[i] == indices[i - 1] + 1 for i in range(1, len(indices)))
is_low_ace = indices == [0, 1, 2, 3, 12]
return is_regular or is_low_ace
straight = is_straight(rank_indices)
counts = {}
for r in ranks:
counts[r] = counts.get(r, 0) + 1
freq = sorted(counts.values(), reverse=True)
if is_flush and rank_indices == [8, 9, 10, 11, 12]:
return "Royal Flush"
if is_flush and straight:
return "Straight Flush"
if freq[0] == 4:
return "Four of a Kind"
if freq[0] == 3 and freq[1] == 2:
return "Full House"
if is_flush:
return "Flush"
if straight:
return "Straight"
if freq[0] == 3:
return "Three of a Kind"
if freq[0] == 2 and freq[1] == 2:
return "Two Pair"
if freq[0] == 2:
return "Pair"
return "High Card"