Ejemplo n.º 1
0
def is_balanced(head: Node) -> bool:
    n = head
    s = Stack()
    balanced = True
    while (n != None and balanced):
        if n.record == "(":
            s.push(n.record)
        else:
            if (s.isEmpty()):
                balanced = False
            else:
                s.pop()
        n = n.next

    if balanced and s.isEmpty():
        return True
    else:
        return False
Ejemplo n.º 2
0
def dec_to_n(input: int, base: int): #-> str:
    s = Stack()
    if (base < 2 or base > 16):
        print("Error: Invalid base.")
        return
    else:
        units = __units(base)
        while(input > 0):
            rem = input % base
            transform_rem = rem
            if base > 10 :
                transform_rem = units[rem]
            s.push(transform_rem)
            input = input // base
    
    # compose string
    acc = []
    while (not s.isEmpty()):
        acc.append(s.pop())

    return ''.join(str(i) for i in acc)
Ejemplo n.º 3
0
def __construct(s: Stack) -> str:
    # compose string
    acc = ""
    while not s.isEmpty():
        acc = acc + str(s.pop())
    return acc