Esempio 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)
Esempio n. 2
0
    def DS_stack(self):
        try:
            length = int(input(" Enter the length of your Stack: "))
        except:
            return self.DS_stack()

        # Stack instantiation with the given length
        stack = Stack(length)

        while True:
            print(
                '\n [STACK OPERATIONS] \n\n 1. Push\n 2. Pop\n 3. Display\n 4. Count\n 5. Back\n'
            )

            try:
                selectedNumber = int(input("Select a number: "))
            except:
                print("\nPlease enter a valid input\n")
                input("Press [Enter] to continue...")
                self.DS_stack()

            if selectedNumber == 1:
                value = int(input(" Enter the value that you want to push: "))
                stack.push(value)
                print("Success!")
                input("Press [Enter] to continue...")

            elif selectedNumber == 2:
                stack.pop()
                print("Success!")
                input("Press [Enter] to continue...")

            elif selectedNumber == 3:
                stack.display()
                input("Press [Enter] to continue...")

            elif selectedNumber == 4:
                stack.count()
                input("Press [Enter] to continue...")

            elif selectedNumber == 5:
                self.DS_main()

            else:
                print(
                    "The number you entered is not in the choices... Going back to the main screen instead..."
                )
                input("Press [Enter] to continue...")
                self.DS_main()