Orig Description
Frequency of appearance operation
There is a transformation operation for a finite sequence called a frequency of appearance operation. The result of transforming the sequence S = {s1, s2, ..., sn} is a sequence of the same length. If we define the result as C = {c1, c2, ..., cn}, then ci represents the number of times si appears in the sequence S.
For example, if S = {3, 4, 1, 5, 9, 2, 6, 5, 3}, then C = {2, 1, 1, 2, 1, 1, 1, 2, 2}. Performing a frequency of appearance operation on this sequence C gives us P = {4, 5, 5, 4, 5, 5, 5, 4, 4}. This sequence does not change with frequency of appearance operations. Such a sequence P is called a fixed point of the sequence S. It is known that for any sequence, the fixed point can be obtained by repeating the frequency of appearance operation.
The example below shows the procedure for the frequency of appearance operation. The first line represents the sequence S, the second line represents the sequence C, and the last line represents the sequence P. We count the number of occurrences and find ci by listing the number of times the first element (s1=2) in the sequence S appears in the same way as the second element (s2=7) with two elements.
Write a program that takes the length of the sequence n and the sequence S as input and outputs the fixed-point sequence P and the minimum number of frequency of appearance operations required to obtain P.
Input
Multiple datasets are given. Each dataset is given in the following format.
n
s1 s2 ... sn
The first line contains an integer n (n ≤ 12) representing the length of the sequence. The second line contains integers si (1 ≤ si ≤ 100) representing the elements of the sequence S separated by spaces.
The input ends with a line containing a single 0. There are no more than 200 datasets.
Output
For each dataset, output the minimum number of times the frequency of appearance operation is performed (an integer) on the first line and the elements p1, p2, ..., pn of the corresponding fixed-point sequence P on the second line, separated by spaces.
Sample Input
10
4 5 1 1 4 5 12 3 5 4
0
Output for the Sample Input
3
6 6 4 4 6 6 4 4 6 6
Extracted Specification
An integer n (n ≤ 12), representing some quantity or size.
A list of integers s of size n, where each integer is between 1 and 100.
The input ends with a line containing a single 0. There are no more than 200 datasets.
### Example Input:
```
10
4 5 1 1 4 5 12 3 5 4
0
```
### Function Signature:
Write a function f(inputs) that takes in the input.
def f(inputs: List[Tuple[int, List[int]]]):
'''
inputs: a list of tuples, where each tuple consists of:
- an integer
- a list of integers
'''
Test Case Generator
Sure! Let's write a test case generator for the given problem statement. The generator will produce valid test cases based on the constraints provided.
Here is the Python test case generator:
```python
import random
def tcgen():
# Generate a random number of datasets
num_datasets = random.randint(1, 200)
datasets = []
for _ in range(num_datasets):
# Generate a random length for the sequence, n
n = random.randint(1, 12)
# Generate the sequence S of length n with elements between 1 and 100
S = [random.randint(1, 100) for _ in range(n)]
# Append the dataset to the list
datasets.append((n, S))
# Add the end marker
datasets.append((0,))
return datasets
# Example usage
if __name__ == "__main__":
test_cases = tcgen()
for case in test_cases:
if case[0] == 0:
print(0)
else:
n, S = case
print(n)
print(" ".join(map(str, S)))
```
This script generates random test cases according to the constraints provided in the problem statement. Each dataset consists of a sequence length \(n\) and a sequence \(S\). The test cases end with a line containing a single 0 to signify the end of input.
### Explanation:
1. **Number of Datasets**: The generator creates a random number of datasets (between 1 and 200).
2. **Sequence Length \(n\)**: For each dataset, a random sequence length \(n\) is generated (between 1 and 12).
3. **Sequence \(S\)**: For each sequence, \(n\) integers are generated, each between 1 and 100.
4. **Output Format**: The datasets are printed in the required format, with each dataset's length \(n\) and sequence \(S\) printed on separate lines, followed by a final line containing a single 0.
You can run this script to generate random test cases for the problem. The output will be a series of datasets formatted correctly, ready to be used for testing the solution to the problem.
Extract Arguments
def extract_arguments(fh):
datasets = []
while True:
n = int(fh.readline().strip())
if n == 0:
break
S = list(map(int, fh.readline().strip().split()))
datasets.append((n, S))
return datasets
# if __name__ == "__main__":
# input_path = sys.argv[1]
# with open(input_path, 'r') as fh:
# datasets = extract_arguments(fh)
# for dataset in datasets:
# n, S = dataset
# f(n, S)