Sum a Spreadsheet Column

Medium · rating 1250 · strings, implementation

The first line contains a 0-based column index k and a row count n. Each of the next n lines is a comma-separated row of integers. Print the sum of the values in column k.

Editorial

Sum a Spreadsheet Column adds the values in one column of comma-separated rows. Split each row on commas, take the k-th field, convert it to a number, and accumulate.

total = 0
for row in rows:
    total += int(row.split(',')[k])
print(total)

Complexity

Time: O(n × cols). Space: O(cols) per row.

Open Sum a Spreadsheet Column in Code Arena →