| f | def itercalc(): | f | def itercalc(): |
| n | stack = [] | n | stack_values = [] |
| cmd = (yield) | | command = (yield) |
| while True: | | while True: |
| n | if cmd == '?': | n | if command == '?': |
| if stack: | | if stack_values: |
| cmd = (yield stack[-1]) | | command = (yield stack_values[-1]) |
| else: | | else: |
| print('Insufficient stack') | | print('Insufficient stack') |
| n | cmd = (yield None) | n | command = (yield None) |
| continue | | continue |
| try: | | try: |
| n | stack.append(int(cmd)) | n | stack_values.append(int(command)) |
| cmd = (yield None) | | command = (yield None) |
| continue | | continue |
| except Exception: | | except Exception: |
| pass | | pass |
| n | if cmd in {'+', '-', '*', '/'}: | n | if command in {'+', '-', '*', '/'}: |
| if len(stack) < 2: | | if len(stack_values) < 2: |
| print('Insufficient stack') | | print('Insufficient stack') |
| n | cmd = (yield None) | n | command = (yield None) |
| continue | | continue |
| n | b = stack.pop() | n | operand_b = stack_values.pop() |
| a = stack.pop() | | operand_a = stack_values.pop() |
| if cmd == '+': | | if command == '+': |
| res = a + b | | result = operand_a + operand_b |
| elif cmd == '-': | | elif command == '-': |
| res = a - b | | result = operand_a - operand_b |
| elif cmd == '*': | | elif command == '*': |
| res = a * b | | result = operand_a * operand_b |
| elif cmd == '/': | | elif command == '/': |
| if b == 0: | | if operand_b == 0: |
| print('Zero division') | | print('Zero division') |
| n | stack.extend([a, b]) | n | stack_values.extend([operand_a, operand_b]) |
| cmd = (yield None) | | command = (yield None) |
| continue | | continue |
| n | res = a // b | n | result = operand_a // operand_b |
| stack.append(res) | | stack_values.append(result) |
| cmd = (yield None) | | command = (yield None) |
| continue | | continue |
| print('Unknown command') | | print('Unknown command') |
| t | cmd = (yield None) | t | command = (yield None) |