コード例 #1
0
def parCheckerMore(symbol_string):
    """
        通用括号匹配,支持 (, [, { 这三种
    @symbol_string: 要匹配的括号字符串
    @return: 返回匹配结果,bool类型
    """
    stack = Stack()
    index = 0
    balanced_flag = True  # 两边括号数量平衡标识

    while index < len(symbol_string) and balanced_flag:
        symbol = symbol_string[index]
        # 判断左边括号类型需要调整
        if symbol in '([{':
            stack.push(symbol)
        else:
            if stack.isEmpty():
                balanced_flag = False
            else:
                top = stack.pop()
                # 栈中移除的括号要和进入栈的括号匹配类型
                if not matches(top, symbol):
                    balanced_flag = False
        index += 1
    if balanced_flag and stack.isEmpty():
        balanced_flag = True
    else:
        balanced_flag = False
    return balanced_flag
コード例 #2
0
def parChecker(symbol_string):
    """
        先写一个只有'()'类型的算法
    @symbol_string: 要匹配的括号字符串
    @return: 返回匹配结果,bool类型
    """
    stack = Stack()
    index = 0
    balanced_flag = True  # 两边括号数量平衡标识

    while index < len(symbol_string) and balanced_flag:
        symbol = symbol_string[index]
        if symbol == '(':
            stack.push(symbol)
        else:
            if stack.isEmpty():
                balanced_flag = False
            else:
                stack.pop()
        index += 1
    if balanced_flag and stack.isEmpty():
        balanced_flag = True
    else:
        balanced_flag = False
    return balanced_flag
コード例 #3
0
def infixToPostfix(infixExpr):
    """
    中缀表达式转后缀表达式算法实现
    @infixExpr: 中缀表达式,格式如:A + B * C,必须含空格,且操作数只包含A-Z 0-9其中的值
    @return: 转换成功的后缀表达式
    """
    # 记录操作符优先级
    prec = {}
    prec["*"] = 3
    prec["/"] = 3
    prec["+"] = 2
    prec["-"] = 2
    prec["("] = 1

    op_stack = Stack()
    possible_str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
    post_fix_lst = []
    token_lst = infixExpr.split()
    for token in token_lst:
        # 如果单词是 操作数, 则直接添加到后缀表达式列表的末尾
        if token in possible_str:
            post_fix_lst.append(token)

        # 如果单词是左'(', 则压入栈顶,标记下
        elif token == '(':
            op_stack.push(token)

        # 如果单词是右')',则反复弹出栈顶操作符,加入到输出列表末尾,直到遇到左括号
        elif token == ')':
            top_token = op_stack.pop()
            while top_token != '(':
                post_fix_lst.append(top_token)
                top_token = op_stack.pop()
        # 如果单词是操作符, 则压入栈顶,
        # 但在压之前, 要比较其与栈顶操作符的优先级
        # 如果栈顶操作符的优先级高于它, 就要反复弹出栈顶操作符, 加入到输出列表末尾
        # 直到栈顶操作符的优先级低于它

        else:
            # 注意条件prec[op_stack.peek()] >= prec[token], 不是 > ,是 >=,因为+- 或*/是同一级
            while (not op_stack.isEmpty()) and (prec[op_stack.peek()] >=
                                                prec[token]):
                post_fix_lst.append(op_stack.pop())
            op_stack.push(token)

    # 扫描结束后,把opstack栈中的所有剩余操作符依次弹出,加入到输出列表末尾
    # 把输出列表再用join()合并成后缀表达式字符串
    while not op_stack.isEmpty():
        post_fix_lst.append(op_stack.pop())

    return " ".join(post_fix_lst)
コード例 #4
0
 def convert_to_base(self, base):
     decimal = self.a
     s = Stack()
     while decimal > 0:
         decimal, remender = divmod(decimal, base)
         s.push(remender)
     n = ""
     while not s.isEmpty():
         n += Digit.digits[s.pop()]
     self.a = n
コード例 #5
0
ファイル: 10num_to_2num.py プロジェクト: hbinr/DSA_study
def divideBy2(decNumber):
    """
    10进制数转2进制
    @num: 需要转换的数
    @return: 转换后的结果
    """
    remainder_stack = Stack()

    while decNumber > 0:
        remainder = decNumber % 2
        remainder_stack.push(remainder)
        decNumber //= 2  # 整除

    result = ''
    while not remainder_stack.isEmpty():
        result += str(remainder_stack.pop())

    return result
コード例 #6
0
ファイル: 10num_to_2num.py プロジェクト: hbinr/DSA_study
def baseConvert(decNumber, base):
    """
    10进制数转成任意16进制以下的数

    @decNumber: 需要转换的数
    @base: 进制
    @return: 转换后的结果
    """
    digits = '0123456789ABCDEF'  # 定义数范围(最大16进制)

    remainder_stack = Stack()

    while decNumber > 0:
        remainder = decNumber % base
        remainder_stack.push(remainder)
        decNumber //= base  # 整除

    result = ''
    while not remainder_stack.isEmpty():
        result += digits[remainder_stack.pop()]

    return result