Example #1
0
def test_stack_isEmpty():
    test = Stack()
    assert (test.isEmpty == True)
    test.push(1)
    assert (test.isEmpty == False)
    test.pop()
    assert (test.isEmpty == True)
Example #2
0
class StackTest(unittest.TestCase):
    def setUp(self):
        self.s = Stack()

    def tearDown(self):
        while not self.s.isEmpty():
            self.s.pop()

    def test_push(self):
        value = 'A'
        self.s.push(value)
        self.assertEqual(1, self.s.size())
        print self.s.get()
        x = self.s.pop()
        print self.s.get()
        self.assertEqual(value, x)
        self.assertEqual(0, self.s.size())

    def test_peek(self):
        self.s.push('A')
        self.s.push('B')
        x = self.s.peek()
        self.assertEqual('B', x)
        self.assertEqual(2, self.s.size())

    def test_empty(self):
        self.assertEqual(self.s.peek(), None)
Example #3
0
def check_balance(string):
    '''
    Check to see if we have balanced parethesis.

    Parameters:
    -----------
    string: str. The string contains the text with parenthesis that needs to be check balanced.

    Return:
    Boolean. Tell if parenthesis is matched or not
    '''

    parethesis = Stack()
    par_dict = {')': '(', '}': '{', ']': '['}
    for s in string.strip():
        if s in par_dict.values():
            parethesis.push(s)
        elif s in par_dict.keys():
            try:
                t = parethesis.pop()
                if t != par_dict[s]:
                    return False
            except TypeError:
                return False
    if parethesis.size() != 0:
        return False

    return True
Example #4
0
def logging(logger: Stack, pos, state, action, timestep, reward, next_pos):
    x, y, z = pos[0], pos[1], pos[2]
    nx, ny, nz = next_pos[0], next_pos[1], next_pos[2]
    logger.push([
        state.id, state.no, action.input_key, timestep, reward,
        str((x, y, z)) + '->' + str((nx, ny, nz))
    ])
    return
Example #5
0
def baseconverter(number, base):
    s = Stack()
    number_string = '0123456789ABCDEF'

    while number > 0:
        remainder = number % base
        s.push(remainder)
        number = number // base

    base_builder = ''
    while not s.isEmpty():
        base_builder = base_builder + str(number_string[s.pop()])

    return base_builder
Example #6
0
def change_base(number, base):
    digits = "0123456789ABCDEF"
    remStack = Stack()
    b = ''

    while (number != 0):
        rem = number % base
        remStack.push(rem)
        number = number // base

    while not remStack.is_empty():
        b = b + digits[remStack.pop()]

    return b
def balancedparantheses(parantheses_string):
    s = Stack()
    balanced = True
    index = 0
    while index < len(parantheses_string) and balanced:
        symbol = parantheses_string[index]
        if symbol == "(":
            s.push(symbol)
        elif s.isEmpty():
            balanced = False
        else:
            s.pop()
        index += 1

    if balanced and s.isEmpty():
        return True
    else:
        return False
def balancedsymbols(symbol_string):
    s = Stack()
    balanced = True
    index = 0
    lastopensymbol = ''
    while index < len(symbol_string) and balanced:
        symbol = symbol_string[index]
        lastopensymbol = s.peek()
        if symbol in "({[":
            s.push(symbol)
        elif s.isEmpty():
            balanced = False
        else:
            balanced = symbolchecker(lastopensymbol, symbol)
            if balanced:
                s.pop()

        index += 1

    if balanced and s.isEmpty():
        return True
    else:
        return False
Example #9
0
def test_stack_push():
    test = Stack()
    test.push(1)
    test.push(2)
    assert (len(test._contents) == 2)
    assert (test._contents[0] == 1)
Example #10
0
def test_stack_peek():
    test = Stack()
    test.push(1)
    test.push(2)
    test.push(3)
    assert (test.peek() == 3)
Example #11
0
def test_stack_pop():
    test = Stack()
    test.push('a')
    test.push('b')
    assert (test.pop() == 'b')
    assert (len(test._contents) == 1)