def test_stack_array(self):
     arr1 = StackArray()
     arr1.push(4)
     arr1.push(5)
     self.assertEqual(arr1.size(), 2)
     self.assertEqual(arr1.is_full(), True)
     arr1.enlarge()
     self.assertEqual(arr1.is_full(), False)
     arr1.push(6)
     arr1.push(7)
     self.assertEqual(arr1.is_full(), True)
     self.assertEqual(arr1.pop(), 7)
     arr1.pop()
     arr1.pop()
     arr1.pop()
     self.assertEqual(arr1.is_empty(), True)
     arr2 = StackArray()
     arr2.enlarge()
     arr2.enlarge()
     arr2.shrink()
     self.assertEqual(arr2.capacity, 4)
     arr2.enlarge()
     arr2.push(5)
     arr2.push(9)
     arr2.push(10)
     self.assertEqual(arr2.peek(), 10)
     arr3 = StackArray()
     self.assertEqual(arr3.peek(), None)
     print(arr2.arr)
     arr4 = StackArray()
     with self.assertRaises(IndexError):
         arr4.pop()
Exemple #2
0
 def test_StackArray(self):
     stack = StackArray(3)
     self.assertRaises(IndexError, stack.pop)
     self.assertEqual(stack.is_empty(), True)
     stack.push('book1')
     stack.push('book2')
     self.assertEqual(stack.peek(), 'book2')
     self.assertEqual(stack.pop(), 'book2')
     self.assertEqual(stack.peek(), 'book1')
     stack.push('book2_2')
     stack.push('book3')
     self.assertRaises(IndexError, stack.push, 'book4')
     self.assertEqual(stack.size(), 3)
     self.assertEqual(stack.is_full(), True)
Exemple #3
0
def infix_to_postfix(infixexpr):
   """Converts an infix expression to an equivalent postfix expression """

   """Signature:  a string containing an infix expression where tokens are space separated is
       the single input parameter and returns a string containing a postfix expression
       where tokens are space separated"""
    
   s = StackArray(30)
   postfixList = []
   tokenList = infixexpr.split()
   prec = {'^':4, '/':3, '*':3, '+':2, '-':2, '(':1}
   for t in tokenList:
      if t in "0123456789":
         postfixList.append(t)
      elif t == "(":
         s.push(t)
      elif t ==")":
         topt = s.pop()
         while topt != "(":
            postfixList.append(topt)
            topt = s.pop()
      else:
         while (not s.is_empty()) and \
            (prec[s.peek()] >= prec[t]):
               postfixList.append(s.pop())
         s.push(t)

   while not s.is_empty():
      postfixList.append(s.pop())
   return " ".join(postfixList)
Exemple #4
0
 def test_stack_array2(self):
     stack = StackArray()
     for i in range(3):
         stack.push(i)
     self.assertEqual(stack.size(), 3)
     self.assertFalse(stack.is_empty())
     self.assertEqual(stack.peek(), 2)
Exemple #5
0
 def test_stack_array5(self):
     stack = StackArray()
     for i in range(3):
         stack.push(i)
         val = stack.pop()
         stack.push(val)
     self.assertEqual(stack.pop(), 2)
     self.assertEqual(stack.peek(), 1)
     self.assertEqual(stack.pop(), 1)
 def test_peek(self):
     stacka = StackArray()
     self.assertRaises(IndexError, stacka.peek)
     stacka.num_items = 2
     stacka.arr_list.arr = [1, 2]
     stacka.arr_list.num_items = 2
     self.assertEqual(stacka.peek(), 2)
     stackl = StackLinked()
     self.assertRaises(IndexError, stackl.peek)
     stackl.top = stacks.Node(1, None)
     stackl.num_items = 1
     self.assertEqual(stackl.peek(), 1)
Exemple #7
0
def infix_to_postfix(infix_expr):
    """ Convert an infix expression into a postfix expression.
    Assumes valid inputs.

    Args:
        list (infix_expr): properly formatted/valid str for conversion

    Returns:
        str: a string of the converted postfix expression
    """
    # Append adds new item to list
    # Concat creates a new list every time instead

    opstack = StackArray()
    res = []
    lstr = infix_expr.split()
    # l_para = r_para = 0
    # operator precedence dict
    prec = {  # higher val = higher prec
        "(": 4,
        "^": 3,  # r-to-l (i.e. 2^3^2 = 2^(3^2) )
        "~": 3,  # right-to-left (i.e. -3^2 = -9)
        # '*/+-' are associated left to right
        "*": 2,
        "/": 2,
        "+": 1,
        "-": 1
    }
    for token in lstr:
        if token[0] in '0123456789':
            res.append(token)
            # not opstack.is_empty() guards against IndexError on empty peek
            if not opstack.is_empty() and opstack.peek() == '^':
                res.append(opstack.pop())
                if not opstack.is_empty() and opstack.peek() == '~':
                    res.append(opstack.pop())
        elif token == '(':
            # l_para += 1
            opstack.push(token)
        elif token == ')':
            # r_para += 1
            # opstack can't be empty for proper formatted input
            while opstack.peek() != '(':
                res.append(opstack.pop())
            opstack.pop()  # remove left paran '('
        else:  # token is ^ ~ * / + -: <-- operators
            while not opstack.is_empty() and prec[token] <= prec[
                    opstack.peek()]:
                if opstack.peek() == '(':
                    break
                elif token == '^' and opstack.peek() == '~':
                    break
                else:
                    res.append(opstack.pop())
            opstack.push(token)
    # if l_para != r_para:
    #     raise SyntaxError
    while not opstack.is_empty():
        res.append(opstack.pop())
    res = " ".join(res)
    res.strip()
    return res
Exemple #8
0
 def test_push(self):
     stk_a = StackArray(10)
     stk_a.push(3)
     self.assertEqual(stk_a.peek(), 3 )
     self.assertEqual(stk_a.pop(), 3 )
     self.assertTrue(stk_a.is_empty())