-
Notifications
You must be signed in to change notification settings - Fork 4
/
StackAnotherExample.py
54 lines (36 loc) · 1.2 KB
/
StackAnotherExample.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
__author__ = "Mateen";
__descr__ = "Convert any number to any base using stack"
import Stack;
def decimalToBinary(decimalNumber):
# decimalNumber = str(decimalNumber)
s = Stack.Stack();
while True and decimalNumber > 0 :
remainder = decimalNumber % 2;
s.push(remainder);
decimalNumber = decimalNumber // 2;
return s.getAllItemsReverse();
def BaseConvertor(decimalNumber, base):
remainderStack = Stack.Stack();
while decimalNumber > 0:
# if decimalNumber == base:
# remainderStack.push(0);
# decimalNumber =0;
# else:
remainder = decimalNumber % base;
remainderStack.push(remainder);
decimalNumber = decimalNumber // base;
baseString = None;
if base == 16:
baseString = "0123456789ABCDEF";
if base == 8:
baseString = "012345678";
if base == 26:
baseString = "0123456789abcdefghijklmnopqrstuvwxyz";
retString = "";
while not remainderStack.isEmpty():
retString = retString + baseString[remainderStack.pop()];
return retString;
print(decimalToBinary(25))
print(BaseConvertor(256,16))
print(BaseConvertor(25,8))
print(BaseConvertor(26,26))