Contest Python: USACO Bronze

unit

Unit 1 — Reading the Input

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.

You will learn

  • Read a whole contest input from standard input with import sys and data = sys.stdin.read().
  • Split the text into tokens with .split() and turn number tokens into integers with int().
  • Read a count N followed by N values, and rebuild an R by C grid 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)

Output

['3', '4', '9', '2']

Notice

.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)

Output

50

Notice

A 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)

Output

[7, 3, 8, 5]

Notice

The common pattern — N first, then N values: read tokens[0] as n, then positions 1..n.

values = [6, 10, 14]
output = ""
for position in range(len(values)):
    if position > 0:
        output = output + " "
    output = output + str(values[position])
print(output)

Output

6 10 14

Notice

Build 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 N and the values, and prints their sum and maximum separated by one space.

Try it yourself

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}")

Run the full solver from this unit folder:

python assets/l1.py < assets/l1/1.in

Notice

The 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, then R rows of C integers. 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)

Output

2 2

Notice

The header R C is 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)

Output

[3, 1]

Notice

One row is C values read with a moving position counter — position keeps 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])

Output

[[3, 1], [4, 2]]
2

Notice

Nested loops build a list-of-lists; grid[r][c] reads one cell (here grid[1][1] is 2).

Put it together: the full program reads an R×C grid from stdin and prints each row's sum, separated by spaces.

Try it yourself

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)

Run the full solver from this unit folder:

python assets/l2.py < assets/l2/1.in

Notice

Read 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 N or the final row/column.

Recap

  • 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 call int().
  • A moving position counter walks through the tokens: first N (or R 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.

Go to the exercises (9)

For parents and teachers: report a problem