forked from winner9871/the-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrep_to_bin.py
32 lines (25 loc) · 876 Bytes
/
rep_to_bin.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
def lookup_table_creater(BASE):
table = {}
for digit in range(BASE):
if digit <= 9: representation = str(digit)
else: representation = chr(digit + 96)
table[representation] = digit
return table
def repToDecimal(rep, BASE):
table = lookup_table_creater(BASE)
if "-" == rep[0]:
sign = -1
rep = rep[1:]
else:
sign = 1
decimalEquivalent = 0
for index, digitRep in enumerate(rep[::-1]):
decimalEquivalent += table[digitRep] * (BASE ** index)
return decimalEquivalent * sign
if __name__ == "__main__":
BASE = int(input("Enter The Base:\t"))
while BASE < 1:
print("Base must be greater than equal to 1")
BASE = int(input("Enter The Base:\t"))
rep = input("Enter The Representation :\t")
print("Decimal Equivalent :\t", repToDecimal(rep, BASE))