- Notifications
You must be signed in to change notification settings - Fork 46.7k
/
Copy pathdecimal_to_any.py
105 lines (96 loc) · 3.11 KB
/
decimal_to_any.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
101
102
103
104
105
"""Convert a positive Decimal Number to Any Other Representation"""
fromstringimportascii_uppercase
ALPHABET_VALUES= {str(ord(c) -55): cforcinascii_uppercase}
defdecimal_to_any(num: int, base: int) ->str:
"""
Convert a positive integer to another base as str.
>>> decimal_to_any(0, 2)
'0'
>>> decimal_to_any(5, 4)
'11'
>>> decimal_to_any(20, 3)
'202'
>>> decimal_to_any(58, 16)
'3A'
>>> decimal_to_any(243, 17)
'E5'
>>> decimal_to_any(34923, 36)
'QY3'
>>> decimal_to_any(10, 11)
'A'
>>> decimal_to_any(16, 16)
'10'
>>> decimal_to_any(36, 36)
'10'
>>> # negatives will error
>>> decimal_to_any(-45, 8) # doctest: +ELLIPSIS
Traceback (most recent call last):
...
ValueError: parameter must be positive int
>>> # floats will error
>>> decimal_to_any(34.4, 6) # doctest: +ELLIPSIS
Traceback (most recent call last):
...
TypeError: int() can't convert non-string with explicit base
>>> # a float base will error
>>> decimal_to_any(5, 2.5) # doctest: +ELLIPSIS
Traceback (most recent call last):
...
TypeError: 'float' object cannot be interpreted as an integer
>>> # a str base will error
>>> decimal_to_any(10, '16') # doctest: +ELLIPSIS
Traceback (most recent call last):
...
TypeError: 'str' object cannot be interpreted as an integer
>>> # a base less than 2 will error
>>> decimal_to_any(7, 0) # doctest: +ELLIPSIS
Traceback (most recent call last):
...
ValueError: base must be >= 2
>>> # a base greater than 36 will error
>>> decimal_to_any(34, 37) # doctest: +ELLIPSIS
Traceback (most recent call last):
...
ValueError: base must be <= 36
"""
ifisinstance(num, float):
raiseTypeError("int() can't convert non-string with explicit base")
ifnum<0:
raiseValueError("parameter must be positive int")
ifisinstance(base, str):
raiseTypeError("'str' object cannot be interpreted as an integer")
ifisinstance(base, float):
raiseTypeError("'float' object cannot be interpreted as an integer")
ifbasein (0, 1):
raiseValueError("base must be >= 2")
ifbase>36:
raiseValueError("base must be <= 36")
new_value=""
mod=0
div=0
whilediv!=1:
div, mod=divmod(num, base)
ifbase>=11and9<mod<36:
actual_value=ALPHABET_VALUES[str(mod)]
else:
actual_value=str(mod)
new_value+=actual_value
div=num//base
num=div
ifdiv==0:
returnstr(new_value[::-1])
elifdiv==1:
new_value+=str(div)
returnstr(new_value[::-1])
returnnew_value[::-1]
if__name__=="__main__":
importdoctest
doctest.testmod()
forbaseinrange(2, 37):
fornuminrange(1000):
assertint(decimal_to_any(num, base), base) ==num, (
num,
base,
decimal_to_any(num, base),
int(decimal_to_any(num, base), base),
)