14695 - Domo the employee   

Description

Our good friend Domo is an employee at a company that stores employee information in a CSV (comma-separated values) file. Each row represents one employee, and each column contains different details such as name, age, and city.

However, sometimes your friend only needs certain columns to create a smaller report (for example, only the name and city). Instead of doing this manually, they asked you to write a program that automatically extracts the requested columns from the CSV data.

You are given the CSV content as a string and a list of column indices to keep. Write a program to return a new CSV string that contains only the selected columns.

 

You may start with the given code

def extract_columns(csv: str, indices: str):
    #TODO:  

# Read the CSV input until EOF
csv_lines = []
while True:
    try:
        line = input()
        if line.strip() == "":
            continue
        csv_lines.append(line)
    except EOFError:
        break

# First line = indices
ind_line = csv_lines[0]
ind = list(map(int, ind_line.split()))

# Remaining lines = CSV data
csv_str = "\n".join(csv_lines[1:])

# Process
print(extract_columns(csv_str, ind_line))

Please Submit the whole code, not only the functions

Input

  • The first line of the input contains the space-separated list of column indices to extract.

  • 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 and valid

  • 0 <= indices < Number of columns

To signal EOF you can use Ctrl + Z + Enter (Windows) or Ctrl + D (Linux/Mac) 

Output

Output the CSV string containing only the selected columns, with rows preserved.

Output Format:

  • Each column must be seperated by a comma (',')
  • Each row of the output must end with a newline character ('\n') 
  • The very last line of your output should also end with exactly one newline ('\n')
  • Do not add extra space at the end of any line

Sample Input  Download

Sample Output  Download

Tags




Discuss