Here is my code.
class Stack ():
def __init__(self):
self.items=[]
def push (self,item):
self.items.append(item)
def pop (self):
return self.items.pop()
def size(self):
return len(self.items)
s1= Stack()
decNum= int(input("Enter the decimal num : "))
newNum=decNum
while newNum==1:
newNum= decNum%2
decNum = decNum//2
s1.push(newNum)
while s1.size() is not Null:
a=s1.pop()
print(a)
First of all, the above code you have pasted has lots of indentation issues, next time you should try to fix those indentation issues and running your program again, before posting it here.
Secondly, your logic is also wrong, you should not be checking -
while newNum == 1:
at start itself, you are setting newNum = decNum , unless you enter 1 , your program would not enter that while loop. The condition you need is -
while decNum != 0:
Secondly, the second while loop, should not be inside the first while loop, it should be outside, so that you print out the stack once all the computation is complete. Also , you don't even need the first newNum = decNum , before the loop starts.
I fixed all your indentation issues, and logical issues, the code is -
import sys
class Stack ():
def __init__(self):
self.items=[]
def push (self,item):
self.items.append(item)
def pop (self):
return self.items.pop()
def size(self):
return len(self.items)
s1= Stack()
decNum= int(raw_input("Enter the decimal num : "))
while decNum!=0:
newNum= decNum%2
decNum = decNum//2
s1.push(newNum)
while s1.size() != 0:
a=s1.pop()
sys.stdout.write(str(a))
sys.stdout.flush()
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments