#P5483. CSV Files Horizontal Merge

    ID: 18715 Type: Default 1000ms 256MiB

CSV Files Horizontal Merge

CSV Files Horizontal Merge

A company’s product market manager is tired of manually merging CSV files. Each CSV file contains rows of data with comma-separated values, and the manager wants to merge these files horizontally into one CSV file.

The merging process is as follows:

  • For each CSV file, determine the maximum number of columns. In the final output, the first row is a header row. For each file, output its file name in the first cell and output empty cells for the remaining columns so that there are exactly m cells, where m is the maximum number of columns for that file. Formally, if a file has a maximum of \(m\) columns, then its header row will be:</p>

    [ \text{[filename, ; ; , ; ; , ; ... , empty]} \quad (\text{total } m \text{ cells}) ]

  • After the header row, merge the data rows of each file by aligning them horizontally. For each row index, copy the corresponding row from each CSV file in order (from left to right). If a file has fewer rows than the maximum, treat the missing rows as rows filled with empty cells. Also, if a particular row in a file has fewer than \(m\) cells, pad it with empty cells to have exactly \(m\) cells.
  • The output CSV is produced by concatenating these cells using commas. Although the input CSV files are guaranteed not to have trailing commas, the output must explicitly include commas for trailing empty cells. Furthermore, the next file’s first column should be immediately to the right of the previous file’s last non-empty column, and the last file should only be output up to its last non-empty column.

Input Format: The first line contains an integer \(N\) representing the number of CSV files. For each file, the input contains:

  1. A line with the file name.
  2. A line with an integer \(R\) denoting the number of rows in the CSV file.
  3. \(R\) lines, each containing a comma-separated row of data (with no trailing comma).

Output Format: Output the merged CSV file as described above.

inputFormat

The input begins with an integer \(N\) denoting the number of CSV files. Then, for each file:

  1. A line containing the file name.
  2. A line containing an integer \(R\) representing the number of rows in this CSV file.
  3. \(R\) lines where each line is a CSV formatted row (comma separated values with no trailing comma).

outputFormat

Output the merged CSV file as follows:

  • The first row is a header row. For each file, output the file name in the first cell and then output empty cells so that the total number of cells equals the maximum number of columns in that file.
  • For the subsequent rows, merge the corresponding rows of each file (padding with empty cells as necessary if a file has fewer cells or rows).
  • Each row is a comma-separated string; if a cell is empty, it is still represented (i.e. trailing commas may appear).

sample

2
a
2
a1,b1,c1
a2,b2
b
4
a1,b1,c1,d1
a2,b2
a3,b3,c3
a4
a,,,b,,,

a1,b1,c1,a1,b1,c1,d1 a2,b2,,a2,b2,, ,,,a3,b3,c3, ,,,a4,,,

</p>