-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathnumbers-with-same-consecutive-differences.py
More file actions
42 lines (31 loc) · 1.07 KB
/
numbers-with-same-consecutive-differences.py
File metadata and controls
42 lines (31 loc) · 1.07 KB
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
class Solution:
def numsSameConsecDiff(self, N, K):
"""
:type N: int
:type K: int
:rtype: List[int]
"""
if N == 1:
return list(range(10))
result = []
def dfs(buff, left):
if left == 0:
result.append(buff)
return
for diff in set([buff % 10 - K, buff % 10 + K]):
if 0 <= diff < 10:
dfs(buff * 10 + diff, left - 1)
for start in range(1, 10):
dfs(start, N - 1)
return result
class TestSolution:
def setup(self):
self.sol = Solution()
def test_case1(self):
assert self.sol.numsSameConsecDiff(3, 7) == [181,292,707,818,929]
def test_case2(self):
assert self.sol.numsSameConsecDiff(2, 1) == [10,12,21,23,32,34,43,45,54,56,65,67,76,78,87,89,98]
def test_case3(self):
assert self.sol.numsSameConsecDiff(1, 0) == [0,1,2,3,4,5,6,7,8,9]
def test_case2(self):
assert self.sol.numsSameConsecDiff(2, 0) == [11,22,33,44,55,66,77,88,99]