Contest Python: ACSL › Unit 0 — ACSL Foundations
Exercises: Unit 0 — ACSL Foundations
There are two kinds of exercises here, just as on an ACSL contest.
- Short-answer (marked short answer): a program and its input are given. Trace the program by hand and write down exactly what it prints. Do not type the program in and run it until you have committed to an answer; tracing is the skill the contest tests.
- Programming: write a complete program that reads the input with
input()and prints exactly the output described. Save it as a.pyfile and run it from a terminal, typing the sample input or feeding it a file:python my_solution.py < assets/ex4/1.in. Print nothing extra: no prompts ininput(), and no words the problem did not ask for.
Each exercise is marked with the lowest ACSL division it belongs to: Junior, Intermediate or Senior. Do every exercise at or below your division. Exercises marked Challenge are extra practice for when you want more.
Exercise 1: Quotient and Remainder
line = input()
parts = line.split()
a = int(parts[0])
b = int(parts[1])
print(a // b, a % b, a - b * 2)
Input
17 5
What exactly does the program print? Work it out by hand first; write the output exactly as it appears.
How this is checked: Checked by comparing your answer with the expected answer.
Answer format: Exactly the one line the program outputs: values separated by one space, strings without quotation marks, and letters in the case the program prints them.
Exercise 2: Letter at a Position
The input is one line: a word in capital letters, then a space, then a whole number K.
Print the letter at position K of the word, counting the first letter as position 1.
Constraints
- The word has between 1 and 20 capital letters and no spaces.
1 <= K <=the length of the word.
Sample Input
COMPUTER 4
Sample Output
P
How this is checked: Checked by running your program on 4 test inputs (1 sample, 3 hidden) and comparing its output line by line.
Exercise 3: Glue or Add?
parts = input().split()
x = parts[0] + parts[1]
y = int(parts[0]) + int(parts[1])
print(x, y)
Input
40 2
What exactly does the program print? Work it out by hand first; write the output exactly as it appears.
How this is checked: Checked by comparing your answer with the expected answer.
Answer format: Exactly the one line the program outputs: values separated by one space, strings without quotation marks, and letters in the case the program prints them.
Exercise 4: Spread of the Scores
The first line holds N, the number of scores.
The second line holds the N scores, separated by spaces.
Print the largest score minus the smallest score.
Constraints
1 <= N <= 100- Every score is between 0 and 1000.
Sample Input
5
72 95 60 88 81
Sample Output
35
How this is checked: Checked by running your program on 4 test inputs (1 sample, 3 hidden) and comparing its output line by line.
Exercise 5: Evens and Odds Until Zero
The input holds positive whole numbers, one per line, and ends with a line holding 0.
The 0 is not part of the data.
Print how many of the numbers are even and how many are odd, in that order, separated by one space.
Constraints
- There are between 1 and 100 numbers before the
0. - Every number before the
0is between 1 and 1000.
Sample Input
14
3
8
21
6
0
Sample Output
3 2
How this is checked: Checked by running your program on 4 test inputs (1 sample, 3 hidden) and comparing its output line by line.
Exercise 6: Trace the Sentinel Loop
total = 0
count = 0
value = int(input())
while value != 0:
if value > count:
total = total + value
count = count + 1
value = int(input())
print(count, total)
Input
5
1
2
9
0
What exactly does the program print? Work it out by hand first; write the output exactly as it appears.
How this is checked: Checked by comparing your answer with the expected answer.
Answer format: Exactly the one line the program outputs: values separated by one space, strings without quotation marks, and letters in the case the program prints them.
Exercise 7: Honor Roll
The first line holds N, the number of students.
Each of the next N lines holds a student's name and score, separated by one space.
Print the names of every student whose score is at least 80, on one line, in the order they appear in the input, separated by single spaces.
If no student scored at least 80, print NONE.
Constraints
1 <= N <= 50- Each name is one word of letters, with no spaces.
- Every score is between 0 and 100.
Sample Input
4
Ada 91
Ben 79
Cy 80
Dee 64
Sample Output
Ada Cy
How this is checked: Checked by running your program on 4 test inputs (1 sample, 3 hidden) and comparing its output line by line.
Exercise 8: Lines That End in Zero
The first line holds K, the number of lists.
Each of the next K lines is one list: some positive whole numbers separated by spaces, always ending with a 0 that is not part of the list.
Print the total of each list, in order, on one line, separated by single spaces.
Constraints
1 <= K <= 20- Each list has between 1 and 30 numbers before its
0. - Every number before a
0is between 1 and 1000.
Sample Input
3
4 9 2 0
10 0
1 1 1 1 0
Sample Output
15 10 4
How this is checked: Checked by running your program on 4 test inputs (1 sample, 3 hidden) and comparing its output line by line.
Exercise 9: Tuple Trouble
parts = input().split()
a = int(parts[0])
b = int(parts[1])
c = int(parts[2])
a, b = b, a
b, c = c, b + a
print(a, b, c)
Input
3 8 5
What exactly does the program print? Work it out by hand first; write the output exactly as it appears.
How this is checked: Checked by comparing your answer with the expected answer.
Answer format: Exactly the one line the program outputs: values separated by one space, strings without quotation marks, and letters in the case the program prints them.
Exercise 10: Farthest From Home
A robot starts at home, the point (0, 0), and can move only along the grid lines.
The grid distance from home to the point (x, y) is abs(x) + abs(y).
The first line holds N, the number of points.
Each of the next N lines holds one point as two integers x y.
Store the points as tuples.
Print the point with the largest grid distance from home, as x y.
If several points tie, print the one that appears first in the input.
Constraints
1 <= N <= 100- Every coordinate is between -1000 and 1000.
Sample Input
4
2 3
-4 2
0 -6
1 1
Sample Output
-4 2
How this is checked: Checked by running your program on 4 test inputs (1 sample, 3 hidden) and comparing its output line by line.
Exercise 11: Out-of-Order Pairs
The first line holds N.
The second line holds N integers.
A pair of positions i and j, with i to the left of j, is out of order when the value at i is greater than the value at j.
Try every pair and print how many pairs are out of order.
Constraints
1 <= N <= 500- Every value is between -1000 and 1000.
Sample Input
5
3 1 4 1 5
Sample Output
3
The out-of-order pairs are 3 1 (positions 1 and 2), 3 1 (positions 1 and 4) and 4 1 (positions 3 and 4).
How this is checked: Checked by running your program on 5 test inputs (1 sample, 4 hidden) and comparing its output line by line.
Exercise 12: Count the Tens
values = []
for part in input().split():
values.append(int(part))
count = 0
last = (0, 0)
for i in range(len(values)):
for j in range(i + 1, len(values)):
if values[i] + values[j] == 10:
count = count + 1
last = (i + 1, j + 1)
print(count, last[0], last[1])
Input
3 7 5 5 2 8
What exactly does the program print? Work it out by hand first; write the output exactly as it appears.
How this is checked: Checked by comparing your answer with the expected answer.
Answer format: Exactly the one line the program outputs: values separated by one space, strings without quotation marks, and letters in the case the program prints them.
Exercise 13: Best Pair Product
The first line holds N.
The second line holds N integers.
Choose two values at different positions so that their product is as large as possible.
Print that largest product.
Constraints
2 <= N <= 500- Every value is between -1000 and 1000.
Sample Input
5
4 -7 5 1 -8
Sample Output
56
The best choice is -7 and -8, because two negative numbers multiply to a positive one.
How this is checked: Checked by running your program on 6 test inputs (1 sample, 5 hidden) and comparing its output line by line.
Exercise 14: Making Change
A shop has coins worth A, B and C cents, and plenty of each.
In how many ways can you pay exactly T cents?
A way is a choice of how many coins of each value to use: whole numbers x, y and z, each 0 or more, with A*x + B*y + C*z equal to T.
Two ways are different if any of the three counts is different.
The input is one line holding A B C T.
Print the number of ways.
Constraints
1 <= A, B, C <= 500 <= T <= 300
Sample Input
5 10 25 30
Sample Output
5
The five ways, as (x, y, z), are (6, 0, 0), (4, 1, 0), (2, 2, 0), (0, 3, 0) and (1, 0, 1).
How this is checked: Checked by running your program on 6 test inputs (1 sample, 5 hidden) and comparing its output line by line.
Exercise 15: Triangle Sticks
The first line holds N, the number of sticks.
The second line holds the N stick lengths.
Three sticks at three different positions make a triangle when each length is less than the sum of the other two.
Try every group of three positions.
Print two numbers separated by one space: how many groups make a triangle, and the largest perimeter (sum of the three lengths) among those groups.
If no group makes a triangle, print 0 0.
Constraints
3 <= N <= 60- Every length is between 1 and 1000.
Sample Input
5
2 3 4 5 10
Sample Output
3 12
The triangles are 2 3 4, 2 4 5 and 3 4 5; the largest perimeter is 3 + 4 + 5 = 12.
2 3 5 is not a triangle, because 5 is not less than 2 + 3.
How this is checked: Checked by running your program on 5 test inputs (1 sample, 4 hidden) and comparing its output line by line.