-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdec2Binary.py
More file actions
46 lines (35 loc) · 1.05 KB
/
dec2Binary.py
File metadata and controls
46 lines (35 loc) · 1.05 KB
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
__author__ = 'sarvps'
'''
Author: Sarv Parteek Singh
Course: CISC 610
Term: Late Summer
Data Structures & Algorithms by Miller
Stack assignment, Problem 1
Brief: Decimal to Binary convertor
References: Sections 3.5,3.8 from Miller
'''
class Stack:
def __init__(self):
self.items = []
def isEmpty(self):
return self.items == []
def push(self, item):
self.items.append(item)
def pop(self):
return self.items.pop()
def peek(self):
return self.items[len(self.items)-1]
def size(self):
return len(self.items)
def decToBin(decNumber):
remstack = Stack()
while decNumber > 0:
rem = decNumber % 2
remstack.push(rem)
decNumber = decNumber // 2
binString = ""
while not remstack.isEmpty():
binString = binString + str(remstack.pop())
return binString # if the output is less than 32 characters, the user can use this
# as a number by doing int(decToBin(decNumber))
#print(decToBin(2561)) #Test