def setUp(self):
        # Set up some Elements
        self.e1 = LinkedListElement(1)
        self.e2 = LinkedListElement(2)
        self.e3 = LinkedListElement(3)
        self.e4 = LinkedListElement(4)

        # Start setting up a LinkedList
        self.stack = Stack(self.e1)
        return 
class TestStackMethods(unittest.TestCase):

    def setUp(self):
        # Set up some Elements
        self.e1 = LinkedListElement(1)
        self.e2 = LinkedListElement(2)
        self.e3 = LinkedListElement(3)
        self.e4 = LinkedListElement(4)

        # Start setting up a LinkedList
        self.stack = Stack(self.e1)
        return 

    def tearDown(self):
        #Clear For Next Test
        self.e1 = self.e2 = self.e3 = self.e4 = None
        self.stack = None
    
    def test_peek(self):
        self.assertEqual(self.stack.peek().value,1)

    def test_push(self):
        # Setup
        # Test stack functionality
        self.stack.push(self.e2)
        self.stack.push(self.e3)
        self.assertEqual(self.stack.peek().value,3)

    def test_pop(self):
       self.stack.push(self.e2)
       self.stack.push(self.e3)
       self.assertEqual(self.stack.pop().value, 3)
       self.assertEqual(self.stack.pop().value, 2)
       self.assertEqual(self.stack.pop().value, 1)
       self.assertEqual(self.stack.pop(), None)
def stack_operations():
    '''USER OPERATIONS OF A STACK IMPLEMENTATION'''
    stack = Stack()
    global choice
    while choice > 0:
        stack_menu = [('MAIN MENU', -1), ('EXIT', 0), \
                    ('PUSH', 1), ('POP', 2), ('PEEK', 3), ('SIZE', 4)]
        print_menu(stack_menu)
        choice = int(input().strip())
        if choice == 1:
            print('ENTER ITEM :', end=' ')
            stack.push(int(input().strip()))
            print('PUSH OPERATION SUCCESSFUL.')
        elif choice == 2:
            if stack.is_empty():
                print('UNDERFLOW')
            else:
                print('POPPED VALUE :', stack.pop())
        elif choice == 3:
            if stack.is_empty():
                print('UNDERFLOW')
            else:
                print('STACK TOP :', stack.peek())
        elif choice == 4:
            print('STACK SIZE :', stack.size())
 def test_simple(self):
     s = Stack()
     s.push('a')
     s.push('b')
     s.push('c')
     self.assertEquals(s.top(), 'c')
     self.assertFalse(s.empty())
     self.assertEquals(s.pop(), 'c')
     self.assertEquals(s.pop(), 'b')
     self.assertEquals(s.pop(), 'a')
     self.assertTrue(s.empty())
Esempio n. 5
0
def test_clear():
    myStack = Stack()
    myStack.push(3)
    myStack.push(4)
    myStack.clear()
    assert myStack.size() == 0
    assert myStack.isEmpty()
    assert myStack.toList() == []
Esempio n. 6
0
def par_checker(in_str: str):
    """
    >>> par_checker('()')
    True
    >>> par_checker(')(')
    False
    >>> par_checker('()()')
    True
    >>> par_checker('({})')
    True
    """
    s = Stack()
    balanced = True
    index = 0
    while index < len(in_str) and balanced:
        symbol = in_str[index]
        if symbol in OPENS:
            s.push(symbol)
        else:
            if s.isEmpty():
                balanced = False
            else:
                top = s.pop()
                if OPENS.index(top) != CLOSERS.index(symbol):
                    balanced = False
        index += 1
    return balanced and s.isEmpty()
Esempio n. 7
0
def test_stack_pop_method_returns_correct_value():
    stack = Stack()
    stack.push(7)
    stack.push(8)
    stack.push(9)
    assert stack.store == [7, 8, 9]
    stack.pop()
    assert stack.store == [7, 8]
Esempio n. 8
0
def symbol_checker(user_input):
    '''
    Check {[( symbols matching has proper open and end 
  '''
    open_symbols = '{[('
    close_symbols = '}])'
    is_valid = True

    stk = Stack()

    for symbol in user_input:
        if symbol in open_symbols:
            stk.push(symbol)
        else:
            if stk.is_empty():
                is_valid = False
                break
            else:
                open_symbol = stk.pop()
                if not open_symbols.index(open_symbol) == close_symbols.index(
                        symbol):
                    is_valid = False
                    break

    if is_valid:
        print("Symbols matching...")
    else:
        print("Invalid symbol pattern...")
Esempio n. 9
0
def revert(s: str):
    stack = Stack()

    for ch in s:
        stack.push(ch)

    res = []
    while not stack.isEmpty():
        res.append(stack.pop())

    return ''.join(res)
Esempio n. 10
0
def dectoBin(decimal):
    s = Stack()
    while decimal > 0:
        rem = decimal % 2
        s.push(rem)
        decimal //= 2

    binString = ''
    while not s.isEmpty():
        binString += str(s.pop())

    return binString
