Orig Description
Problem D: Cheat Case
Problem
Faced with an upcoming math test and certain that even studying now won't save us from failing, we decide to cheat.
Our math teacher, teacher A, is known for hand-writing the math test papers. Our plan is to steal the brush-strokes used by the teacher while creating the test's problems with a pen that can wiretap the brush strokes, a device made by a professor in our neighborhood. However, the professor was unable to create a program to interpret the brush-strokes and reconstruct the problems. Worse, even if the problems could be reconstructed, we wouldn't be smart enough to solve them. Out of options, we decide to ask you, a programmer, to create a program that can interpret brush-strokes and solve the resulting problems.
This test will have problems where you'll have to solve arithmetic expressions. The arithmetic symbols are only + (addition), - (subtraction), and · (multiplication); multiplication takes priority over addition and subtraction. The expressions will always have the correct syntax. In addition, there will not be any numbers other than 0 with a leading 0.
The paper on which the problems are written can be mapped as a grid. The brush-strokes are given as a set of line segments, where each segment is either horizontal or vertical along the X-axis or has the same starting and ending point (a dot). This line segment represents filling ...
Extracted Specification
Canonicalized Input Description:
An integer N (1 ≤ N ≤ 10^3), representing some quantity or size.
A list of tuples, where each tuple contains:
- Two integers x1, y1 (0 ≤ x1, y1 ≤ 10^3), representing coordinates.
- Two integers x2, y2 (0 ≤ x2, y2 ≤ 10^3), representing coordinates.
### Example Input:
```
5
0 0 1 0
1 0 1 1
1 1 2 1
1 1 1 1
2 0 2 2
```
### Function Signature:
Write a function f(N, segments) that takes in the input.
```python
def f(N: int, segments: List[Tuple[int, int, int, int]]):
'''
N: an integer
segments: a list of tuples, each containing four integers
'''
```
Test Case Generator
Sure, I can help with that. Let's create a test case generator for "Problem D: Cheat Case". This problem involves interpreting brush-strokes on a grid to reconstruct arithmetic expressions and then solving them. Here is a suitable test case generator:
```python
import random
def generate_expression():
# Generates a random arithmetic expression with +, -, and * operators
operators = ['+', '-', '*']
expression = ""
# Length of the expression (number of numbers and operators)
length = random.randint(3, 10) # At least one number, one operator, and another number
for i in range(length):
if i % 2 == 0:
# Add a number
expression += str(random.randint(0, 99))
else:
# Add an operator
expression += random.choice(operators)
# Ensure the expression does not end with an operator
if expression[-1] in operators:
expression = expression[:-1] + str(random.randint(0, 99))
return expression
def generate_brush_strokes(expression):
# Generates brush strokes for the given expression on a grid
grid_size = 100 # Define a fixed grid size for simplicity
brush_strokes = []
x, y = 0, 0 # Starting point in the grid
for char in expression:
if char.isdigit():
for digit in char:
brush_strokes.append((x, y, x, y)) # Dot for each digit
x += 1 # Move to the next column
else:
if char == '+':
brush_strokes.append((x, y, x + 1, y)) # Horizontal stroke for '+'
elif char == '-':
brush_strokes.append((x, y, x + 2, y)) # Horizontal stroke for '-'
elif char == '*':
brush_strokes.append((x, y, x, y + 1)) # Vertical stroke for '*'
x += 3 # Move to the next column after operator
return brush_strokes, grid_size
def tcgen():
expression = generate_expression()
brush_strokes, grid_size = generate_brush_strokes(expression)
return grid_size, brush_strokes
# Example usage
grid_size, brush_strokes = tcgen()
print(f"Grid Size: {grid_size}")
for stroke in brush_strokes:
print(stroke)
```
### Explanation:
1. **generate_expression Function**: This function generates a random arithmetic expression using the operators `+`, `-`, and `*`. The length of the expression varies between 3 and 10 characters to ensure a mix of numbers and operators.
2. **generate_brush_strokes Function**: This function converts the generated arithmetic expression into brush strokes on a fixed grid. Each digit is represented as a dot, and each operator is represented with horizontal or vertical strokes. The starting point for each character is incremented to ensure non-overlapping strokes.
3. **tcgen Function**: This function calls `generate_expression` and `generate_brush_strokes` to create a complete test case, returning the grid size and the list of brush strokes.
This generator should provide a variety of test cases for the given problem statement.
Extract Arguments
def extract_arguments(fh):
M = int(fh.readline().strip())
brush_strokes = []
for _ in range(M):
brush_strokes.append(tuple(map(int, fh.readline().strip().split())))
return M, brush_strokes
# if __name__ == "__main__":
# input_path = sys.argv[1]
# with open(input_path, 'r') as fh:
# M, brush_strokes = extract_arguments(fh)
# f(M, brush_strokes)