1
exercism fetch go poker

cases_test.go

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
package poker

// Source: exercism/problem-specifications
// Commit: d078ba8 poker: Add canonical-data (#793)
// Problem Specifications Version: 1.0.0

type validTestCase struct {
	name  string
	hands []string
	best  []string
}

var validTestCases = []validTestCase{
	{
		name: "single hand always wins",
		hands: []string{
			"4♤ 5♤ 7♡ 8♢ J♧",
		},
		best: []string{"4♤ 5♤ 7♡ 8♢ J♧"},
	},
	{
		name: "highest card out of all hands wins",
		hands: []string{
			"4♢ 5♤ 6♤ 8♢ 3♧",
			"2♤ 4♧ 7♤ 9♡ 10♡",
			"3♤ 4♤ 5♢ 6♡ J♡",
		},
		best: []string{"3♤ 4♤ 5♢ 6♡ J♡"},
	},
	{
		name: "a tie has multiple winners",
		hands: []string{
			"4♢ 5♤ 6♤ 8♢ 3♧",
			"2♤ 4♧ 7♤ 9♡ 10♡",
			"3♤ 4♤ 5♢ 6♡ J♡",
			"3♡ 4♡ 5♧ 6♧ J♢",
		},
		best: []string{"3♤ 4♤ 5♢ 6♡ J♡", "3♡ 4♡ 5♧ 6♧ J♢"},
	},
	{
		name: "multiple hands with the same high cards, tie compares next highest ranked, down to last card",
		hands: []string{
			"3♤ 5♡ 6♤ 8♢ 7♡",
			"2♤ 5♢ 6♢ 8♧ 7♤",
		},
		best: []string{"3♤ 5♡ 6♤ 8♢ 7♡"},
	},
	{
		name: "one pair beats high card",
		hands: []string{
			"4♤ 5♡ 6♧ 8♢ K♡",
			"2♤ 4♡ 6♤ 4♢ J♡",
		},
		best: []string{"2♤ 4♡ 6♤ 4♢ J♡"},
	},
	{
		name: "highest pair wins",
		hands: []string{
			"4♤ 2♡ 6♤ 2♢ J♡",
			"2♤ 4♡ 6♧ 4♢ J♢",
		},
		best: []string{"2♤ 4♡ 6♧ 4♢ J♢"},
	},
	{
		name: "two pairs beats one pair",
		hands: []string{
			"2♤ 8♡ 6♤ 8♢ J♡",
			"4♤ 5♡ 4♧ 8♧ 5♧",
		},
		best: []string{"4♤ 5♡ 4♧ 8♧ 5♧"},
	},
	{
		name: "both hands have two pairs, highest ranked pair wins",
		hands: []string{
			"2♤ 8♡ 2♢ 8♢ 3♡",
			"4♤ 5♡ 4♧ 8♤ 5♢",
		},
		best: []string{"2♤ 8♡ 2♢ 8♢ 3♡"},
	},
	{
		name: "both hands have two pairs, with the same highest ranked pair, tie goes to low pair",
		hands: []string{
			"2♤ Q♤ 2♧ Q♢ J♡",
			"J♢ Q♡ J♤ 8♢ Q♧",
		},
		best: []string{"J♢ Q♡ J♤ 8♢ Q♧"},
	},
	{
		name: "both hands have two identically ranked pairs, tie goes to remaining card (kicker)",
		hands: []string{
			"J♢ Q♡ J♤ 8♢ Q♧",
			"J♤ Q♤ J♧ 2♢ Q♢",
		},
		best: []string{"J♢ Q♡ J♤ 8♢ Q♧"},
	},
	{
		name: "three of a kind beats two pair",
		hands: []string{
			"2♤ 8♡ 2♡ 8♢ J♡",
			"4♤ 5♡ 4♧ 8♤ 4♡",
		},
		best: []string{"4♤ 5♡ 4♧ 8♤ 4♡"},
	},
	{
		name: "both hands have three of a kind, tie goes to highest ranked triplet",
		hands: []string{
			"2♤ 2♡ 2♧ 8♢ J♡",
			"4♤ A♡ A♤ 8♧ A♢",
		},
		best: []string{"4♤ A♡ A♤ 8♧ A♢"},
	},
	{
		name: "with multiple decks, two players can have same three of a kind, ties go to highest remaining cards",
		hands: []string{
			"4♤ A♡ A♤ 7♧ A♢",
			"4♤ A♡ A♤ 8♧ A♢",
		},
		best: []string{"4♤ A♡ A♤ 8♧ A♢"},
	},
	{
		name: "a straight beats three of a kind",
		hands: []string{
			"4♤ 5♡ 4♧ 8♢ 4♡",
			"3♤ 4♢ 2♤ 6♢ 5♧",
		},
		best: []string{"3♤ 4♢ 2♤ 6♢ 5♧"},
	},
	{
		name: "aces can end a straight (10 J Q K A)",
		hands: []string{
			"4♤ 5♡ 4♧ 8♢ 4♡",
			"10♢ J♡ Q♤ K♢ A♧",
		},
		best: []string{"10♢ J♡ Q♤ K♢ A♧"},
	},
	{
		name: "aces can start a straight (A 2 3 4 5)",
		hands: []string{
			"4♤ 5♡ 4♧ 8♢ 4♡",
			"4♢ A♡ 3♤ 2♢ 5♧",
		},
		best: []string{"4♢ A♡ 3♤ 2♢ 5♧"},
	},
	{
		name: "both hands with a straight, tie goes to highest ranked card",
		hands: []string{
			"4♤ 6♧ 7♤ 8♢ 5♡",
			"5♤ 7♡ 8♤ 9♢ 6♡",
		},
		best: []string{"5♤ 7♡ 8♤ 9♢ 6♡"},
	},
	{
		name: "even though an ace is usually high, a 5-high straight is the lowest-scoring straight",
		hands: []string{
			"2♡ 3♧ 4♢ 5♢ 6♡",
			"4♤ A♡ 3♤ 2♢ 5♡",
		},
		best: []string{"2♡ 3♧ 4♢ 5♢ 6♡"},
	},
	{
		name: "flush beats a straight",
		hands: []string{
			"4♧ 6♡ 7♢ 8♢ 5♡",
			"2♤ 4♤ 5♤ 6♤ 7♤",
		},
		best: []string{"2♤ 4♤ 5♤ 6♤ 7♤"},
	},
	{
		name: "both hands have a flush, tie goes to high card, down to the last one if necessary",
		hands: []string{
			"4♡ 7♡ 8♡ 9♡ 6♡",
			"2♤ 4♤ 5♤ 6♤ 7♤",
		},
		best: []string{"4♡ 7♡ 8♡ 9♡ 6♡"},
	},
	{
		name: "full house beats a flush",
		hands: []string{
			"3♡ 6♡ 7♡ 8♡ 5♡",
			"4♤ 5♡ 4♧ 5♢ 4♡",
		},
		best: []string{"4♤ 5♡ 4♧ 5♢ 4♡"},
	},
	{
		name: "both hands have a full house, tie goes to highest-ranked triplet",
		hands: []string{
			"4♡ 4♤ 4♢ 9♤ 9♢",
			"5♡ 5♤ 5♢ 8♤ 8♢",
		},
		best: []string{"5♡ 5♤ 5♢ 8♤ 8♢"},
	},
	{
		name: "with multiple decks, both hands have a full house with the same triplet, tie goes to the pair",
		hands: []string{
			"5♡ 5♤ 5♢ 9♤ 9♢",
			"5♡ 5♤ 5♢ 8♤ 8♢",
		},
		best: []string{"5♡ 5♤ 5♢ 9♤ 9♢"},
	},
	{
		name: "four of a kind beats a full house",
		hands: []string{
			"4♤ 5♡ 4♢ 5♢ 4♡",
			"3♤ 3♡ 2♤ 3♢ 3♧",
		},
		best: []string{"3♤ 3♡ 2♤ 3♢ 3♧"},
	},
	{
		name: "both hands have four of a kind, tie goes to high quad",
		hands: []string{
			"2♤ 2♡ 2♧ 8♢ 2♢",
			"4♤ 5♡ 5♤ 5♢ 5♧",
		},
		best: []string{"4♤ 5♡ 5♤ 5♢ 5♧"},
	},
	{
		name: "with multiple decks, both hands with identical four of a kind, tie determined by kicker",
		hands: []string{
			"3♤ 3♡ 2♤ 3♢ 3♧",
			"3♤ 3♡ 4♤ 3♢ 3♧",
		},
		best: []string{"3♤ 3♡ 4♤ 3♢ 3♧"},
	},
	{
		name: "straight flush beats four of a kind",
		hands: []string{
			"4♤ 5♡ 5♤ 5♢ 5♧",
			"7♤ 8♤ 9♤ 6♤ 10♤",
		},
		best: []string{"7♤ 8♤ 9♤ 6♤ 10♤"},
	},
	{
		name: "both hands have straight flush, tie goes to highest-ranked card",
		hands: []string{
			"4♡ 6♡ 7♡ 8♡ 5♡",
			"5♤ 7♤ 8♤ 9♤ 6♤",
		},
		best: []string{"5♤ 7♤ 8♤ 9♤ 6♤"},
	},
}

