Skip to content

Instantly share code, notes, and snippets.

@dimejimudele
Created February 3, 2019 11:55
Show Gist options
  • Save dimejimudele/e0004d0aece51ebb088472f0f6e0646f to your computer and use it in GitHub Desktop.
Save dimejimudele/e0004d0aece51ebb088472f0f6e0646f 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 len(input) != 0 and isinstance(input, string) and not input.isspace(), "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)
"""
s = stack()
print(s.items)
s.push(7)
s.push(8)
s.push(9)
s.push(10)
print(s.peek())
print(s.size())
print(s.isEmpty())
"""
import main.stack
def parChecker(input):
s = main.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