-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathbases.py
47 lines (38 loc) · 1.07 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
#!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
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
def convert(str_num, base1, base2):
"""
Convert given number from base1 to base2.
"""
assert 2 <= base1 <= 36
assert 2 <= base2 <= 36
# TODO: Convert number
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()