here's what's been frying my brain for about the past 3 hours,
I need to make a program using STACKS i can't use RE's to check HTML code and see if it is valid code ie. matching tags and such.
i've got it to check if the <'s and >'s match up correctly but i can't even really think of a way to tell if the actual tags match up. for example... <b></b> i don't know how to look at those 2 so that the program can tell that they match correctly...
maybe the way i said it is a little confusing, but if you get what i'm saying or can ask any questions to clear it up and help me that would be awesome!
CODE
def htmlChecker(htmlcode):
stack = Stack()
stack2 = Stack()
for ch in htmlcode:
if ch =='<':
if stack.isEmpty():
stack.push(ch)
else:
return stack.isEmpty()
if ch =='>':
stack.pop()
if not stack.isEmpty()and ch!='<':
stack2.push(ch)
print stack2
Building stacks out of this:
CODE
class Stack():
"""Top of the stack is at the end of the list"""
def __init__(self):
self._items = []
def push(self,obj):
self._items.append(obj)
def pop(self):
return self._items.pop()
def peek(self):
return self._items[-1]
def isEmpty(self):
return len(self._items)==0
def __len__(self):
return len(self._items)
def __str__(self):
return "bottom "+str(self._items)+ " top"
def reverse(self):
return self._items.reverse()
def lower(self):
return self._items.lower()