Created
February 3, 2019 10:35
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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