Skip to content

Commit 06989a6

Browse files
author
Yan Cao
committed
Added rotated mirror number
1 parent 1da8d73 commit 06989a6

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

Interviews/Rotated_Mirror_Number.py

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
"""
2+
####From Alec's email, someone's onsite interview with Facebook for finding rotated mirrow number like 808 which is less than N
3+
4+
"""
5+
6+
def rotated_mirror_number(n):
7+
length = 0
8+
while n - 10**length > 0:
9+
length += 1
10+
ret = []
11+
rotated_helper(n, length, [], ret)
12+
return ret
13+
14+
def rotated_helper(n, length, res, ret):
15+
num = convert_to_num(res)
16+
if num > n or len(res) > length:
17+
return
18+
if len(res) > 0 and res[0] != 0:
19+
ret.append(num)
20+
if len(res) == 0:
21+
for i in range(10):
22+
res.append(i)
23+
rotated_helper(n, length, res, ret)
24+
res.pop()
25+
26+
for i in range(10):
27+
res.append(i)
28+
res.insert(0,i)
29+
rotated_helper(n, length, res, ret)
30+
res.pop()
31+
res.pop(0)
32+
33+
def convert_to_num(int_list):
34+
res = 0
35+
for digit in int_list:
36+
res = res*10 + digit
37+
return res
38+
39+
40+
print rotated_mirror_number(10000)

0 commit comments

Comments
 (0)