예제 #1
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())
예제 #2
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()
예제 #3
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()
예제 #4
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()
예제 #5
0
파일: rpn_092.py 프로젝트: 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()
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
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
예제 #8
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()
예제 #9
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())
예제 #10
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()
예제 #11
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
예제 #12
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
예제 #13
0
파일: rpn_092.py 프로젝트: NiallS4/CA117
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()