forked from MakeSchool-18/Data-Structures
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbases.py
100 lines (91 loc) · 2.39 KB
/
bases.py
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#!python
import string
def decode(str_num, base):
"""
Decode given number from given base to base 10.
str_num -- string representation of number in given base
base -- base of given number
"""
assert 2 <= base <= 36
# TODO: Decode number
result = 0
for digit in range(1, len(str_num) + 1):
value = str_num[-digit]
if ord(value) >= 97 and ord(value) <= 172:
value = int(ord(value) - 87)
elif ord(value) >= 65 and ord(value) <= 90:
value = int(ord(value) - 55)
else:
value = int(value)
placeholder = digit - 1
result += value * base ** placeholder
return result
def encode(num, base):
"""
Encode given number from base 10 to given base.
num -- the number in base 10
base -- base to convert to
"""
assert 2 <= base <= 36
# TODO: Encode number
num_rep={10:'a',
11:'b',
12:'c',
13:'d',
14:'e',
15:'f',
16:'g',
17:'h',
18:'i',
19:'j',
20:'k',
21:'l',
22:'m',
23:'n',
24:'o',
25:'p',
26:'q',
27:'r',
28:'s',
29:'t',
30:'u',
31:'v',
32:'w',
33:'x',
34:'y',
35:'z'}
result = ''
current = num
while current != 0:
remainder = current % base
if 36 > remainder > 9:
remainder_string = num_rep[remainder]
elif remainder >= 36:
remainder_string = '(' + str(remainder) + ')'
else:
remainder_string = str(remainder)
result += remainder_string
current = current / base
return result
def convert(str_num, base1, base2):
"""
Convert given number from base1 to base2.
"""
assert 2 <= base1 <= 36
assert 2 <= base2 <= 36
# TODO: Convert number
in_base_ten = decode(str_num, base1)
return encode(in_base_ten, base2)
def main():
import sys
args = sys.argv[1:] # Ignore script file name
if len(args) == 3:
str_num = args[0]
base1 = int(args[1])
base2 = int(args[2])
result = convert(str_num, base1, base2)
print('{} in base {} is {} in base {}'.format(str_num, base1, result, base2))
else:
print('Usage: {} number base1 base2'.format(sys.argv[0]))
if __name__ == '__main__':
main()