Esempio n. 1
0
def matcher(line):
    left = "({["
    right = "]})"
    square = "[]"
    curl = "{}"
    roun = "()"
    s = Stack()
    for char in line:
        try:
            if char in left:
                s.push(char)

            elif char in right and s.top() in square and char in square:
                s.pop()

            elif char in right and s.top() in curl and char in curl:
                s.pop()

            elif char in right and s.top() in roun and char in roun:
                s.pop()

        except IndexError:
            return False

    return s.is_empty()
def matcher(l):
    s = Stack()
    for c in l:
        if c in lefties:
            s.push(c)
        try:
            if c in righties and s.top() == dictr[c]:
                s.pop()
        except IndexError:
            return False
    if s.is_empty():
        return True
    return False
def matcher(s):
    stack = Stack()
    for e in s:
        if e in lefties:
            stack.push(e)
        try:
            if e in righties and stack.top() == mapping[e]:
                stack.pop()
        except IndexError:
            return False
    if stack.is_empty():
        return True
    return False
Esempio n. 4
0
def matcher(line):
    d = {')': '(', '}': '{', ']': '['}

    s = Stack()

    for c in line:
        if c in d.values():
            s.push(c)

        elif c in d.keys():
            if s.is_empty():
                return False
            if s.top() != d[c]:
                return False
            s.pop()

    return s.is_empty()
Esempio n. 5
0
def matcher(l):
    if len(l) % 2 == 1:
        return False
    l = list(l)
    l.sort()
    l = "".join(l)
    lefts = "{[("
    rights = "}])"

    s = Stack()
    i = 0
    while i < len(l):
        if l[i] in lefts:
            s.push(l[i])
        elif l[i] in rights and len(s.l) > 0:
            s.pop()
        i += 1
    return s.is_empty()
Esempio n. 6
0
def matcher(line):

    s = Stack()

    lefties = '({['
    righties = ')}]'
    brackets = ['()', '{}', '[]']
    line = line.strip()

    try:
        for pattern in line:
            if pattern in lefties:
                s.push(pattern)
            else:
                if s.top() + pattern in brackets:
                    s.pop()
        return s.is_empty()
    except:
        return False
Esempio n. 7
0
def matcher(line):

    s = Stack()

    left = '({['
    right = ')}]'
    brackets = ['()', '{}', '[]']
    line = line.strip()

    try:
        for ch in line:
            if ch in left:
                s.push(ch)
            else:
                if s.top() + ch in brackets:
                    s.pop()
        return s.is_empty()
    except:
        return False
Esempio n. 8
0
def matcher(line):
    s = Stack()
    for char in line:
        if char == "(" or char == "{" or char == "[":
            s.push(char)
        elif char == ")" or char == "}" or char == "]":
            try:
                if not s.is_empty:
                    return False
                elif char == ")" and s.top() == "(":
                    s.pop()
                elif char == "}" and s.top() == "{":
                    s.pop()
                elif char == "]" and s.top() == "[":
                    s.pop()
                else:
                    return False
            except:
                return False
    return s.is_empty()
Esempio n. 9
0
def main():

    s = Stack()

    print(len(s))
    s.push(1)
    print(s.top())
    print(s.is_empty())
    print(s.pop())
    print(s.is_empty())
    try:
        print(s.pop())
    except IndexError:
        print('Error')
    try:
        print(s.top())
    except IndexError:
        print('Error')
    s.push(1)
    s.push(2)
    s.push(3)
    print(len(s))
    print(s.pop())
    print(s.pop())
    print(s.pop())
    print(s.is_empty())
Esempio n. 10
0
def calculator(l):
    s = Stack()
    for i in l.split():
        if i in binops.keys():
            y = s.pop()
            x = s.pop()
            s.push(binops[i](x, y))
        elif i in uniops.keys():
            s.push(uniops[i](s.pop()))
        else:
            s.push(float(i))
    return s.top()
Esempio n. 11
0
File: rpn_092.py Progetto: cawnj/ca1
def calculator(line):
    stack = Stack()
    for e in line.split():
        if e in binops.keys():
            y = stack.pop()
            x = stack.pop()
            stack.push(binops[e](x, y))
        elif e in uniops.keys():
            stack.push(uniops[e](stack.pop()))
        else:
            stack.push(float(e))
    return stack.top()
Esempio n. 12
0
def calculator(s):
    stack, q = Stack(), Queue()
    s = s.split()

    i = 0
    while i < len(s) and ifl(s[i]):
        stack.push(float(s[i]))
        i += 1

    for c in s[i:]:
        q.enqueue(c)

    while len(q) > 0:
        calc, n1 = q.dequeue(), stack.pop()

        if calc == 'r': stack.push(n1**(1 / 2))
        elif calc.isalpha(): stack.push(n1 * -1)
        else: stack.push(eval(str(stack.pop()) + calc + str(n1)))

    return stack.pop()
