forked from adnanaziz/EPIJudge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvert_base.py
24 lines (18 loc) · 857 Bytes
/
convert_base.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
import functools
import string
from test_framework import generic_test
def convert_base(num_as_string: str, b1: int, b2: int) -> str:
def construct_from_base(num_as_int, base):
return ('' if num_as_int == 0 else
construct_from_base(num_as_int // base, base) +
string.hexdigits[num_as_int % base].upper())
is_negative = num_as_string[0] == '-'
num_as_int = functools.reduce(
lambda x, c: x * b1 + string.hexdigits.index(c.lower()),
num_as_string[is_negative:], 0)
return ('-' if is_negative else '') + ('0' if num_as_int == 0 else
construct_from_base(num_as_int, b2))
if __name__ == '__main__':
exit(
generic_test.generic_test_main('convert_base.py', 'convert_base.tsv',
convert_base))