GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — coding-problems/Other/jumping_numbers.py at master · CROXX-Code/coding-problems

Expand file tree

/

jumping_numbers.py

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
61 lines (44 loc) · 1.58 KB
/

jumping_numbers.py

Copy path

File metadata and controls

61 lines (44 loc) · 1.58 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
'''
Jumping numbers
A number is called as a Jumping Number if all adjacent digits in it differ by 1.
The difference between ‘9’ and ‘0’ is not considered as 1.
All single digit numbers are considered as Jumping Numbers.
For example 7, 8987 and 4343456 are Jumping numbers but 796 and 89098 are not.
Input: 20
Output: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12]
=========================================
Make a tree (DFS way/backtracking), for each next digit take the last digit, go up and down
(example: 123, last digit is 3, so next digit should be 2 or 4).
Time Complexity: O(9 * 2^(NumOfDigits(N) - 1))
Space Complexity: O(1) , recursion stack will have depth 9 (but this can be considered as constant)
'''
############
# Solution #
############
def jumping_numbers(x):
result = []
# take all 9 possible starting combinations
for i in range(1, 10):
jumping_num(i, x, result)
return result
def jumping_num(num, x, result):
if num > x:
return
result.append(num)
last_digit = num % 10
next_num = num * 10
# decrease the last digit by one
if last_digit != 0:
jumping_num(next_num + last_digit - 1, x, result)
# increase the last digit by one
if last_digit != 9:
jumping_num(next_num + last_digit + 1, x, result)
###########
# Testing #
###########
# Test 1
# Correct result => [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12]
print(jumping_numbers(20))
# Test 2
# Correct result => [1, 10, 12, 2, 21, 23, 3, 32, 34, 4, 43, 45, 5, 54, 56, 6, 65, 67, 7, 76, 78, 8, 87, 89, 9, 98]
print(jumping_numbers(100))
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.