-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path__init__.py
More file actions
27 lines (21 loc) · 734 Bytes
/
__init__.py
File metadata and controls
27 lines (21 loc) · 734 Bytes
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
from datastructures.stacks.dynamic import DynamicSizeStack as Stack
def convert_int_to_bin(dec_num: int) -> int:
"""
Converts a decimal integer to binary using a stack data structure. Uses the division by 2 method to perform the
conversion. Each remainder from the division is stored on the stack and popped off until we have the binary
number
Args:
dec_num (int): Decimal Number
Return:
int: binary representation
"""
stack = Stack()
current = dec_num
while current != 0:
current, rem = divmod(current, 2)
stack.push(rem)
bin_num = ""
while not stack.is_empty():
binary = stack.pop()
bin_num += str(binary)
return int(bin_num)