Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 94f317b

Browse files
committed
Add: X of a Kind in a Deck of Cards
1 parent d798f4f commit 94f317b

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,20 @@
11
package x_of_a_kind_in_a_deck_of_cards
2+
3+
func hasGroupsSizeX(deck []int) bool {
4+
m, l := make(map[int]int), len(deck)
5+
for _, v := range deck {
6+
m[v]++
7+
}
8+
flag:
9+
for x := 2; x <= l; x++ {
10+
if l%x == 0 {
11+
for _, v := range m {
12+
if v%x != 0 {
13+
continue flag
14+
}
15+
}
16+
return true
17+
}
18+
}
19+
return false
20+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,39 @@
11
package x_of_a_kind_in_a_deck_of_cards
2+
3+
import "testing"
4+
5+
type caseType struct {
6+
input []int
7+
expected bool
8+
}
9+
10+
func TestHasGroupsSizeX(t *testing.T) {
11+
tests := [...]caseType{
12+
{
13+
input: []int{1, 2, 3, 4, 4, 3, 2, 1},
14+
expected: true,
15+
},
16+
{
17+
input: []int{1, 1, 1, 2, 2, 2, 3, 3},
18+
expected: false,
19+
},
20+
{
21+
input: []int{1},
22+
expected: false,
23+
},
24+
{
25+
input: []int{1, 1},
26+
expected: true,
27+
},
28+
{
29+
input: []int{1, 1, 2, 2, 2, 2},
30+
expected: true,
31+
},
32+
}
33+
for _, tc := range tests {
34+
output := hasGroupsSizeX(tc.input)
35+
if output != tc.expected {
36+
t.Fatalf("input: %v, output: %v, expected: %v", tc.input, output, tc.expected)
37+
}
38+
}
39+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /