def brackets_matching(character):
    "多类型括号匹配"

    s = Stack()
    balance = True

    symbol = ['(', '[', '{']

    for i in character:
        if i in symbol:
            #print(i)
            s.push(i)
        else:
            if s.isEmpty():
                balance = False
                break
            else:
                pop = s.pop()
                if (pop == '(' and i == ')') or (pop == '{' and i == '}') or (
                        pop == '[' and i == ']'):
                    pass
                else:
                    balance = False
                    break

    if not s.isEmpty():
        balance = False

    return balance
def bracket_matching(character):
    "单一符号匹配"

    balance = True
    s = Stack()

    for i in character:
        #print(i)
        if i == '(':
            s.push(i)
        if i == ')':
            if s.isEmpty():
                balance = False
                break
            else:
                s.pop()

    if not s.isEmpty():
        balance = False

    return balance
Пример #3
0
def is_valid_parenthesis(str):
    stack = Stack()
    is_valid = True

    for i in range(0, len(str)):
        if str[i] in "{[(" and is_valid:
            stack.push(str[i])
        else:
            if stack.is_Empty():
                is_valid = False
            else:
                top = stack.pop()
                if not matching(top, str[i]):
                    is_valid = False
    return stack, is_valid
def binary_Conversion(decNumber, base):
    "将十进制数转换成任意进制"

    s = Stack()

    while decNumber > 0:
        rem = decNumber % base
        s.push(rem)
        decNumber = decNumber // base

    "拼接字符串"
    string = ""
    while not s.isEmpty():
        string = string + str(s.pop())

    return string
Пример #5
0
def parChecker_1(symbolString):
    s = Stack()
    # print Stack()
    print "the symbol collection we need to check are : %s " % symbolString
    balanced = True
    index = 0
    while index < len(symbolString) and balanced:
        symbol = symbolString[index]
        if symbol == "(":
            s.push(symbol)
        else:
            if s.isEmpty():
                balanced = False
            else:
                s.pop()

        index = index + 1

    if balanced and s.isEmpty():
        return True
    else:
        return False
Пример #6
0
def parChecker(symbolStr) :
	print "the parentheses we need to check are : ", symbolStr
	s = Stack()
	# print Stack()
	balanced = True
	index = 0
	while index < len(symbolStr) and balanced:
		symbol = symbolStr[index]
		if symbol == "(" :
			s.push(symbol)
		else : # the symbol is not "(", it could be ")", or something else, of which we do not care
			if s.isEmpty():
				balanced = False
			else :
				s.pop()
		
		index = index + 1

	if balanced and s.isEmpty :
		return True
	else :
		return False
Пример #7
0
from python_stack import Stack

s = Stack()

s.push(1)
s.push(2)
s.push(3)

print(s.isEmpty())

print(s.size())

print(s.peek())

print(s.pop())

print(s.peek())



    def calPoints(self, ops):
        s = Stack()
        count = 0

        for i in ops:
            if i == "+":
                last = s.pop()
                sec = s.peek()
                s.push(last)
                s.push(last + sec)
            elif i == "D":
                s.push(s.peek() * 2)
            elif i == "C":
                s.pop()
            else:
                s.push(int(i))

        for i in range(s.size()):
            count += s.pop()

        return count
    def backspaceCompare(self, S, T):
        s1 = Stack()
        s2 = Stack()
        str1 = ""
        str2 = ""

        for i in S:
            if i == "#" and not s1.isEmpty():
                s1.pop()
            elif i == "#" and s1.isEmpty():
                pass
            else:
                s1.push(i)

        for j in T:
            if j == "#" and not s2.isEmpty():
                s2.pop()
            elif j == "#" and s2.isEmpty():
                pass
            else:
                s2.push(j)

        for k in range(s1.size()):
            str1 = str1 + s1.pop()
        for n in range(s2.size()):
            str2 = str2 + s2.pop()

        return str1 == str2