14720 - Domo the manager   

Description

WiIth the help of I2P(I) students our good friend Domo received a promotion and now is a manager at his company that stores employee information in a CSV (comma-separated values) file. Where each row represents one employee, and each column contains different details such as name, age, and city. Now that our friend is a manager he has a lot more responsibilities to be done.

Previously we only need to extract certain columns to create a smaller report, now Domo needs to be able to combine certain columns, modify its "header" name, and finally merge it to our original csv string.

Write a program that can combine selected columns and modify our own header name which will be merged into our original csv string as a new column. 
 

def merge_col(current_csv, extracted_col):
    #TODO: Merge the combined columns to our original csv string
   
def combine_col(csv, indices, col_name):
    #TODO: Combine the extracted columns with the modified header
   
# Read the CSV input until EOF
csv_lines = []
while True:
    try:
        line = input()
        if line.strip() == "":
            continue
        csv_lines.append(line)
    except EOFError:
        break
#
num_col = int(csv_lines[0])                     #first line number of col
indices = csv_lines[1:num_col+1]                #indices
col_name = csv_lines[num_col+1: 2*num_col+1]    #new colname
 
# Remaining lines = CSV data
csv_str = "\n".join(csv_lines[2*num_col+1:])
new_csv = csv_str
for i in range(num_col):
   #TODO: use the functions to create our new csv string

print(new_csv)

'''
print(num_col)
print(indices)
print(col_name)
print(csv_str)
'''

Input

  • An integer n, where n is the number of new columns that will be merged.
  • Each of the next n lines contains the indices of the columns (separated by spaces) to be combined for that new column.
  • Each of the next n lines contains one new header name.
  • Followed by the CSV content, where:
  • Rows are separated by newline \n Columns are separated by commas ,
  • The first row of the CSV is the header (column names)


Constraints

  • 1 ≤ Number of rows ≤ 1000
  • 1 ≤ Number of columns ≤ 50 Column indices are 0-based Indices CAN be out of the range of the columns
  • To signal EOF you can use Ctrl + Z + Enter (Windows) or Ctrl + D (Linux/Mac) 

Output

  • Output the new CSV string that have been merged with the combined columns.
  • Output Format:
  • Each column must be separated by a comma (',')
  • Each row of the output must end with a newline character ('\n')
  • Do not add extra space at the end of any line For the combined column, combine it with a blank character between each words (' ')
  • Each out-of-range indice is replaced by "N/A" individually. (Indices will be considered out-of-range if the given indices exceed the number of column of the ORIGINAL csv)
  • The very last line of your output should also end with exactly one newline ('\n')

Sample Input  Download

Sample Output  Download

Tags




Discuss