Esempio n. 13
0
def calculator(line):

    changers_b = {
        '+': lambda a, b: a + b,
        '-': lambda a, b: a - b,
        '*': lambda a, b: a * b,
        '/': lambda a, b: a / b,
        '%': lambda a, b: a % b,
        '**': lambda a, b: a**b
    }

    changers_u = {
        'n': lambda a: -a,
        's': lambda a: a * a,
        'r': lambda a: math.sqrt(a),
        'l': lambda a: math.log(a)
    }

    stack = Stack()
    line = line.strip().split()

    for i in range(0, len(line)):

        if line[i] in changers_b:
            try:
                one = float(stack.pop())
                two = float(stack.pop())
                stack.push(changers_b[line[i]](two, one))

            except (ValueError, IndexError):
                break

        elif line[i] in changers_u:

            try:
                one = float(stack.pop())
                stack.push(changers_u[line[i]](one))

            except ValueError:
                break

        elif line[i].split('.')[0].isdigit():
            stack.push(line[i])

    if len(stack) == 1:
        return float(stack.l[0])

    else:
        raise IndexError("What's up bro?")
Esempio n. 14
0
def calculator(line):
    chars = line.split()

    s = Stack()

    for c in chars:
        if is_num(c):
            s.push(float(c))
        elif c in un:
            s.push(float(uniops[c](s.pop())))
        elif c in binops:
            b = s.pop()
            s.push(float(binops[c](s.pop(), b)))

    return s.top()
Esempio n. 15
0
def calculator(line):
    s = Stack()
    normalop = ["+", "-", "*", "/"]
    specialop = ["r", "n"]
    l = line.split()
    for char in l:
        if char not in normalop and char not in specialop:
            s.push(char)
        elif char in normalop:
            p1 = float(s.pop())
            p2 = float(s.pop())
            s.push(normal(char, p1, p2))
        elif char in specialop:
            p = float(s.pop())
            s.push(special(char, p))
    return float(s.top())
Esempio n. 16
0
def calculator(line):
    def plus(a, b):
        return a + b

    def subtract(a, b):
        return a - b

    def product(a, b):
        return a * b

    def divide(a, b):
        return a / b

    def remainder(a, b):
        return a % b

    def power(a, b):
        return a**b

    def neg(a):
        return -a

    def square(a):
        return a**2

    def sqrt(a):
        return math.sqrt(a)

    def log(a):
        return math.log(a)

    binops = {
        '+': plus,
        '-': subtract,
        '*': product,
        '/': divide,
        '%': remainder,
        '**': power,
    }

    uniops = {
        'n': neg,
        's': square,
        'r': sqrt,
        'l': log,
    }

    s = Stack()
    for n in line.split():
        if n.replace('.', '').isdigit():
            s.push(float(n))
        elif n in binops:
            b = s.pop()
            a = s.pop()
            c = float(binops[n](a, b))
            s.push(c)
        elif n in uniops:
            a = s.pop()
            b = uniops[n](a)
            s.push(b)
    return s.top()
Esempio n. 17
0
def calculator(line):
    items = line.split()
    nums = Stack()
    for k in items:
        if k.isdecimal() or "." in k:
            nums.push(float(k))
        elif k == "*":
            fin = nums.l[-2] * nums.l[-1]
            nums.pop()
            nums.pop()
            nums.push(fin)
        elif k == "+":
            fin = nums.l[-2] + nums.l[-1]
            nums.pop()
            nums.pop()
            nums.push(fin)
        elif k == "-":
            fin = nums.l[-2] - nums.l[-1]
            nums.pop()
            nums.pop()
            nums.push(fin)
        elif k == "/":
            fin = nums.l[-2] / nums.l[-1]
            nums.pop()
            nums.pop()
            nums.push(fin)
        elif k == "r":
            fin = sqrt(nums.l[-1])
            nums.pop()
            nums.push(fin)
        elif k == "n":
            fin = -nums.l[-1]
            nums.pop()
            nums.push(fin)

    return nums.l[0]
Esempio n. 18
0
def calculator(line):
    line = line.strip().split()
    op = ["+", "-", "/", "r", "n", "*"]
    if len(line) == 1:
        return float(line[0])
    stack = Stack()
    operation = []
    for b in line:
        num = 0
        if set(b).issubset(("0123456789.")):
            stack.push(float(b))
        else:
            operation.append(b)

    i = 0
    while i < len(operation):
        if operation[i] == "+":
            s = stack.pop()
            t = stack.pop()
            stack.push((s + t))
        elif operation[i] == "-":
            s = stack.pop()
            t = stack.pop()
            stack.push((t - s))
        elif operation[i] == "/":
            s = stack.pop()
            t = stack.pop()
            stack.push((t / s))
        elif operation[i] == "n":
            s = stack.pop()
            stack.push(-1 * s)
        elif operation[i] == "r":
            s = stack.pop()
            stack.push(sqrt(s))
        elif operation[i] == "*":
            s = stack.pop()
            t = stack.pop()
            stack.push((s * t))
        i += 1

    return stack.pop()