Skip to content

Commit 4a4d11a

Browse files
author
Shuo
authored
Merge pull request #255 from openset/develop
Add: Self Dividing Numbers
2 parents a775677 + 837375f commit 4a4d11a

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,15 @@
11
package self_dividing_numbers
2+
3+
func selfDividingNumbers(left int, right int) []int {
4+
ans := make([]int, 0)
5+
flag:
6+
for i := left; i <= right; i++ {
7+
for x := i; x > 0; x /= 10 {
8+
if d := x % 10; d == 0 || i%d != 0 {
9+
continue flag
10+
}
11+
}
12+
ans = append(ans, i)
13+
}
14+
return ans
15+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,28 @@
11
package self_dividing_numbers
2+
3+
import (
4+
"reflect"
5+
"testing"
6+
)
7+
8+
type caseType struct {
9+
left int
10+
right int
11+
expected []int
12+
}
13+
14+
func TestSelfDividingNumbers(t *testing.T) {
15+
tests := [...]caseType{
16+
{
17+
left: 1,
18+
right: 22,
19+
expected: []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 15, 22},
20+
},
21+
}
22+
for _, tc := range tests {
23+
output := selfDividingNumbers(tc.left, tc.right)
24+
if !reflect.DeepEqual(output, tc.expected) {
25+
t.Fatalf("input: %v %v, output: %v, expected: %v", tc.left, tc.right, output, tc.expected)
26+
}
27+
}
28+
}

0 commit comments

Comments
 (0)