Пример #1
0
def isvalid(string):
    validbrackets = {'(':')','[':']','{':'}'}
    list1 =[]
    for letter in string:
        try:
            closing = validbrackets[list1.pop()]
        except:
            return False;
        if letter in validbrackets:
            mystack.push (list1,letter)
        elif (closing != letter):
            return False
    if(len(list1)==0):
        return True;
Пример #2
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)
Пример #3
0
 def test_length(self):
     """ -- Verifica el numero de elementos de la lsita cuando son mayores a 1
     """
     L = LinkedList()
     push(L, "hola")
     push(L, "jorge")
     push(L, "como")
     push(L, "va?")
     res = length(L)
     self.assertEqual(res, 4)
Пример #4
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)
Пример #5
0
 def test_stack_ops(self):
     """ -- Verifica que el ultimo elemento ingresado en la pila sea el primero en salir
     """
     L = LinkedList()
     push(L, "hola")
     push(L, "jorge")
     push(L, "como")
     push(L, "va?")
     first = pop(L)
     second = pop(L)
     self.assertEqual([first, second], ["va?", "como"])
import mystack

thestack = []

print("The stack should initially be: []")
print("Your stack is ", thestack)
print()
print("Trying to pop from an empty stack, should be None")
print(mystack.pop(thestack))
print()
print("Pushing 0 to stack, should be [0]")
mystack.push(thestack, 0)
print("Your stack is ", thestack)
print()
print("Pushing 1 to stack, should be [0, 1]")
mystack.push(thestack, 1)
print("Your stack is ", thestack)
print()
print("Pushing 2 to stack, should be [0, 1, 2]")
mystack.push(thestack, 2)
print("Your stack is ", thestack)
print()
print("Pushing 3 to stack, should be [0, 1, 2, 3]")
mystack.push(thestack, 3)
print("Your stack is ", thestack)
print()
print("Pop should return 3")
print(mystack.pop(thestack))
print()
print("Pop should return 2")
print(mystack.pop(thestack))
Пример #7
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))