Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Program to find all upside down numbers of length n in Python
An upside down number (also called a strobogrammatic number) is a number that appears the same when rotated 180 degrees. The digits that remain valid when rotated are: 0, 1, 6, 8, and 9, where 6 becomes 9 and 9 becomes 6 when rotated.
So, if the input is like n = 2, then the output will be ['11', '69', '88', '96'].
Understanding Valid Digits
When rotated 180 degrees ?
0 ? 0
1 ? 1
6 ? 9
8 ? 8
9 ? 6
Algorithm
We use a recursive approach to build upside down numbers from the inside out ?
For length 0: return empty string
For length 1: return ['0', '1', '8'] (only digits that look the same rotated)
For longer lengths: recursively build the middle part and add valid digit pairs around it
Avoid leading zeros for the outermost layer
Implementation
class Solution:
def solve(self, n):
if not n:
return []
def middle(x=n):
if not x:
return [""]
if x == 1:
return list("018")
ret = []
mid = middle(x - 2)
for m in mid:
if x != n: # Not the outermost layer, can use 0
ret.append("0" + m + "0")
ret.append("1" + m + "1")
ret.append("6" + m + "9")
ret.append("8" + m + "8")
ret.append("9" + m + "6")
return ret
return sorted(middle())
# Test the solution
ob = Solution()
print("Length 1:", ob.solve(1))
print("Length 2:", ob.solve(2))
print("Length 3:", ob.solve(3))
Length 1: ['0', '1', '8'] Length 2: ['11', '69', '88', '96'] Length 3: ['101', '111', '181', '609', '619', '689', '808', '818', '888', '906', '916', '986']
How It Works
The algorithm builds numbers recursively from the center outward ?
Base cases: Length 0 returns empty string, length 1 returns single valid digits
Recursive case: For length x, get all valid numbers of length x-2
Add pairs: Surround each middle part with valid digit pairs (0-0, 1-1, 6-9, 8-8, 9-6)
Avoid leading zeros: Skip "0-0" pairs for the outermost layer to prevent leading zeros
Alternative Approach
Here's a simpler iterative approach ?
def find_upside_down_numbers(n):
if n == 0:
return []
# Valid pairs for building upside down numbers
pairs = [('0', '0'), ('1', '1'), ('6', '9'), ('8', '8'), ('9', '6')]
single = ['0', '1', '8'] # Valid single digits for odd lengths
if n == 1:
return single
result = []
def backtrack(current, remaining):
if remaining == 0:
result.append(current)
return
if remaining == 1: # Middle digit for odd length
for digit in single:
result.append(current + digit)
return
for left, right in pairs:
# Skip leading zeros
if len(current) == 0 and left == '0':
continue
backtrack(current + left, remaining - 2)
backtrack('', n)
return sorted(result)
# Test the function
print("Length 2:", find_upside_down_numbers(2))
print("Length 4:", find_upside_down_numbers(4))
Length 2: ['11', '69', '88', '96'] Length 4: ['1001', '1111', '1691', '1881', '1961', '6009', '6119', '6699', '6889', '6969', '8008', '8118', '8698', '8888', '8968', '9006', '9116', '9696', '9886', '9966']
Conclusion
Upside down numbers are built by pairing digits that remain valid when rotated 180 degrees. The recursive approach builds from the center outward, while avoiding leading zeros for valid numbers.
