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