Exemplo n.º 1
0
	def test_stack(self):
		stack = Stack(5)

		# Check instantiation default values
		self.assertEqual(stack.data, [0,0,0,0,0])
		self.assertEqual(stack.top, -1)

		# Check if isEmpty() is Working
		self.assertTrue(stack.isEmpty())

		# Check if isFull() is Working
		self.assertFalse(stack.isFull())

		# Check if push() is working properly
		stack.push(10)
		self.assertEqual(stack.data, [10,0,0,0,0])

		# Check if count() is working properly
		self.assertEqual(stack.count(), 1)

		# Check isEmpty() again since it is not empty now
		self.assertFalse(stack.isEmpty())

		# Check if pop is working properly
		stack.pop()
		self.assertEqual(stack.data, [0,0,0,0,0])

		# Check count() again since there are no values now
		self.assertEqual(stack.count(), 0)
Exemplo n.º 2
0
from DataStructures import Stack

par = {")": "(", "}": "{", "]": "["}
state = True
stack = Stack()
print("Enter a text to check wether it is or not correctly parenthesized.")
texto = input("Enter a text : ")

for i in texto:
    if i in par.values():
        stack.push(i)
        continue
    elif i in par.keys():
        if not stack.isEmpty() and par[i] == stack.peek():
            stack.pop()
            continue
        else:
            state = False
            break

if stack.isEmpty() and state == True:
    print("VALID")
else:
    print("NOT VALID")
Exemplo n.º 3
0
    def Depthfirst(self):
        self.__setup()
        stack = Stack()
        temp = None
        pathFound = False
        self.finalPath = None

        if self.board[self.startRow][self.startCol].getType() == "START":
            stack.push(self.board[self.startRow][self.startCol])
            stack.getTop().getData().setVisited(True)

            while not stack.isEmpty() and not pathFound:
                temp = stack.pop().getData()

                if temp.getType() == "FINISH":
                    pathFound = True
                else:

                    if temp.getRow() > 0 and self.board[temp.getRow() - 1][temp.getCol()].getType() != "WALL"\
                            and self.board[temp.getRow() - 1][temp.getCol()].getVisited() == False:

                        self.board[temp.getRow() - 1][temp.getCol()
                                                      ].setVisited(True)
                        self.board[temp.getRow() - 1][temp.getCol()
                                                      ].setPrev(temp)
                        stack.push(
                            self.board[temp.getRow() - 1][temp.getCol()])

                    if temp.getRow() < self.numRow - 1\
                            and self.board[temp.getRow() + 1][temp.getCol()].getType() != "WALL"\
                            and self.board[temp.getRow() + 1][temp.getCol()].getVisited() == False:

                        self.board[temp.getRow() + 1][temp.getCol()
                                                      ].setVisited(True)
                        self.board[temp.getRow() + 1][temp.getCol()
                                                      ].setPrev(temp)
                        stack.push(
                            self.board[temp.getRow() + 1][temp.getCol()])

                    if temp.getCol() > 0 and self.board[temp.getRow()][temp.getCol() - 1].getType() != "WALL"\
                            and self.board[temp.getRow()][temp.getCol() - 1].getVisited() == False:

                        self.board[temp.getRow()][temp.getCol() -
                                                  1].setVisited(True)
                        self.board[temp.getRow()][temp.getCol() -
                                                  1].setPrev(temp)
                        stack.push(self.board[temp.getRow()]
                                   [temp.getCol() - 1])

                    if temp.getCol() < self.numCol - 1\
                            and self.board[temp.getRow()][temp.getCol() + 1].getType() != "WALL"\
                            and self.board[temp.getRow()][temp.getCol() + 1].getVisited() == False:

                        self.board[temp.getRow()][temp.getCol() +
                                                  1].setVisited(True)
                        self.board[temp.getRow()][temp.getCol() +
                                                  1].setPrev(temp)
                        stack.push(self.board[temp.getRow()]
                                   [temp.getCol() + 1])
                pathfinder_gui.showStack(temp, stack)

        if pathFound:
            self.finalPath = Stack()

            while temp != None:
                self.finalPath.push(temp)
                temp = temp.getPrev()
            pathfinder_gui.showPath(self.finalPath)