Orig Description
JPEG Compression
The fundamental idea in the JPEG compression algorithm is to sort coeffi-
cient of given image by zigzag path and encode it. In this problem, we don’t
discuss about details of the algorithm, but you are asked to make simple pro-
gram. You are given single integer N , and you must output zigzag path on
a matrix where size is N by N . The zigzag scanning is start at the upper-left
corner (0, 0) and end up at the bottom-right corner. See the following Figure
and sample output to make sure rule of the zigzag scanning. For example, if you
are given N = 8, corresponding output should be a matrix shown in right-side
of the Figure. This matrix consists of visited time for each element.
Input
Several test cases are given. Each test case consists of one integer N (0 < N <
10) in a line. The input will end at a line contains single zero.
Output
For each input, you must output a matrix where each element is the visited
time. All numbers in the matrix must be right justified in a field of width 3.
Each matrix should be prefixed by a header “Case x:” where x equals test case
number.
Sample Input
3
4
0
Output for the Sample Input
Case 1:
1 2 6
3 5 7
4 8 9
Case 2:
1 2 6 7
3 5 8 13
4 9 12 14
10 11 15 16
Extracted Specification
An integer N (0 < N < 10).
### Example Input:
```
3
4
0
```
### Function Signature:
Write a function f(inputs) that takes in the input.
def f(inputs: List[int]):
'''
inputs: a list of integers
'''
Test Case Generator
Certainly! Below is a test case generator for the JPEG Compression problem. This generator will create random test cases based on the given constraints and format them as specified.
```python
import random
def generate_zigzag_matrix(N):
if N == 0:
return []
matrix = [[0] * N for _ in range(N)]
i, j = 0, 0
direction = 1 # 1 for up-right, -1 for down-left
for num in range(1, N * N + 1):
matrix[i][j] = num
if direction == 1:
if j + 1 < N and i - 1 >= 0:
i -= 1
j += 1
elif j + 1 < N:
j += 1
direction = -1
else:
i += 1
direction = -1
else:
if i + 1 < N and j - 1 >= 0:
i += 1
j -= 1
elif i + 1 < N:
i += 1
direction = 1
else:
j += 1
direction = 1
return matrix
def tcgen():
test_cases = []
num_cases = random.randint(1, 10)
for _ in range(num_cases):
N = random.randint(1, 9)
test_cases.append(N)
test_cases.append(0) # End of input signal
return test_cases
def format_output(test_cases):
results = []
case_num = 1
for N in test_cases:
if N == 0:
break
matrix = generate_zigzag_matrix(N)
result = f"Case {case_num}:"
for row in matrix:
result += "\n" + " ".join(f"{val:3}" for val in row)
results.append(result)
case_num += 1
return "\n\n".join(results)
# Example usage:
test_cases = tcgen()
print("Sample Input")
for case in test_cases:
print(case)
print("\nSample Output")
print(format_output(test_cases))
```
### Explanation:
1. **generate_zigzag_matrix(N)**:
- This function generates the zigzag matrix for a given size \(N \times N\).
- The traversal starts from the top-left corner and follows the zigzag pattern until the bottom-right corner.
2. **tcgen()**:
- This function generates random test cases. It creates a random number of test cases between 1 and 10, and for each test case, it picks a random \(N\) between 1 and 9 (since \(N\) should be \(0 < N < 10\)).
- Adds an additional test case with 0 to signal the end of input.
3. **format_output(test_cases)**:
- This function formats the output as specified in the problem statement.
- For each test case, it generates the zigzag matrix and formats it with the specified width.
- It prefixes each matrix with "Case x:", where x is the test case number.
### Usage:
- **Sample Input** is printed first, followed by the generated test cases.
- **Sample Output** is printed next, showing the formatted matrices for each test case.
You can run this code snippet to generate random test cases and see the corresponding outputs formatted as required.
Extract Arguments
def extract_arguments(fh):
inputs = []
while True:
N = int(fh.readline().strip())
if N == 0:
break
inputs.append((N,))
return inputs