-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathnumberSystem.py
More file actions
43 lines (33 loc) · 1.12 KB
/
numberSystem.py
File metadata and controls
43 lines (33 loc) · 1.12 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
def numberSystem(digits, n):
from math import log
base = len(digits)
power = int(log(n)/log(base))
human_digits = []
if 0 in digits:
n -= 1
while power >= 0:
current_digit = int(n/(base**power))
human_digits.append(current_digit)
n -= (current_digit * base**power)
power -= 1
if 0 not in digits and 0 in human_digits:
non0_digits = [human_digits[0]-1]
non0_digits += [base - 1 + d for d in human_digits[1:]]
non0_digits[-1] += 1
non0_digits = non0_digits[::-1]
carry = 0
for i, d in enumerate(non0_digits):
d += carry
carry = int((d-1)/base)
non0_digits[i] = d - carry * base
non0_digits = non0_digits[::-1]
alien_digits = [str(digits[d-1]) for d in non0_digits if d != 0]
elif 0 not in digits:
alien_digits = [str(digits[d-1]) for d in human_digits]
else:
alien_digits = [str(digits[d]) for d in human_digits]
return(''.join(alien_digits))
if __name__=='__main__':
digits = [4, 7, 8]
n = 3274653
print(numberSystem(digits, n))