Contest problems hand you the input as text and expect exact output text. A real contest program reads ALL of that text from standard input, works out the answer, and prints it. This unit teaches that pipeline — read stdin → parse → print — one step at a time.
-
Read a whole contest input from standard input with
import sysanddata = sys.stdin.read(). -
Split the text into tokens with
.split()and turn number tokens into integers withint(). -
Read a count
Nfollowed byNvalues, and rebuild anRbyCgrid as a list of lists. -
Print output in the exact form the judge expects, with single spaces between values.
-
Lesson 1 — Read values
The Real Program Reads stdin
A contest submission begins by reading everything from standard input into one string:
import sys data = sys.stdin.read()The rest of the unit turns that one string into numbers and grids. In the notebook we practice the parsing on a literal
data = "..."string (there is no contest input waiting here); the full programs at the end read real stdin and are run from a terminal..split()breaks text on any whitespace (spaces and newlines) into a list of string tokens.data = "3\n4 9 2\n" tokens = data.split() print(tokens)Scroll the code for more ↓
Output
['3', '4', '9', '2'].split()ignores the newline and the trailing space —"3\n4 9 2\n"becomes['3', '4', '9', '2'], four tokens.token = "42" number = int(token) print(number + 8)Scroll the code for more ↓
Output
50A token is still a STRING — call
int(token)before doing arithmetic.data = "4\n7 3 8 5" tokens = data.split() n = int(tokens[0]) values = [] for position in range(n): values.append(int(tokens[position + 1])) print(values)Scroll the code for more ↓
Output
[7, 3, 8, 5]The common pattern —
Nfirst, thenNvalues: readtokens[0]asn, then positions1..n.values = [6, 10, 14] output = "" for position in range(len(values)): if position > 0: output = output + " " output = output + str(values[position]) print(output)Scroll the code for more ↓
Output
6 10 14Build exact output by adding a single space only BETWEEN values (not before the first).
Put it together: the full program reads the whole input from stdin, parses
Nand the values, and prints their sum and maximum separated by one space.Try it import sys data = sys.stdin.read() tokens = data.split() n = int(tokens[0]) values = [] for position in range(n): values.append(int(tokens[position + 1])) total = sum(values) biggest = max(values) print(f"{total} {biggest}")Scroll the code for more ↓
Run the full solver from this unit folder:
python assets/l1.py < assets/l1/1.inThe same parsing on real stdin — read, parse N and the values, print
sum maximum.Complexity: building the list and its sum/max is
O(n).Lesson 2 — Read a grid
A grid begins with
R C: the number of rows and columns, thenRrows ofCintegers. Build it one step at a time.data = "2 2\n3 1\n4 2" tokens = data.split() rows = int(tokens[0]) columns = int(tokens[1]) print(rows, columns)Scroll the code for more ↓
Output
2 2The header
R Cis just the first two tokens — read them before any grid values.position = 2 row = [] for column in range(columns): row.append(int(tokens[position])) position = position + 1 print(row)Scroll the code for more ↓
Output
[3, 1]One row is
Cvalues read with a movingpositioncounter —positionkeeps its place in the token list across rows.tokens = "2 2\n3 1\n4 2".split() rows = int(tokens[0]) columns = int(tokens[1]) position = 2 grid = [] for r in range(rows): current_row = [] for c in range(columns): current_row.append(int(tokens[position])) position = position + 1 grid.append(current_row) print(grid) print(grid[1][1])Scroll the code for more ↓
Output
[[3, 1], [4, 2]] 2Nested loops build a list-of-lists;
grid[r][c]reads one cell (heregrid[1][1]is2).Put it together: the full program reads an
R×Cgrid from stdin and prints each row's sum, separated by spaces.Try it import sys data = sys.stdin.read() tokens = data.split() rows = int(tokens[0]) columns = int(tokens[1]) grid = [] position = 2 for r in range(rows): row = [] for c in range(columns): row.append(int(tokens[position])) position = position + 1 grid.append(row) answer = "" for r in range(rows): if r > 0: answer = answer + " " answer = answer + str(sum(grid[r])) print(answer)Scroll the code for more ↓
Run the full solver from this unit folder:
python assets/l2.py < assets/l2/1.inRead
R C, rebuild the grid, print one row-sum per row.Complexity: reading every cell is
O(R*C).Judges compare output EXACTLY. Test the edges: a single value, a 1-row or 1-column grid, and a value at the last position — these catch off-by-one mistakes on
Nor the final row/column.-
A contest program reads ALL of its input from stdin as one string, then parses it and prints the answer.
-
.split()breaks text on any spaces or newlines, and each token stays a string until you callint(). -
A moving
positioncounter walks through the tokens: firstN(orR C), then the values or grid rows. -
grid[r][c]reads one cell of a list-of-lists grid built with nested loops.
-
- Judges compare output exactly, so test the edges: one value, one row or column, and the last position.
You can now write a program that reads a contest input, parses it and prints the exact answer.