Contest Python: USACO Bronze › Unit 2 — Boolean Logic & Algebra
Exercises: Unit 2 — Boolean Logic & Algebra
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, decide, print. Write each program in a .py file and run it with piped input, e.g. python my_solution.py < case.txt.
Exercise 1: Ride Height Check
The input contains three integers: height age adult. The height is in centimetres, age is in years, and adult is 1 if an adult rides along or 0 if not. A rider may board exactly when they are at least 120 cm tall, and they are also at least 10 years old or riding with an adult. Print BOARD if the rider may board; otherwise, print WAIT.
Constraints
50 <= height <= 2501 <= age <= 100adultis0or1.
Sample Input
125 8 1
Sample Output
BOARD
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 2: De Morgan in Action
The input contains Boolean flags a b, written as 1 or 0. Compute not (a and not b). De Morgan's law says this is exactly the same as (not a) or b, so you may use whichever form is easier to read — both give the same answer for every row. Print TRUE or FALSE.
Constraints
aandbare each0or1.
Sample Input
1 0
Sample Output
FALSE
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 3: At Least One Signal
The first value is N, followed by N flags. Print YES if at least one flag is 1; otherwise, print NO. Scan the flags with a manual loop and a Boolean flag variable.
This sample intentionally puts the decisive 1 in the last position. A loop that stops one position early will incorrectly print NO.
Constraints
1 <= N <= 1000- Every flag is
0or1.
Sample Input
5
0 0 0 0 1
Sample Output
YES
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: Every Check Passed
The first value is N, followed by N flags from a safety checklist. Print YES only when every flag is 1; otherwise, print NO. Scan the flags with a manual loop and a Boolean flag variable.
Constraints
1 <= N <= 1000- Every flag is
0or1.
Sample Input
4
1 1 0 1
Sample Output
NO
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 5: Majority with a Dissenter
The first value is N, followed by N answers, each written as X or O. Print YES if more than half of the answers are X and at least one answer is O. Otherwise, print NO.
Constraints
1 <= N <= 1000- Every answer is exactly
XorO.
Sample Input
5
X X O X X
Sample Output
YES
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 6: Club Vote
The first two values are N Q: the number of members present and the number required for a quorum. They are followed by N votes, each YES or NO. Print PASS if at least Q members are present and strictly more than half of the votes are YES; otherwise, print FAIL.
Constraints
1 <= N <= 10001 <= Q <= 1000- Every vote is exactly
YESorNO.
Sample Input
7 6
YES NO YES YES NO YES YES
Sample Output
PASS
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 7: Skip Safe Numbers
The first value is N, followed by N tokens. Each token is either the word SKIP or an integer. Print YES if at least one integer token is positive; otherwise, print NO.
For each token, first check that it is not SKIP, and only then convert it with int(). Combine those two checks with short-circuiting so the word SKIP is never converted to an integer. Use a manual loop and a Boolean flag variable to remember whether a positive integer was found.
Constraints
1 <= N <= 1000- Every numeric token is between -10000 and 10000.
- At least one token may be
SKIP.
Sample Input
5
SKIP -3 0 SKIP 8
Sample Output
YES
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: Robot Lab Rule Engine
The input contains five flags in this order: student mentor badge banned emergency. Each flag is 1 for true or 0 for false. Print OPEN when the person is not banned and either (student or mentor) and badge is true, or when emergency and mentor is true. Otherwise, print CLOSED. Use parentheses to make the complete rule clear.
The emergency rule stands on its own: if there is an emergency and a mentor is present, the lab opens even if that mentor is banned.
Constraints
- Each flag is
0or1. - The flags always appear in the stated order.
Sample Input
1 0 1 0 0
Sample Output
OPEN
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 9: Truth-Table Row Evaluator
The input contains a rule name followed by Boolean flags a b, written as 1 or 0. The rule is one of A_AND_NOT_B, NOT_A_OR_B, A_OR_B, or A_AND_B. Evaluate that rule for the given truth-table row without using automatic expression evaluation. Print TRUE or FALSE.
Constraints
- The rule name is one of the four names listed above.
aandbare each0or1.
Sample Input
A_AND_NOT_B 1 0
Sample Output
TRUE
How this is checked: Checked by running your program on 8 test inputs (1 sample, 7 hidden) and comparing its output word by word.