Пример #1
0
def isvalid(string):
    closeToOpen = {"}": "{", "]": "[", ")": "("}
    closing = closeToOpen.keys()
    opening = closeToOpen.values()
    openStack = []
    for i in string:
        if i in opening:
            mystack.push(openStack, i)
        elif i in closing:
            if not mystack.isempty(
                    openStack) and closeToOpen[i] == mystack.peek(openStack):
                mystack.pop(openStack)
                continue
            return False
    return mystack.isempty(openStack)
Пример #2
0
def isvalid(string):
    closeToOpen = {"}": "{", "]": "[", ")": "(", "\"": "\"", "”": "“"}
    closing = closeToOpen.keys()
    opening = closeToOpen.values()
    openStack = []
    for i in string:
        if i in opening:
            #print ("Adding", i, openStack)
            mystack.push(openStack, i)
        elif i in closing:
            if not mystack.isempty(
                    openStack) and closeToOpen[i] == mystack.peek(openStack):
                #print ("Removing", closeToOpen[i], i, openStack)
                mystack.pop(openStack)
                continue
            return False
    #print (openStack)
    return mystack.isempty(openStack)
print("Peek should be 5")
print(mystack.peak(thestack))
print()
print("Pop should return 5")
print(mystack.pop(thestack))
print()
print("The stack should be: [0, 1, 4]")
print("Your stack is ", thestack)
print()
print("Pop should return 4")
print(mystack.pop(thestack))
print()
print("The stack should be: [0, 1]")
print("Your stack is ", thestack)
print()
print("Clearing stack")
while not mystack.isempty(thestack):
    mystack.pop(thestack)
print()
print("The stack should be: []")
print("Your stack is ", thestack)
print()
print("Pop should return None")
print(mystack.pop(thestack))
print()
print("Peak should return None")
print(mystack.peak(thestack))
print()
print("The stack should be: []")
print("Your stack is ", thestack)
Пример #4
0
import mystack
stack = []
print ('Adding 5 to stack: ', mystack.push(stack, 5))
print ('Adding 6 to stack: ', mystack.push(stack, 6))
print ('Adding 7 to stack: ', mystack.push(stack, 7))
print ('Stack Empty: ', mystack.isempty(stack))
print ('Peeking at stack: ', mystack.peek(stack))
print ('Poping from stack: ', mystack.pop(stack))
print ('Printing stack: ', stack)
print ('Poping from stack: ', mystack.pop(stack))
print ('Printing stack: ', stack)
print ('Adding 8 to stack: ', mystack.push(stack, 8))
print ('Adding 9 to stack: ', mystack.push(stack, 9))
print ('Poping from stack: ', mystack.pop(stack))
print ('Printing stack: ', stack)
print ('Poping from stack: ', mystack.pop(stack))
print ('Printing stack: ', stack)
print ('Poping from stack: ', mystack.pop(stack))
print ('Printing stack: ', stack)
print ('Poping from stack: ', mystack.pop(stack))
print ('Printing stack: ', stack)
print ('Stack Empty: ', mystack.isempty(stack))