Contest Python: USACO Bronze › Unit 1 — Reading the Input
Exercises: Unit 1 — Reading the Input
Each problem is a program that reads the whole input from stdin and prints the exact output (real contest style — no solve() wrapper): import sys; data = sys.stdin.read(), parse it, print the answer. Write each program in a .py file and run it with piped input, e.g. python my_solution.py < case.txt.
Exercise 1: Sum and Maximum
The first line is N. The second line contains N integers. Print their sum and maximum, separated by one space.
Constraints
1 <= N <= 1000- Every value is between 0 and 10000.
Sample Input
3
4 9 2
Sample Output
15 9
How this is checked: Checked by running your program on 2 test inputs (1 sample, 1 hidden) and comparing its output word by word.
Exercise 2: Above the Threshold
The first line contains N T. The second line contains N integers. Print how many of those integers are greater than T.
Constraints
1 <= N <= 10000 <= T <= 10000- Every value is between 0 and 10000.
Sample Input
5 6
2 9 6 10 4
Sample Output
2
How this is checked: Checked by running your program on 3 test inputs (1 sample, 2 hidden) and comparing its output word by word.
Exercise 3: Per-Line Sums
The first line is K. Each of the next K lines begins with a count M, followed by M integers. Print one sum for each of those lines, separated by spaces and kept in the same order.
Constraints
1 <= K <= 1001 <= M <= 100on every list line- Every value is between 0 and 1000.
Sample Input
3
3 2 4 6
2 10 5
4 1 1 2 3
Sample Output
12 15 7
How this is checked: Checked by running your program on 2 test inputs (1 sample, 1 hidden) and comparing its output word by word.
Exercise 4: Find the Target
The first line contains N T. The second line contains N integers. Scan the values with a loop. If T is present, print YES and the first 1-based position, separated by one space. Otherwise, print NO.
Constraints
1 <= N <= 1000- Every value and
Tis between 0 and 10000.
Sample Input
5 8
3 8 4 8 1
Sample Output
YES 2
How this is checked: Checked by running your program on 4 test inputs (1 sample, 3 hidden) and comparing its output word by word.
Exercise 5: Grid Row Sums
The first line contains R C. The next R lines each contain C integers. Build the grid as a list of lists. Print the sum of each row, separated by spaces.
Constraints
1 <= R <= 1001 <= C <= 100- Every grid value is between 0 and 1000.
Sample Input
2 3
1 2 3
4 5 6
Sample Output
6 15
How this is checked: Checked by running your program on 2 test inputs (1 sample, 1 hidden) and comparing its output word by word.
Exercise 6: Grid Column Sums
The first line contains R C. The next R lines each contain C integers. Build the grid as a list of lists. Print the sum of each column from left to right, separated by spaces.
Constraints
1 <= R <= 1001 <= C <= 100- Every grid value is between 0 and 1000.
Sample Input
2 3
1 2 3
4 5 6
Sample Output
5 7 9
How this is checked: Checked by running your program on 2 test inputs (1 sample, 1 hidden) and comparing its output word by word.
Exercise 7: Double Every Number
The first line is N. The second line contains N integers. Double each integer and print all doubled values in order, separated by single spaces. Build the output with a loop.
Constraints
1 <= N <= 1000- Every value is between 0 and 10000.
Sample Input
4
3 1 5 2
Sample Output
6 2 10 4
How this is checked: Checked by running your program on 2 test inputs (1 sample, 1 hidden) and comparing its output word by word.
Exercise 8: Echo the Chosen Name
The header line contains K P: the number of names and a 1-based position. The next K lines each contain one name with no spaces. Print the name at position P.
Constraints
1 <= K <= 10001 <= P <= K- Each name contains only letters and has no spaces.
Sample Input
4 3
Ari
Bo
Chen
Dee
Sample Output
Chen
How this is checked: Checked by running your program on 2 test inputs (1 sample, 1 hidden) and comparing its output word by word.
Exercise 9: Grid Border Sum
The first line contains R C. The next R lines each contain C integers. Build the grid as a list of lists. Print the sum of the outer-ring values: the first row, last row, first column, and last column. Count each corner once.
Constraints
2 <= R <= 1002 <= C <= 100- Every grid value is between 0 and 1000.
Sample Input
3 4
1 2 3 4
5 6 7 8
9 10 11 12
Sample Output
65
How this is checked: Checked by running your program on 2 test inputs (1 sample, 1 hidden) and comparing its output word by word.