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 d8bac59

Browse files
author
openset
committed
Add: Ugly Number
1 parent 9d4849b commit d8bac59

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

‎problems/ugly-number/ugly_number.go

Lines changed: 12 additions & 0 deletions
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+
}
Lines changed: 42 additions & 0 deletions
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)

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