Skip to content

Instantly share code, notes, and snippets.

@dimejimudele
Created February 3, 2019 10:35
Show Gist options
  • Save dimejimudele/de6e5d1a0ea7394a424d26b31e326169 to your computer and use it in GitHub Desktop.
Save dimejimudele/de6e5d1a0ea7394a424d26b31e326169 to your computer and use it in GitHub Desktop.
Algorithms and data structures created by dimejimudele - https://repl.it/@dimejimudele/Algorithms-and-data-structures
#Implementing a Stack in a class
class stack:
def __init__(self):
self.items = []
def isEmpty(self):
return self.items == []
def push(self, item):
return self.items.append(item)
def peek(self):
return self.items[-1]
def pop(self):
return self.items.pop()
def size(self):
return len(self.items)
def parChecker(input):
s = stack()
idx = 0
balanced = True
assert (input == []) == False, "Input string cannot be empty"
while idx < len(input) and balanced:
symbol = input[idx]
if symbol == "(":
s.push(symbol)
else:
if s.isEmpty():
balanced = False
else:
s.pop()
if s.isEmpty() and balanced:
return "Balanced"
else:
return "Non balanced"
a = parChecker("((()")
print(a)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment