/
GlebBavykin
/
python_sketches
Обзор
Документация
Войти
/
GlebBavykin
/
python_sketches
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
main
algorithms/polish_notation.py
35 строк
966 B
Gleb Bavykin
add mypy to uv
07 июл 2026, 21:33
07 июл 2026, 21:33
91c60eb
Код
Авторство
О чём код?
from collections import deque def evaluate_pn(expression: str, reverse=True) -> int: """ Evaluate polish expressions and return result """ stack: deque = deque() tokens = expression.split() if not reverse: tokens = tokens[::-1] for char in tokens: if char == "+": a, b = int(stack.pop()), int(stack.pop()) stack.append(a + b) elif char == "-": a, b = int(stack.pop()), int(stack.pop()) if reverse: stack.append(b - a) else: stack.append(a - b) elif char == "/": a, b = int(stack.pop()), int(stack.pop()) if reverse: stack.append(b / a) else: stack.append(a / b) elif char == "*": a, b = int(stack.pop()), int(stack.pop()) stack.append(a * b) else: stack.append(char) return stack.pop()