poker_test.go

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package poker

import (
	"reflect"
	"testing"
)

// Define a function BestHand([]string) ([]string, error).

var invalidTestCases = []struct {
	name string
	hand string
}{
	{
		name: "1 is an invalid card rank",
		hand: "1♢ 2♡ 3♡ 4♡ 5♡",
	},
	{
		name: "11 is an invalid card rank",
		hand: "11♢ 2♡ 3♡ 4♡ 5♡",
	},
	{
		name: "too few cards",
		hand: "2♡ 3♡ 4♡ 5♡",
	},
	{
		name: "too many cards",
		hand: "2♡ 3♡ 4♡ 5♡ 6♡ 7♡",
	},
	{
		name: "lack of rank",
		hand: "11♢ 2♡ ♡ 4♡ 5♡",
	},
	{
		name: "lack of suit",
		hand: "2♡ 3♡ 4 5♡ 7♡",
	},
	{
		name: "H is an invalid suit",
		hand: "2♡ 3♡ 4H 5♡ 7♡",
	},
	{
		name: "♥ is an invalid suit",
		hand: "2♡ 3♡ 4♥ 5♡ 7♡",
	},
	{
		name: "lack of spacing",
		hand: "2♡ 3♡ 5♡7♡ 8♡",
	},
	{
		name: "double suits after rank",
		hand: "2♡ 3♡ 5♡♡ 8♡ 9♡",
	},
}

func TestBestHandValid(t *testing.T) {
	for _, tt := range validTestCases {
		actual, err := BestHand(tt.hands)
		if err != nil {
			var _ error = err
			t.Fatalf("Got unexpected error in valid case %q: %v", tt.name, err)
		}
		if !reflect.DeepEqual(actual, tt.best) {
			t.Fatalf("Mismatch in result of valid case %q: got %#v, want %#v",
				tt.name, actual, tt.best)
		}
	}
}

func TestBestHandInvalid(t *testing.T) {
	for _, tt := range invalidTestCases {
		_, err := BestHand([]string{tt.hand})
		if err == nil {
			t.Fatalf("Did not get an error for invalid case %q", tt.name)
		}
	}
}

func BenchmarkBestHand(b *testing.B) {
	for i := 0; i < b.N; i++ {
		for _, tt := range validTestCases {
			BestHand(tt.hands)
		}
	}
}