Python :Data Structure in Python:-
Stack Implementation:
class stack:
def __init__(self):
self.item = []
def push(self, val):
self.item.append(val)
def pop(self):
return self.item.pop(0)
def isempty(self):
return self.item==[]
def peek(self):
return self.item[0]
def size(self):
return len(self.item)
- Application of Stack :
class stack:
def __init__(self):
self.item = []
def push(self, val):
self.item.append(val)
def pop(self):
return self.item.pop(0)
def isempty(self):
return self.item==[]
def peek(self):
return self.item[0]
def size(self):
return len(self.item)
def parChecker(input_string):
x= stack()
balance = True
index = 0
while index < len(input_string) and balance:
symbol = input_string[index]
if symbol in "([{":
x.push(symbol)
else:
if(x.isempty()):
balance =False
else:
data = x.pop()
if not match(data,symbol):
balance = False
index = index + 1
if(balance and x.isempty()):
return True
else :
return False
def match(open,close):
open1= "({["
close1 = ")}]"
return open1.index(open) == close1.index(close)
print(parChecker('{{([][])}()}'))
- Application of Stack
def divide(num,base):
rem_stack = stack()
while (num > 0):
rem = num % base
rem_stack.push(rem)
num = num // base
binstrng = ""
while not rem_stack.isempty():
binstrng = binstrng + str(rem_stack.pop())
return binstrng
print divide(25,2)
No comments:
Post a Comment