Exemple #1
0
def copy(original_stack):
    temp_list = []
    copied_stock = Stack()
    for i in original_stack:
        temp_list.append(i)
    for i in temp_list[::-1]:
        copied_stock.push(i)
    return copied_stock
Exemple #2
0
def parentheses(test_str):
    s = Stack()
    for i in test_str:
        if i in right_parenthesis:
            s.push(i)
        else:
            pop = s.pop()
            if not ord(pop) - ord(i) <= 2:  # 比较ascii数值,小于等于2的话就是匹配的左括号
                return False
    return s.is_empty()
Exemple #3
0
from algs4.Stack import Stack

test_str_list = '( ( 1 + 2 ) * ( ( 3 - 4 ) * ( 5 - 6 ) ) )'.split()
test_stock = Stack()

for i in test_str_list:
    test_stock.push(i)


def copy(original_stack):
    temp_list = []
    copied_stock = Stack()
    for i in original_stack:
        temp_list.append(i)
    for i in temp_list[::-1]:
        copied_stock.push(i)
    return copied_stock


print(copy(test_stock))
Exemple #4
0
 def __init__(self):
     self.__right = Stack()
     self.__left = Stack()
     self.__N = 0
Exemple #5
0
class Buffer:
    def __init__(self):
        self.__right = Stack()
        self.__left = Stack()
        self.__N = 0

    def insert(self, c):
        self.__right.push(c)
        self.__N += 1

    def delete(self):
        self.__N -= 1
        return self.__right.pop() if not self.__right.is_empty() else ''

    def left(self, k):
        for i in range(k):
            if self.__left.is_empty():
                return
            self.__right.push(self.__left.pop())

    def right(self, k):
        for i in range(k):
            if self.__right.is_empty():
                return
            self.__left.push(self.__right.pop())

    def size(self):
        return self.__N
Exemple #6
0
from algs4.Stack import Stack

test_str = '( ( 1 + 2 ) * ( ( 3 - 4 ) * ( 5 - 6 ) ) )'
# test_str = '2 * 3 / ( 2 - 1 ) + 3 * ( 4 - 1 )'
test_str_list = test_str.split()
operator_stack = Stack()
numeric_stack = Stack()
operator_count = 0
number_count = 0
operator_precedence = {'+': 1, '-': 1, '*': 2, '/': 2}
res_str = ''


def not_greater(this, peek):
    try:
        a = operator_precedence[this]
        b = operator_precedence[peek]
        return True if a <= b else False
    except KeyError:
        return False


for i in test_str_list:
    if i.isnumeric():
        res_str += i + ' '
    elif i == '(':
        operator_stack.push(i)
    elif i == ')':
        while not operator_stack.is_empty() and operator_stack.peek() != '(':
            res_str += operator_stack.pop() + ' '
        operator_stack.pop()
Exemple #7
0
from algs4.Stack import Stack

test_str = '1 + 2 ) * 3 - 4 ) * 5 - 6 ) ) )'
test_str_list = test_str.split()
res_stack = Stack()
num_count = 0
operator_count = 0
for i in test_str_list[::-1]:
    res_stack.push(i)
    if i.isnumeric():
        num_count += 1
        if num_count == 2:
            num_count = 0
            for j in range(operator_count):
                res_stack.push('(')
            operator_count = 0
    elif i is not ')' and i is not ' ':
        operator_count += 1

res_str = ''
for i in res_stack:
    res_str += i + ' '
print(res_str)
Exemple #8
0
from algs4.Stack import Stack

postfix_str = '1 2 + 3 4 - 5 6 - * *'
postfix_str_list = postfix_str.split()
numeric_stack = Stack()
operator_stack = Stack()

for i in postfix_str_list:
    if i.isnumeric():
        numeric_stack.push(i)
    else:
        res = 0
        num2 = int(numeric_stack.pop())
        num1 = int(numeric_stack.pop())
        if i == '+':
            res += num1 + num2
        elif i == '-':
            res += num1 - num2
        elif i == '*':
            res += num1 * num2
        elif i == '/':
            res += num1 / num2
        numeric_stack.push(res)

print(numeric_stack.peek())
Exemple #9
0
 def __init__(self):
     self._list_two_stack = [Stack(), Stack()]
     # self.s1 = Stack()
     # self.s2 = Stack()
     self._N = 0