Recover Missing Matrix Cells
Python
Medium
3 views
Problem Description
Read r c and then r lines of matrix. Some lines may have less than c numbers. Treat missing cells as 0. Output sum of each column.
Input Format
First line r c. Next r lines (can be short).
Output Format
One line of c sums.
Official Solution
import sys
lines=sys.stdin.read().splitlines()
if not lines: sys.exit(0)
r,c=map(int,lines[0].split())
col=[0]*c
for i in range(r):
parts=(lines[1+i] if 1+i<len(lines) else '').split()
for j in range(c):
try:
col[j]+=int(parts[j])
except Exception:
col[j]+=0
sys.stdout.write(' '.join(map(str,col)))
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!