-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVersion2.0.py
More file actions
42 lines (36 loc) · 1.84 KB
/
Version2.0.py
File metadata and controls
42 lines (36 loc) · 1.84 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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from functools import reduce
def find_black_hole_number(num):
def sort_digits(temp, biggest=True): # To list the number, sort the list and turn it back into a number again.
return reduce(lambda y, z: y*10+z, sorted(map(lambda x: int(x), list(str(temp))), reverse=biggest))
global calculated
path = [num] # If we find a number in path, it means a black hole number(group) has occur.
while True: # Run until find black hole number
largest_version = sort_digits(num)
smallest_version = sort_digits(num, biggest=False)
num = largest_version-smallest_version
if not num: # Deal with numbers such as 1111.
return 0
elif num < start: # Four-digit number 1000, f(1000)=1000-1=999, not a four-digit number
num = num*10
calculated.append(num)
elif num in path:
return num # We have find a black hole number(group), at the same time break the loop.
elif num in calculated:
return 0
else:
path.append(num)
calculated.append(num)
if __name__ == '__main__':
digits = int(input('Please enter the number of digits:'))
start = 10**(digits-1) # For example, the start of three-digit number is 10**(3-1)=100
end = 10**digits-1 # For example, the end of three-digit number is 10**3-1=999
blackHoleNumbers = set() # Use set means we don't have to deduplicate later.
calculated = list()
for number in range(start, end+1):
if number not in calculated:
blackHoleNumbers.add(find_black_hole_number(number))
calculated.append(number)
blackHoleNumbers.remove(0) # Remove "fake" black hole number 0 caused by those numbers with no different digits.
print(blackHoleNumbers)