Bloomberg Interview Question: Process CSV Files in Folder

11 Views
No Comments
def process_path(path: str) -> int:
    """Process all CSV files inside `path` and its subdirectories"""
    pass
There are a number of CSV files scattered throughout a folder and its subfolders,
containing information about trades. Implement the function process_path
which takes a path to the folder and finds all of the CSV files and sum 
all of the values in the second column. The second column always contains integers.

For example, given a folder like

.
|-- a
|   `-- b
|       `-- ex.csv
|-- example.csv
`-- ignoreme.log

where:

# ex.csv:
a,5,-1,-1,0
a,10,-1,-1,0

# example.csv:
b,0,-1,-1,0
b,-3,-1,-1,0

It should return 12    (5 + 10 + 0 + 3)

You are asked to implement a function that recursively walks through a directory and all its subdirectories, finds every .csv file, reads each line, extracts the second column, and sums all integer values found there. Files with other extensions should be ignored. The second column is guaranteed to contain integers. The final result is the total sum across all CSVs.

END
 0