Orig Description
Formula for beginners
Doctor: Peter, I finally did it!
Peter: What is it this time? What kind of pointless invention did you come up with now?
Doctor: I came up with a revolutionary method for processing formulas with a computer. Take a look at this table.
Normal notationDoctor's "revolutionary" notation
1 + 21 2 +
3 * 4 + 73 4 * 7 +
10 / ( 2 - 12 ) 10 2 12 - /
( 3 - 4 ) * ( 7 + 2 * 3 )3 4 - 7 2 3 * + *
Peter: Huh?
Doctor: Hahaha. I guess it's still too complicated for you, a beginner. This is where it gets interesting.
Peter: Uh, what...?
Doctor: You know that computers have a data structure called a stack, right? It's the one where you put in things in first and take them out last.
Peter: Yes, I know, but...
Doctor: This revolutionary notation uses that stack. For example, in 10 2 12 - /, the processing goes like this:
Object10212-/
↓↓↓↓2-12↓10/-10
Stack
.
.
10
.
2
10
12
2
10
.
-10
10
.
.
-1
Doctor: See? You don't need to worry about parentheses or operator precedence. The word order is "divide 10 by the result of subtracting 2 from 12." It sounds a bit like Japanese, doesn't it? With this revolutionary invention, our research lab is safe and sound. Hahaha.
Peter: Uh, Doctor? This is something we learned in the basic course at Aizu University when I was in Japan. They called it "Reverse Polish Notation," and everyone in the class could program it easily.
Doctor: ...
And so, Peter had to teach the program to the Doctor. Create a program that takes a formula written in Reverse Polish Notation as input and outputs the result of the calculation.
Input
Multiple datasets will be given. In each dataset, one line will be given, consisting of a formula written in Reverse Polish Notation (a string of up to 80 characters consisting of integers and operation symbols separated by a single space (half-width) character). No formulas that divide a value by 0 or a value close to 0 will be given.
The number of datasets is no more than 50.
Output
For each dataset, output the result of the calculation (a real number) on one line. The calculation result may include an error of up to 0.00001.
Sample Input
10 2 12 - /
3 4 - 7 2 3 * + *
-1 -2 3 + +
Output for the Sample Input
-1.000000
-13.000000
0.000000
Extracted Specification
An integer \(N\) (1 ≤ \(N\) ≤ 50), representing the number of datasets.
A list of strings \(L\) of size \(N\), where each string represents a sequence of integers and operations in Reverse Polish Notation (a string of up to 80 characters consisting of integers and operation symbols separated by a single space).
### Example Input:
```
3
10 2 12 - /
3 4 - 7 2 3 * + *
-1 -2 3 + +
```
### Function Signature:
Write a function f(N, L) that takes in the input.
def f(N: int, L: List[str]):
'''
N: an integer
L: a list of strings
'''
Test Case Generator
Sure, I can help you write a test case generator for the given problem statement "Formula for beginners". Let's break down the requirements and generate the test cases accordingly.
### Requirements:
1. Each dataset consists of a formula written in Reverse Polish Notation (RPN).
2. Each formula is a string of up to 80 characters consisting of integers and operation symbols separated by a single space.
3. There are no formulas that divide by 0 or a value close to 0.
4. The number of datasets is no more than 50.
### Operations in RPN:
- Addition: `+`
- Subtraction: `-`
- Multiplication: `*`
- Division: `/`
### Test Case Generator:
We will generate random formulas in RPN and ensure they are valid and do not perform invalid operations like division by zero.
```python
import random
def generate_rpn_expression(max_length=80):
"""Generate a valid RPN expression"""
operators = ['+', '-', '*', '/']
stack = []
expression = []
while len(expression) < max_length:
if len(stack) < 2 or (random.random() > 0.5 and len(stack) < max_length - len(expression) - 1):
# Push a number onto the stack
num = random.randint(-10**4, 10**4)
stack.append(num)
expression.append(str(num))
else:
# Pop two numbers and push an operator
if len(stack) < 2:
break
b = stack.pop()
a = stack.pop()
operator = random.choice(operators)
if operator == '/' and b == 0:
# Prevent division by zero
b = random.randint(1, 10**4)
result = eval(f"{a} {operator} {b}")
stack.append(result)
expression.append(operator)
return ' '.join(expression)
def tcgen():
num_datasets = random.randint(1, 50)
test_cases = [generate_rpn_expression() for _ in range(num_datasets)]
return test_cases
# Example of generating test cases
if __name__ == "__main__":
test_cases = tcgen()
for test_case in test_cases:
print(test_case)
```
### Explanation:
1. **generate_rpn_expression**: Generates a single valid RPN expression.
- It uses a stack to maintain valid intermediate states.
- It ensures that we don't perform division by zero.
- It keeps the length of the expression within a specified maximum length (default 80 characters).
2. **tcgen**: Generates a list of RPN expressions.
- It randomly determines the number of datasets (between 1 and 50).
- It generates the required number of valid RPN expressions.
This generator ensures that each test case follows the problem constraints and is ready for use in testing the RPN evaluation program.
Extract Arguments
def extract_arguments(fh):
inputs = []
for line in fh:
inputs.append(line.strip())
return inputs