Skip to content

Commit 057cbd2

Browse files
author
Shuo
authored
Merge pull request #709 from openset/develop
Add: Ugly Number
2 parents 96e947a + d8bac59 commit 057cbd2

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

Diff for: problems/ugly-number/ugly_number.go

+12
Original file line numberDiff line numberDiff line change
@@ -1 +1,13 @@
11
package problem263
2+
3+
func isUgly(num int) bool {
4+
if num < 1 {
5+
return false
6+
}
7+
for _, n := range [...]int{2, 3, 5} {
8+
for num%n == 0 {
9+
num /= n
10+
}
11+
}
12+
return num == 1
13+
}

Diff for: problems/ugly-number/ugly_number_test.go

+42
Original file line numberDiff line numberDiff line change
@@ -1 +1,43 @@
11
package problem263
2+
3+
import "testing"
4+
5+
type caseType struct {
6+
input int
7+
expected bool
8+
}
9+
10+
func TestIsUgly(t *testing.T) {
11+
tests := [...]caseType{
12+
{
13+
input: 1,
14+
expected: true,
15+
},
16+
{
17+
input: 6,
18+
expected: true,
19+
},
20+
{
21+
input: 8,
22+
expected: true,
23+
},
24+
{
25+
input: 14,
26+
expected: false,
27+
},
28+
{
29+
input: 0,
30+
expected: false,
31+
},
32+
{
33+
input: -30,
34+
expected: false,
35+
},
36+
}
37+
for _, tc := range tests {
38+
output := isUgly(tc.input)
39+
if output != tc.expected {
40+
t.Fatalf("input: %v, output: %v, expected: %v", tc.input, output, tc.expected)
41+
}
42+
}
43+
}

0 commit comments

Comments
 (0)