Problem p00167 - Generation 1

Orig Description

Bubble Sort
Sorting algorithms are essential basic algorithms in computer science for arranging data. For example, sorting an array of integer values in ascending order is a sorting operation, as shown in the figure below.
Many sorting algorithms have been developed, but one of the basic algorithms is bubble sort. For example, let's try to arrange an given array of integers in ascending order using bubble sort.
In bubble sort, at each calculation step, the array is divided into a "sorted part" and an "unsorted part". At the beginning, the entire array becomes the unsorted part.
Starting from the beginning of the unsorted part, adjacent elements are compared (green elements in the figure), and they are swapped so that the larger value comes to the right. If the two values are equal, they are not swapped.
Repeat this process until the end of the unsorted part (white elements in the figure). Finally, add the end to the sorted part (blue elements), and one step is completed.
Repeat this step until the length of the unsorted part becomes 1.
When the length of the unsorted part becomes 1, the sorting process is finished.
Now, create a program that takes an array of n numbers as input and sorts them in ascending order from the beginning of the array using the above bubble sort procedure, and outputs the number of array element swaps required.
Input
The input consists of multiple datasets. The end of the input is indicated by a single zero.
Each dataset is given in the following format.
n
a1
a2
:
an
The first line contains the number of numbers n (1 ≤ n ≤ 100), and the following n lines contain the i-th number ai (1 ≤ ai ≤ 1000000).
The number of datasets does not exceed 20.
Output
For each dataset, output the number of swaps required to sort the data elements in one line.
Sample Input
5
5
3
2
1
4
6
1
2
3
4
5
6
3
3
2
1
0
Output for the Sample Input
7
0
3

Extracted Specification

An integer n (1 ≤ n ≤ 100), followed by n integers, each on a new line (1 ≤ each integer ≤ 1000000). The input consists of multiple datasets. The end of the input is indicated by a single zero. The number of datasets does not exceed 20.

### Example Input:

```
5
5
3
2
1
4
6
1
2
3
4
5
6
3
3
2
1
0
```

### Function Signature:
Write a function f(inputs) that takes in the input:
```python
def f(inputs: List[Tuple[int, List[int]]]):
    '''
    inputs: a list of tuples, each containing an integer and a list of integers
    '''
```

Test Case Generator

Sure, I can help you write a test case generator for the "Bubble Sort" problem. Below is the Python code for generating test cases:

```python
import random

def tcgen():
    datasets = []
    
    num_datasets = random.randint(1, 20)  # Number of datasets (1 to 20)
    
    for _ in range(num_datasets):
        n = random.randint(1, 100)  # Number of elements in the dataset (1 to 100)
        a = [random.randint(1, 1000000) for _ in range(n)]  # Elements (1 to 1000000)
        datasets.append((n, a))
    
    return datasets

def format_datasets(datasets):
    output = []
    for n, a in datasets:
        output.append(str(n))
        output.extend(map(str, a))
    output.append("0")
    return "\n".join(output)

# Example usage
datasets = tcgen()
formatted_datasets = format_datasets(datasets)
print(formatted_datasets)
```

### Explanation:

1. **`tcgen` Function**:
   - Generates a random number of datasets (between 1 and 20).
   - For each dataset, generates a random number of elements `n` (between 1 and 100).
   - For each element, generates a random integer between 1 and 1,000,000.
   - Collects these datasets in a list.

2. **`format_datasets` Function**:
   - Takes the list of datasets and formats them into the required input format.
   - Adds the terminating zero at the end of the input.

### Example Usage:

- The `tcgen` function generates the test datasets.
- The `format_datasets` function formats these datasets into the input format expected by the problem.

This generator will help you create multiple random test cases to thoroughly test your bubble sort implementation.

Extract Arguments

def extract_arguments(fh):
    datasets = []
    while True:
        n = int(fh.readline().strip())
        if n == 0:
            break
        a = [int(fh.readline().strip()) for _ in range(n)]
        datasets.append((n, a))
    return datasets

# if __name__ == "__main__":
#     input_path = sys.argv[1]
#     with open(input_path, 'r') as fh: 
#         inputs = extract_arguments(fh)
#         f(inputs)