Orig Description
()
Problem Statement
Given a string S. Initially, S is an empty string.
You perform the following operation n times in order:
Add x_i occurrences of p_i (which is either "(" or ")") to the end of S.
Determine whether or not S becomes a balanced string after the operations.
A string is balanced if:
The empty string is balanced.
If a and b are balanced strings, then a + b is also balanced.
If a is a balanced string, then "(" + a + ")" is also balanced.
Input
The input is in the following format. All numbers given are integers.
n
p_1 x_1
. . .
p_n x_n
Constraints
1≦n≦1,000
1≦x_i≦10^6
p_i is either "(" or ")".
Output
If S becomes a balanced string, print "YES" on a line. Otherwise, print "NO" on a line.
Sample Input 1
3
( 5
) 4
) 1
Output for the Sample Input 1
YES
The string S="((((()))))" is balanced.
Sample Input 2
5
( 2
) 2
( 3
) 1
) 2
Output for the Sample Input 2
YES
Sample Input 3
2
) 1
( 1
Output for the Sample Input 3
NO
Extracted Specification
An integer n (1 ≤ n ≤ 1,000), representing some quantity or size.
A list of n tuples, where each tuple contains:
- A character p from the set {'(', ')'}
- An integer x (1 ≤ x ≤ 10^6), representing a count
### Example Input:
```
3
( 5
) 4
) 1
```
### Function Signature:
Write a function f(inputs: List[Tuple[str, int]]) that takes in the input.
def f(inputs: List[Tuple[str, int]]):
'''
inputs: a list of tuples, each containing a character and an integer
'''
Test Case Generator
Sure! Here’s a test case generator for the given problem statement:
```python
import random
def tcgen():
# Generate n, the number of operations
n = random.randint(1, 1000)
operations = []
for _ in range(n):
# Randomly choose between '(' and ')'
p = random.choice(['(', ')'])
# Generate x_i, the number of occurrences (1 ≤ x_i ≤ 10^6)
x = random.randint(1, 1000000)
operations.append((p, x))
return n, operations
# Function to print the test case in the required format
def print_test_case():
n, operations = tcgen()
print(n)
for op in operations:
print(op[0], op[1])
# Example of generating and printing a test case
print_test_case()
```
### Explanation
1. **Generate `n`**: The number of operations is randomly chosen between 1 and 1000 (inclusive).
2. **Generate operations**: For each operation, randomly choose between `'('` and `')'`, and generate a random integer `x` between 1 and 1,000,000 (inclusive).
3. **Store operations**: Each operation is stored as a tuple `(p, x)` where `p` is either `'('` or `')'`, and `x` is the number of occurrences.
4. **Print the test case**: The function `print_test_case()` prints the generated test case in the required format.
You can use the `print_test_case()` function to generate and output a new test case, which can be helpful for testing purposes.
Extract Arguments
def extract_arguments(fh):
n = int(fh.readline().strip())
operations = []
for _ in range(n):
p, x = fh.readline().strip().split()
x = int(x)
operations.append((p, x))
return n, operations
# if __name__ == "__main__":
# input_path = sys.argv[1]
# with open(input_path, 'r') as fh:
# n, operations = extract_arguments(fh)
# f(n, operations)