-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinary.py
48 lines (32 loc) · 910 Bytes
/
binary.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
from stack import Stack
def divide_by2(decimal):
rem_stack = Stack()
while decimal > 0:
rem = decimal % 2
rem_stack.push(rem)
decimal = decimal // 2
binary = ''
while not rem_stack.is_empty():
binary = binary + str(rem_stack.pop())
return binary
def base_converter(decimal, base):
digits = "0123456789ABCDEF"
rem_stack = Stack()
while decimal > 0:
rem = decimal % base
rem_stack.push(rem)
decimal = decimal // base
new_string = ''
while not rem_stack.is_empty():
new_string = new_string + digits[rem_stack.pop()]
return new_string
def main():
print(divide_by2(42))
print(divide_by2(233))
def main1():
print(base_converter(25, 2))
print(base_converter(31, 16))
print(base_converter(25, 8))
print(base_converter(256, 16))
print(base_converter(26, 26))
main1()