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 D (1 ≤ D ≤ 50), representing the number of datasets.
A list of strings F of size D, where each string is up to 80 characters long and consists 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(D, F) that takes in the input.
def f(D: int, F: List[str]):
'''
D: an integer
F: a list of strings
'''
Test Case Generator
Certainly! Here's a test case generator for the Reverse Polish Notation (RPN) problem:
```python
import random
def generate_rpn_expression():
operations = ['+', '-', '*', '/']
stack = []
result = []
num_operations = random.randint(1, 20)
# Ensure valid RPN by alternating between numbers and operations
for _ in range(num_operations):
if len(stack) < 2:
number = random.randint(-100, 100)
stack.append(number)
result.append(str(number))
else:
if random.random() < 0.5:
# Add a number
number = random.randint(-100, 100)
stack.append(number)
result.append(str(number))
else:
# Add an operation
operation = random.choice(operations)
result.append(operation)
# Pop two numbers from the stack and push the result
num2 = stack.pop()
num1 = stack.pop()
if operation == '+':
stack.append(num1 + num2)
elif operation == '-':
stack.append(num1 - num2)
elif operation == '*':
stack.append(num1 * num2)
elif operation == '/':
# Ensure no division by zero
if num2 == 0:
num2 = 1
stack.append(num1 / num2)
# Ensure the result RPN expression is valid by adding any remaining operations
while len(stack) > 1:
operation = random.choice(operations)
result.append(operation)
num2 = stack.pop()
num1 = stack.pop()
if operation == '+':
stack.append(num1 + num2)
elif operation == '-':
stack.append(num1 - num2)
elif operation == '*':
stack.append(num1 * num2)
elif operation == '/':
if num2 == 0:
num2 = 1
stack.append(num1 / num2)
return ' '.join(result)
def tcgen():
num_datasets = random.randint(1, 50)
test_cases = [generate_rpn_expression() for _ in range(num_datasets)]
return test_cases
# Example usage
for test_case in tcgen():
print(test_case)
```
### Explanation:
1. **generate_rpn_expression**: This function generates a single valid RPN expression:
- It uses a stack to help ensure the expression remains valid.
- It alternates between adding numbers and operations, ensuring at least two numbers are available for operations.
- It handles edge cases such as ensuring no division by zero.
2. **tcgen**: This function generates the specified number of test cases (1 to 50):
- Each test case is generated by calling `generate_rpn_expression`.
### Example Output:
```
10 2 12 - /
3 4 - 7 2 3 * + *
-1 -2 3 + +
```
This output mimics the format of the sample input provided in your problem statement. Each line is a valid RPN expression that can be evaluated.
Extract Arguments
def extract_arguments(fh):
formulas = []
for line in fh:
formulas.append(line.strip())
return formulas