예제 #1
0
def is_balanced(ip):
    s = Stack()
    match_pair = {')': '(', ']': '[', '}': '{'}

    for item in ip:
        if item == '{' or item == '(' or item == '[':
            s.push(item)
        else:
            if match_pair[item] == s.peak():
                s.pop()
            else:
                return False
    '''
    Return True if stack is empty, False otherwise
    '''
    return not s.size()
예제 #2
0
def sort_stack(ip):
    result = Stack()
    result.push(ip.pop())
    while ip.size() > 0:
        el = ip.pop()
        count = 0
        print(result.stack)
        while result.peak() > el:
            print('Inside inner while')
            ip.push(result.pop())
            count += 1
        result.push(el)

        for item in range(count):
            result.push(ip.pop())

    return result