예제 #1
0
def infix2postfix(infix):
    prec = {
        '(': 1,
        '+': 2,
        '-': 2,
        '*': 3,
        '/': 3,
    }

    stack = Stack()
    infix_list, postfix_list = infix.split(), []

    for char in infix_list:
        if char in string.ascii_uppercase:
            postfix_list.append(char)
        elif char == '(':
            stack.push(char)
        elif char == ')':
            token = stack.pop()
            while token != '(':
                postfix_list.append(token)
                token = stack.pop()
        else:
            while not stack.is_empty() and prec[stack.peek()] >= prec[char]:
                postfix_list.append(stack.pop())
            stack.push(char)

    while not stack.is_empty():
        postfix_list.append(stack.pop())

    return ' '.join(postfix_list)
def par_checker(symbol_string):
    stack = Stack()
    idx, n, balanced = 0, len(symbol_string), True

    while idx < n and balanced:
        symbol = symbol_string[idx]
        if symbol in '([{':
            stack.push(symbol)
        else:
            if stack.is_empty() or not matches(stack.pop(), symbol):
                balanced = False
        idx += 1

    if balanced and stack.is_empty():
        return True
    else:
        return False
def par_checker(symbol_string):
    stack = Stack()
    index, length, balanced = 0, len(symbol_string), True

    while index < length and balanced:
        symbol = symbol_string[index]
        if symbol == '(':
            stack.push(symbol)
        else:
            if stack.is_empty():
                balanced = False
            else:
                stack.pop()
        index += 1

    if balanced and stack.is_empty():
        return True
    else:
        return False
예제 #4
0
def htmlChecker(htmlStr):
    stack = Stack()
    hsize = len(htmlStr)
    i = 0
    while i < hsize:
        tag = []
        openTag = True
        if htmlStr[i] == '<':
            tag.append('<')
            i += 1
            if htmlStr[i] == '/':
                openTag = False
                i += 1
            while (i < hsize) and htmlStr[i] == ' ':
                i += 1
            while (i < hsize) and (htmlStr[i].isalpha()
                                   or htmlStr[i].isdigit()):
                tag.append(htmlStr[i])
                i += 1
            while (i < hsize) and htmlStr[i] != '>':
                i += 1
            if (i >= hsize):
                return False
            tag.append(htmlStr[i])
            htmTag = ''.join(tag)
            # print ("tag: ", htmTag)
            if openTag:
                stack.push(htmTag)
            elif stack.is_empty():
                return False
            else:
                topTag = stack.pop()
                # print("popped: ", topTag)
                # print("htmTag: ", htmTag)
                if topTag != htmTag:
                    return False
        i += 1
    if not stack.is_empty():
        return False
    return True
def decimal2bin(decimal_number):
    stack = Stack()

    while decimal_number > 0:
        rem = decimal_number % 2
        stack.push(rem)
        decimal_number //= 2

    res_list = []
    while not stack.is_empty():
        res_list.append(str(stack.pop()))

    return ''.join(res_list)
def base_converter(decimal_number, base=10):
    digits = '0123456789ABCDEF'
    assert base <= 16
    stack = Stack()

    while decimal_number > 0:
        rem = decimal_number % base
        stack.push(rem)
        decimal_number //= base

    res_list = []
    while not stack.is_empty():
        res_list.append(digits[stack.pop()])

    return ''.join(res_list)
예제 #7
0
    def _search(self, key, on_delete=False):
        """
        跳表 search/insert/delete 方法通用查询逻辑

        Args:
            key: 查询的键
            on_delete: delete方法特有逻辑

        Returns:
            目标节点, 目标节点的前驱节点列表
        """
        node: Union[HeaderNode, DataNode] = self._head
        tower_stack = Stack()  # 前驱节点列表(通过栈维护由底向上的顺序)
        found = False

        while node is not None and not found:
            if node.next is None or node.next.key > key:
                tower_stack.push(node)  # 降到下一层前保存当前层的前驱节点
                node = node.down
            elif node.next.key <= key:
                node = node.next
                if node.key == key:
                    found = True

        if found and on_delete:
            # 删除操作中,需要继续往下找到每一层目标节点的前驱节点
            pre_node = self._head if tower_stack.is_empty(
            ) else tower_stack.pop()
            while pre_node is not None:
                if pre_node.next is None or pre_node.next.key >= key:
                    tower_stack.push(pre_node)
                    pre_node = pre_node.down
                else:
                    pre_node = pre_node.next

        return node, tower_stack