Skip to content

Commit ed9506b

Browse files
kytrinyxkotp
authored andcommitted
Sync poker with problem-specifications
The docs and tests.toml were updated in the sync, and I regenerated the test suite. A number of new tests were brought in, one of which required a tweak to the example solution.
1 parent 3c4b83a commit ed9506b

File tree

4 files changed

+165
-125
lines changed

4 files changed

+165
-125
lines changed

exercises/practice/poker/.docs/instructions.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@
22

33
Pick the best hand(s) from a list of poker hands.
44

5-
See [wikipedia](https://en.wikipedia.org/wiki/List_of_poker_hands) for an
6-
overview of poker hands.
5+
See [wikipedia][poker-hands] for an overview of poker hands.
6+
7+
[poker-hands]: https://en.wikipedia.org/wiki/List_of_poker_hands

exercises/practice/poker/.meta/example.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ def five_high_straight?
7777
end
7878

7979
def straight?
80-
rank_values.each_cons(2).all? { |a, b| a + 1 == b }
80+
(rank_values - [14]).each_cons(2).all? { |a, b| a + 1 == b }
8181
end
8282

8383
def flush?

exercises/practice/poker/.meta/tests.toml

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,12 @@ description = "both hands have two pairs, with the same highest ranked pair, tie
3939
[15a7a315-0577-47a3-9981-d6cf8e6f387b]
4040
description = "both hands have two identically ranked pairs, tie goes to remaining card (kicker)"
4141

42+
[f761e21b-2560-4774-a02a-b3e9366a51ce]
43+
description = "both hands have two pairs that add to the same value, win goes to highest pair"
44+
45+
[fc6277ac-94ac-4078-8d39-9d441bc7a79e]
46+
description = "two pairs first ranked by largest pair"
47+
4248
[21e9f1e6-2d72-49a1-a930-228e5e0195dc]
4349
description = "three of a kind beats two pair"
4450

@@ -57,6 +63,9 @@ description = "aces can end a straight (10 J Q K A)"
5763
[76856b0d-35cd-49ce-a492-fe5db53abc02]
5864
description = "aces can start a straight (A 2 3 4 5)"
5965

66+
[e214b7df-dcba-45d3-a2e5-342d8c46c286]
67+
description = "aces cannot be in the middle of a straight (Q K A 2 3)"
68+
6069
[6980c612-bbff-4914-b17a-b044e4e69ea1]
6170
description = "both hands with a straight, tie goes to highest ranked card"
6271

@@ -90,5 +99,17 @@ description = "with multiple decks, both hands with identical four of a kind, ti
9099
[923bd910-dc7b-4f7d-a330-8b42ec10a3ac]
91100
description = "straight flush beats four of a kind"
92101

102+
[d9629e22-c943-460b-a951-2134d1b43346]
103+
description = "aces can end a straight flush (10 J Q K A)"
104+
105+
[05d5ede9-64a5-4678-b8ae-cf4c595dc824]
106+
description = "aces can start a straight flush (A 2 3 4 5)"
107+
108+
[ad655466-6d04-49e8-a50c-0043c3ac18ff]
109+
description = "aces cannot be in the middle of a straight flush (Q K A 2 3)"
110+
93111
[d0927f70-5aec-43db-aed8-1cbd1b6ee9ad]
94-
description = "both hands have straight flush, tie goes to highest-ranked card"
112+
description = "both hands have a straight flush, tie goes to highest-ranked card"
113+
114+
[be620e09-0397-497b-ac37-d1d7a4464cfc]
115+
description = "even though an ace is usually high, a 5-high straight flush is the lowest-scoring straight flush"

exercises/practice/poker/poker_test.rb

Lines changed: 139 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -2,195 +2,213 @@
22
require_relative 'poker'
33

44
class PokerTest < Minitest::Test
5-
def test_one_hand
6-
high_of_jack = %w[4S 5S 7H 8D JC]
7-
game = Poker.new([high_of_jack])
8-
assert_equal [high_of_jack], game.best_hand
5+
def test_single_hand_always_wins
6+
# skip
7+
hands = [%w[4S 5S 7H 8D JC]]
8+
assert_equal [%w[4S 5S 7H 8D JC]], Poker.new(hands).best_hand
99
end
1010

11-
def test_highest_card
11+
def test_highest_card_out_of_all_hands_wins
1212
skip
13-
high_of_8 = %w[4S 5H 6S 8D 2H]
14-
high_of_queen = %w[2S 4H 6C 9D QH]
15-
game = Poker.new([high_of_8, high_of_queen])
16-
assert_equal [high_of_queen], game.best_hand
13+
hands = [%w[4D 5S 6S 8D 3C], %w[2S 4C 7S 9H 10H], %w[3S 4S 5D 6H JH]]
14+
assert_equal [%w[3S 4S 5D 6H JH]], Poker.new(hands).best_hand
1715
end
1816

19-
def test_highest_card_10
17+
def test_a_tie_has_multiple_winners
2018
skip
21-
high_of_8 = %w[4D 5S 6S 8D 3C]
22-
high_of_10 = %w[2S 4C 7S 9H 10H]
23-
game = Poker.new([high_of_8, high_of_10])
24-
assert_equal [high_of_10], game.best_hand
19+
hands = [%w[4D 5S 6S 8D 3C], %w[2S 4C 7S 9H 10H], %w[3S 4S 5D 6H JH], %w[3H 4H 5C 6C JD]]
20+
assert_equal [%w[3S 4S 5D 6H JH], %w[3H 4H 5C 6C JD]], Poker.new(hands).best_hand
2521
end
2622

27-
def test_nothing_vs_one_pair
23+
def test_multiple_hands_with_the_same_high_cards_tie_compares_next_highest_ranked_down_to_last_card
2824
skip
29-
high_of_king = %w[4S 5H 6C 8D KH]
30-
pair_of_4 = %w[2S 4H 6S 4D JH]
31-
game = Poker.new([high_of_king, pair_of_4])
32-
assert_equal [pair_of_4], game.best_hand
25+
hands = [%w[3S 5H 6S 8D 7H], %w[2S 5D 6D 8C 7S]]
26+
assert_equal [%w[3S 5H 6S 8D 7H]], Poker.new(hands).best_hand
3327
end
3428

35-
def test_two_pair
29+
def test_one_pair_beats_high_card
3630
skip
37-
pair_of_2 = %w[4S 2H 6S 2D JH]
38-
pair_of_4 = %w[2S 4H 6C 4D JD]
39-
game = Poker.new([pair_of_2, pair_of_4])
40-
assert_equal [pair_of_4], game.best_hand
31+
hands = [%w[4S 5H 6C 8D KH], %w[2S 4H 6S 4D JH]]
32+
assert_equal [%w[2S 4H 6S 4D JH]], Poker.new(hands).best_hand
4133
end
4234

43-
def test_one_pair_vs_double_pair
35+
def test_highest_pair_wins
4436
skip
45-
pair_of_8 = %w[2S 8H 6S 8D JH]
46-
fives_and_fours = %w[4S 5H 4C 8C 5C]
47-
game = Poker.new([pair_of_8, fives_and_fours])
48-
assert_equal [fives_and_fours], game.best_hand
37+
hands = [%w[4S 2H 6S 2D JH], %w[2S 4H 6C 4D JD]]
38+
assert_equal [%w[2S 4H 6C 4D JD]], Poker.new(hands).best_hand
4939
end
5040

51-
def test_two_double_pair
41+
def test_two_pairs_beats_one_pair
5242
skip
53-
eights_and_twos = %w[2S 8H 2D 8D 3H]
54-
fives_and_fours = %w[4S 5H 4C 8S 5D]
55-
game = Poker.new([eights_and_twos, fives_and_fours])
56-
assert_equal [eights_and_twos], game.best_hand
43+
hands = [%w[2S 8H 6S 8D JH], %w[4S 5H 4C 8C 5C]]
44+
assert_equal [%w[4S 5H 4C 8C 5C]], Poker.new(hands).best_hand
5745
end
5846

59-
def test_another_two_double_pair
47+
def test_both_hands_have_two_pairs_highest_ranked_pair_wins
6048
skip
61-
aces_and_twos = %w[2S AH 2C AD JH]
62-
queens_and_jacks = %w[JD QH JS 8D QC]
63-
game = Poker.new([aces_and_twos, queens_and_jacks])
64-
assert_equal [aces_and_twos], game.best_hand
49+
hands = [%w[2S 8H 2D 8D 3H], %w[4S 5H 4C 8S 5D]]
50+
assert_equal [%w[2S 8H 2D 8D 3H]], Poker.new(hands).best_hand
6551
end
6652

67-
def test_double_pair_vs_three
53+
def test_both_hands_have_two_pairs_with_the_same_highest_ranked_pair_tie_goes_to_low_pair
6854
skip
69-
eights_and_twos = %w[2S 8H 2H 8D JH]
70-
three_of_4 = %w[4S 5H 4C 8S 4H]
71-
game = Poker.new([eights_and_twos, three_of_4])
72-
assert_equal [three_of_4], game.best_hand
55+
hands = [%w[2S QS 2C QD JH], %w[JD QH JS 8D QC]]
56+
assert_equal [%w[JD QH JS 8D QC]], Poker.new(hands).best_hand
7357
end
7458

75-
def test_two_three
59+
def test_both_hands_have_two_identically_ranked_pairs_tie_goes_to_remaining_card_kicker
7660
skip
77-
three_twos = %w[2S 2H 2C 8D JH]
78-
three_aces = %w[4S AH AS 8C AD]
79-
game = Poker.new([three_twos, three_aces])
80-
assert_equal [three_aces], game.best_hand
61+
hands = [%w[JD QH JS 8D QC], %w[JS QS JC 2D QD]]
62+
assert_equal [%w[JD QH JS 8D QC]], Poker.new(hands).best_hand
8163
end
8264

83-
def test_three_vs_straight
65+
def test_both_hands_have_two_pairs_that_add_to_the_same_value_win_goes_to_highest_pair
8466
skip
85-
three_of_4 = %w[4S 5H 4C 8D 4H]
86-
straight = %w[3S 4D 2S 6D 5C]
87-
game = Poker.new([three_of_4, straight])
88-
assert_equal [straight], game.best_hand
67+
hands = [%w[6S 6H 3S 3H AS], %w[7H 7S 2H 2S AC]]
68+
assert_equal [%w[7H 7S 2H 2S AC]], Poker.new(hands).best_hand
8969
end
9070

91-
def test_a_5_high_straight
71+
def test_two_pairs_first_ranked_by_largest_pair
9272
skip
93-
three_of_4 = %w[4S 5H 4C 8D 4H]
94-
straight_to_5 = %w[4D AH 3S 2D 5C]
95-
game = Poker.new([three_of_4, straight_to_5])
96-
assert_equal [straight_to_5], game.best_hand
73+
hands = [%w[5C 2S 5S 4H 4C], %w[6S 2S 6H 7C 2C]]
74+
assert_equal [%w[6S 2S 6H 7C 2C]], Poker.new(hands).best_hand
9775
end
9876

99-
def test_two_straights
77+
def test_three_of_a_kind_beats_two_pair
10078
skip
101-
straight_to_8 = %w[4S 6C 7S 8D 5H]
102-
straight_to_9 = %w[5S 7H 8S 9D 6H]
103-
game = Poker.new([straight_to_8, straight_to_9])
104-
assert_equal [straight_to_9], game.best_hand
79+
hands = [%w[2S 8H 2H 8D JH], %w[4S 5H 4C 8S 4H]]
80+
assert_equal [%w[4S 5H 4C 8S 4H]], Poker.new(hands).best_hand
10581
end
10682

107-
def test_5_high_straight_vs_other_straight
83+
def test_both_hands_have_three_of_a_kind_tie_goes_to_highest_ranked_triplet
10884
skip
109-
straight_to_jack = %w[8H 7C 10D 9D JH]
110-
straight_to_5 = %w[4S AH 3S 2D 5H]
111-
game = Poker.new([straight_to_jack, straight_to_5])
112-
assert_equal [straight_to_jack], game.best_hand
85+
hands = [%w[2S 2H 2C 8D JH], %w[4S AH AS 8C AD]]
86+
assert_equal [%w[4S AH AS 8C AD]], Poker.new(hands).best_hand
11387
end
11488

115-
def test_straight_vs_flush
89+
def test_with_multiple_decks_two_players_can_have_same_three_of_a_kind_ties_go_to_highest_remaining_cards
11690
skip
117-
straight_to_8 = %w[4C 6H 7D 8D 5H]
118-
flush_to_7 = %w[2S 4S 5S 6S 7S]
119-
game = Poker.new([straight_to_8, flush_to_7])
120-
assert_equal [flush_to_7], game.best_hand
91+
hands = [%w[4S AH AS 7C AD], %w[4S AH AS 8C AD]]
92+
assert_equal [%w[4S AH AS 8C AD]], Poker.new(hands).best_hand
12193
end
12294

123-
def test_two_flushes
95+
def test_a_straight_beats_three_of_a_kind
12496
skip
125-
flush_to_8 = %w[3H 6H 7H 8H 5H]
126-
flush_to_7 = %w[2S 4S 5S 6S 7S]
127-
game = Poker.new([flush_to_8, flush_to_7])
128-
assert_equal [flush_to_8], game.best_hand
97+
hands = [%w[4S 5H 4C 8D 4H], %w[3S 4D 2S 6D 5C]]
98+
assert_equal [%w[3S 4D 2S 6D 5C]], Poker.new(hands).best_hand
12999
end
130100

131-
def test_flush_vs_full
101+
def test_aces_can_end_a_straight_10_j_q_k_a
132102
skip
133-
flush_to_8 = %w[3H 6H 7H 8H 5C]
134-
full = %w[4S 5H 4C 5D 4H]
135-
game = Poker.new([flush_to_8, full])
136-
assert_equal [full], game.best_hand
103+
hands = [%w[4S 5H 4C 8D 4H], %w[10D JH QS KD AC]]
104+
assert_equal [%w[10D JH QS KD AC]], Poker.new(hands).best_hand
137105
end
138106

139-
def test_two_fulls
107+
def test_aces_can_start_a_straight_a_2_3_4_5
140108
skip
141-
full_of_4_by_9 = %w[4H 4S 4D 9S 9D]
142-
full_of_5_by_8 = %w[5H 5S 5D 8S 8D]
143-
game = Poker.new([full_of_4_by_9, full_of_5_by_8])
144-
assert_equal [full_of_5_by_8], game.best_hand
109+
hands = [%w[4S 5H 4C 8D 4H], %w[4D AH 3S 2D 5C]]
110+
assert_equal [%w[4D AH 3S 2D 5C]], Poker.new(hands).best_hand
145111
end
146112

147-
def test_full_vs_square
113+
def test_aces_cannot_be_in_the_middle_of_a_straight_q_k_a_2_3
148114
skip
149-
full = %w[4S 5H 4D 5D 4H]
150-
square_of_3 = %w[3S 3H 2S 3D 3C]
151-
game = Poker.new([square_of_3, full])
152-
assert_equal [square_of_3], game.best_hand
115+
hands = [%w[2C 3D 7H 5H 2S], %w[QS KH AC 2D 3S]]
116+
assert_equal [%w[2C 3D 7H 5H 2S]], Poker.new(hands).best_hand
153117
end
154118

155-
def test_two_square
119+
def test_both_hands_with_a_straight_tie_goes_to_highest_ranked_card
156120
skip
157-
square_of_2 = %w[2S 2H 2C 8D 2D]
158-
square_of_5 = %w[4S 5H 5S 5D 5C]
159-
game = Poker.new([square_of_2, square_of_5])
160-
assert_equal [square_of_5], game.best_hand
121+
hands = [%w[4S 6C 7S 8D 5H], %w[5S 7H 8S 9D 6H]]
122+
assert_equal [%w[5S 7H 8S 9D 6H]], Poker.new(hands).best_hand
161123
end
162124

163-
def test_square_vs_straight_flush
125+
def test_even_though_an_ace_is_usually_high_a_5_high_straight_is_the_lowest_scoring_straight
164126
skip
165-
square_of_5 = %w[4S 5H 5S 5D 5C]
166-
straight_flush_to_10 = %w[7S 8S 9S 6S 10S]
167-
game = Poker.new([square_of_5, straight_flush_to_10])
168-
assert_equal [straight_flush_to_10], game.best_hand
127+
hands = [%w[2H 3C 4D 5D 6H], %w[4S AH 3S 2D 5H]]
128+
assert_equal [%w[2H 3C 4D 5D 6H]], Poker.new(hands).best_hand
169129
end
170130

171-
def test_two_straight_flushes
131+
def test_flush_beats_a_straight
172132
skip
173-
straight_flush_to_8 = %w[4H 6H 7H 8H 5H]
174-
straight_flush_to_9 = %w[5S 7S 8S 9S 6S]
175-
game = Poker.new([straight_flush_to_8, straight_flush_to_9])
176-
assert_equal [straight_flush_to_9], game.best_hand
133+
hands = [%w[4C 6H 7D 8D 5H], %w[2S 4S 5S 6S 7S]]
134+
assert_equal [%w[2S 4S 5S 6S 7S]], Poker.new(hands).best_hand
177135
end
178136

179-
def test_highest_card_down_to_fifth_card
137+
def test_both_hands_have_a_flush_tie_goes_to_high_card_down_to_the_last_one_if_necessary
180138
skip
181-
high_of_8_low_of_3 = %w[3S 5H 6S 8D 7H]
182-
high_of_8_low_of_2 = %w[2S 5D 6D 8C 7S]
183-
game = Poker.new([high_of_8_low_of_3, high_of_8_low_of_2])
184-
assert_equal [high_of_8_low_of_3], game.best_hand
139+
hands = [%w[4H 7H 8H 9H 6H], %w[2S 4S 5S 6S 7S]]
140+
assert_equal [%w[4H 7H 8H 9H 6H]], Poker.new(hands).best_hand
185141
end
186142

187-
def test_three_hand_with_tie
143+
def test_full_house_beats_a_flush
188144
skip
189-
spade_straight_to_9 = %w[9S 8S 7S 6S 5S]
190-
diamond_straight_to_9 = %w[9D 8D 7D 6D 5D]
191-
three_of_4 = %w[4D 4S 4H QS KS]
192-
hands = [spade_straight_to_9, diamond_straight_to_9, three_of_4]
193-
game = Poker.new(hands)
194-
assert_equal [spade_straight_to_9, diamond_straight_to_9], game.best_hand
145+
hands = [%w[3H 6H 7H 8H 5H], %w[4S 5H 4C 5D 4H]]
146+
assert_equal [%w[4S 5H 4C 5D 4H]], Poker.new(hands).best_hand
147+
end
148+
149+
def test_both_hands_have_a_full_house_tie_goes_to_highest_ranked_triplet
150+
skip
151+
hands = [%w[4H 4S 4D 9S 9D], %w[5H 5S 5D 8S 8D]]
152+
assert_equal [%w[5H 5S 5D 8S 8D]], Poker.new(hands).best_hand
153+
end
154+
155+
def test_with_multiple_decks_both_hands_have_a_full_house_with_the_same_triplet_tie_goes_to_the_pair
156+
skip
157+
hands = [%w[5H 5S 5D 9S 9D], %w[5H 5S 5D 8S 8D]]
158+
assert_equal [%w[5H 5S 5D 9S 9D]], Poker.new(hands).best_hand
159+
end
160+
161+
def test_four_of_a_kind_beats_a_full_house
162+
skip
163+
hands = [%w[4S 5H 4D 5D 4H], %w[3S 3H 2S 3D 3C]]
164+
assert_equal [%w[3S 3H 2S 3D 3C]], Poker.new(hands).best_hand
165+
end
166+
167+
def test_both_hands_have_four_of_a_kind_tie_goes_to_high_quad
168+
skip
169+
hands = [%w[2S 2H 2C 8D 2D], %w[4S 5H 5S 5D 5C]]
170+
assert_equal [%w[4S 5H 5S 5D 5C]], Poker.new(hands).best_hand
171+
end
172+
173+
def test_with_multiple_decks_both_hands_with_identical_four_of_a_kind_tie_determined_by_kicker
174+
skip
175+
hands = [%w[3S 3H 2S 3D 3C], %w[3S 3H 4S 3D 3C]]
176+
assert_equal [%w[3S 3H 4S 3D 3C]], Poker.new(hands).best_hand
177+
end
178+
179+
def test_straight_flush_beats_four_of_a_kind
180+
skip
181+
hands = [%w[4S 5H 5S 5D 5C], %w[7S 8S 9S 6S 10S]]
182+
assert_equal [%w[7S 8S 9S 6S 10S]], Poker.new(hands).best_hand
183+
end
184+
185+
def test_aces_can_end_a_straight_flush_10_j_q_k_a
186+
skip
187+
hands = [%w[KC AH AS AD AC], %w[10C JC QC KC AC]]
188+
assert_equal [%w[10C JC QC KC AC]], Poker.new(hands).best_hand
189+
end
190+
191+
def test_aces_can_start_a_straight_flush_a_2_3_4_5
192+
skip
193+
hands = [%w[KS AH AS AD AC], %w[4H AH 3H 2H 5H]]
194+
assert_equal [%w[4H AH 3H 2H 5H]], Poker.new(hands).best_hand
195+
end
196+
197+
def test_aces_cannot_be_in_the_middle_of_a_straight_flush_q_k_a_2_3
198+
skip
199+
hands = [%w[2C AC QC 10C KC], %w[QH KH AH 2H 3H]]
200+
assert_equal [%w[2C AC QC 10C KC]], Poker.new(hands).best_hand
201+
end
202+
203+
def test_both_hands_have_a_straight_flush_tie_goes_to_highest_ranked_card
204+
skip
205+
hands = [%w[4H 6H 7H 8H 5H], %w[5S 7S 8S 9S 6S]]
206+
assert_equal [%w[5S 7S 8S 9S 6S]], Poker.new(hands).best_hand
207+
end
208+
209+
def test_even_though_an_ace_is_usually_high_a_5_high_straight_flush_is_the_lowest_scoring_straight_flush
210+
skip
211+
hands = [%w[2H 3H 4H 5H 6H], %w[4D AD 3D 2D 5D]]
212+
assert_equal [%w[2H 3H 4H 5H 6H]], Poker.new(hands).best_hand
195213
end
196214
end

0 commit comments

Comments
 (0)