Esempio n. 11
0
def decToBase(decimal, base):
    alphanumeric = "0123456789ABCDEF"
    remainders = Stack()
    baseString = ''
    while decimal > 0:
        remainder = decimal % base
        remainders.push(remainder)
        decimal //= base

    while not remainders.isEmpty():
        baseString += alphanumeric[remainders.pop()]

    return baseString
def decimal_to_binary(number):
    remainder_stack = Stack()

    while (number > 0):
        rem = number % 2
        #print rem
        number = number // 2
        #print number
        remainder_stack.push(rem)

    s = ''
    for k in range(remainder_stack.size()):
        s += str(remainder_stack.pop())
    print s
Esempio n. 13
0
def balanceCheck(symbols):
    s = Stack()
    balanced = True
    index = 0
    while index < len(symbols) and balanced:
        symbol = symbols[index]
        if symbol in "({[":
            s.push(symbol)
        else:
            if s.isEmpty():
                balanced = False
            else:
                top = s.pop()
                if not matched(top, symbol):
                    balanced = False
        index += 1
    if balanced and s.isEmpty():
        return True
    else:
        return False
Esempio n. 14
0
def convert_number(user_input, base):
    '''
    Convert number into different base format
    2 : Binary
    8 : Octal
    16 : Hex
  '''
    stk = Stack()
    remainder_list = '0123456789ABCDEF'
    while user_input > 0:
        remainder = user_input % base
        stk.push(remainder_list[remainder])
        user_input = user_input // base
    result = ''
    while not stk.is_empty():
        result += str(stk.pop())
    print(result)
Esempio n. 15
0
def test_isEmpty_empty_stack():
    myStack = Stack()
    assert myStack.isEmpty()
 def test_multiple(self):
     s = Stack()
     s.push('a')
     self.assertFalse(s.empty())
     s.push('b')
     self.assertEquals(s.pop(), 'b')
     s.push('b')
     self.assertEquals(s.pop(), 'b')
     s.push('b')
     self.assertEquals(s.pop(), 'b')
     self.assertEquals(s.pop(), 'a')
     self.assertTrue(s.empty())
     self.assertRaises(EmptyStackError, s.pop)
 def test_empty(self):
     s = Stack()
     self.assertTrue(s.empty(), True)
     self.assertRaises(EmptyStackError, s.top)
     self.assertRaises(EmptyStackError, s.pop)
Esempio n. 18
0
from datastructures.stack import Stack
import time

n = 26
transfers = 0

stack_i = Stack()
stack_j = Stack()
stack_k = Stack()

for i in reversed(range(n)):
    # print(".")
    stack_i.push(i)

start_time = time.time()


def transfer(from_stack, to_stack):
    global transfers
    transfers += 1
    to_stack.push(from_stack.pop())


def do_hanoi(n, stack_i, stack_j, stack_k):
    if n == 0:
        return
    do_hanoi(n - 1, stack_i, stack_k, stack_j)
    transfer(stack_i, stack_j)
    do_hanoi(n - 1, stack_k, stack_j, stack_i)

Esempio n. 19
0
def test_stack_is_empty_method():
    stack = Stack()
    assert stack.is_empty() is True
    stack.push(1)
    assert stack.is_empty() is False
Esempio n. 20
0
def test_stack_push_method_add_value():
    stack = Stack()
    stack.push(7)
    stack.push(8)
    stack.push(9)
    assert stack.store == [7, 8, 9]
Esempio n. 21
0
def test_pop():
    myStack = Stack()
    myStack.push(2)
    assert myStack.pop() == 2
Esempio n. 22
0
def insert(to_insert, stack, temp_stack):
    position = 0
    while not stack.stack_empty():
        if stack.peek() < to_insert:
            # Found correct position
            break
        temp_stack.push(stack.pop())
        position += 1
    stack.push(to_insert)
    for _ in xrange(position):
        stack.push(temp_stack.pop())


def sort_stack_recursive(stack, temp_stack):
    if stack.stack_empty():
        return
    temp_stack.push(stack.pop())
    sort_stack_recursive(stack, temp_stack)
    to_insert = temp_stack.pop()
    insert(to_insert, stack, temp_stack)


stack = Stack()
temp_stack = Stack()
for i in xrange(10, 0, -1):
    stack.push(i)
stack.traverse()
sort_stack(stack, temp_stack)
stack.traverse()
Esempio n. 23
0
def test_peek():
    myStack = Stack()
    myStack.push(3)
    myStack.push(4)
    assert myStack.peek() == 4
Esempio n. 24
0
def test_isEmpty_non_empty_stack():
    myStack = Stack()
    myStack.push(1)
    assert not myStack.isEmpty()
Esempio n. 25
0
def test_size():
    myStack = Stack()
    myStack.push(3)
    myStack.push(4)
    assert myStack.size() == 2
Esempio n. 26
0
def test_toList():
    myStack = Stack()
    myStack.push(3)
    myStack.push(4)
    assert myStack.toList() == [3, 4]
Esempio n. 27
0
 def setUp(self) -> None:
     self.stack = Stack("plate1")