-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path#2-1.py
More file actions
76 lines (65 loc) · 1.48 KB
/
Copy path#2-1.py
File metadata and controls
76 lines (65 loc) · 1.48 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#solution of #2-1
def convert2decimal(n, k, b):
r = 0
for i in range(k):
r+= int(n[k-i-1])*pow(b,i)
return r
def convert2origin(decimal, k, b):
r = ""
for i in range(k-1):
decimal, remain = decimal//b , decimal%b
if decimal:
r = str(remain) + r
if decimal < b:
r = str(decimal) + r
else:
r = '0' + r
return r
#Check for duplicates
def check(z, tmp, result):
r = 0
if (z in tmp):
if len(result) and z==result[0]:
r = 1
else:
result.append(z)
tmp.append(z)
return r
#When input is decimal
def loop_decimal(n, k, b, tmp, result):
x = "".join(sorted(n, reverse=True))
y = "".join(sorted(n))
z = str(int(x) - int(y))
for i in range(k-len(z)):
z = '0' + z
if check(z, tmp, result):
return len(result)
return loop_decimal(z, k, b, tmp, result)
#When input isn't decimal
def loop_normal(n, k, b, tmp, result):
x = "".join(sorted(n, reverse=True))
y = "".join(sorted(n))
z = convert2origin((convert2decimal(x, k, b) - convert2decimal(y, k, b)), k, b)
if check(z, tmp, result):
return len(result)
return loop_normal(z, k, b, tmp, result)
#main fuction
def solution(n, b):
#Your code here
k = len(n)
tmp = []
result = []
r = 0
try:
if b == 10:
r = loop_decimal(n, k, b, tmp, result)
else:
r = loop_normal(n, k, b, tmp, result)
except:
r = 0
return r
#Test
#if __name__ == '__main__':
#n = '210022'
#b = 3
#solution(n, b)