Contest Python: ACSL › Unit 8 — Boolean Algebra

Exercises: Unit 8 — Boolean Algebra

There are two kinds of exercises here, just as on an ACSL contest.

  • Short-answer: work it out with pencil and paper and write one exact answer.
    • A statement question is answered TRUE or FALSE.
    • A value is 1 or 0; a "how many" question is answered with just the number, such as 4.
    • A truth-table question asks for the result column: its digits from top to bottom as one string, with the rows in counting-up order (00, 01, 10, 11 for A, B), such as 1101.
    • Ordered pairs and triples are written with no spaces inside, in counting-up order, separated by a comma and a space: (0,1), (1,1). Write NONE if there are none.
    • A simplify question asks for a sum of products, with the fewest terms and then the fewest letters: letters in alphabetical order inside each term, and terms in order as Lesson 2 describes (A * B + ~A * C). Write 1 or 0 if it is always TRUE or always FALSE.
    • An option question is answered with the labels of every correct option, in order, separated by a comma and a space, such as A, C, or NONE.
  • Programming: write a complete program that reads the input with input() and prints exactly the output described. Save it as a .py file and run it from a terminal, typing the sample input or feeding it a file.

Each exercise is marked with the lowest ACSL division it belongs to. Do every exercise at or below your division. Exercises 1 to 6 are an Elementary mock test: six questions in 30 minutes, with no programming, using only what Lesson 1 teaches. Exercises 7 to 9 are extra Elementary practice. Exercises marked Challenge are extra practice for when you want more.

Exercise 1: True or False?

  • Division: Elementary

Is the following statement TRUE or FALSE?

(3 + 4 > 6 OR 2 * 5 < 9) AND NOT (8 - 3 = 4)

How this is checked: Checked by comparing your answer with the expected answer.

Answer format: `TRUE` or `FALSE`, in capital letters.

For parents and teachers: report a problem

Exercise 2: Build the Truth Table

  • Division: Elementary

Build the truth table of ~A + A * ~B. Give its result column, with the rows in the order (0,0), (0,1), (1,0), (1,1).

How this is checked: Checked by comparing your answer with the expected answer.

Answer format: a number

Also check yourself

The automatic check cannot see these, so tick each one when your work does it.

For parents and teachers: report a problem

Exercise 3: Simplify

  • Division: Elementary

Simplify ~(A + ~B) + A * B as a sum of products.

How this is checked: Checked by comparing your answer with the expected answer.

Answer format: A sum of products: `~` NOT, `*` AND, `+` OR, with one space around each `*` and `+`; the fewest terms, then the fewest letters; letters in alphabetical order inside each term, and the terms in alphabetical order too, with a plain letter before its `~` form (as in `A * B + ~A * C`); `1` or `0` if it is always TRUE or always FALSE.

For parents and teachers: report a problem

Exercise 4: Count the Pairs

  • Division: Elementary

How many ordered pairs (A,B) make ~(A * ~B) * (A + B) TRUE?

How this is checked: Checked by comparing your answer with the expected answer.

Answer format: a number

For parents and teachers: report a problem

Exercise 5: Which Are Equivalent?

  • Division: Elementary

Which of the following expressions are equivalent to ~(A + ~B)?

  • (A) ~A * B
  • (B) ~A + B
  • (C) ~(A * ~B)
  • (D) B * ~(A * B)
  • (E) ~A * ~B

How this is checked: Checked by comparing your answer with the expected answer.

Answer format: The labels of every correct option, in order, separated by a comma and a space, as in `A, C`; `NONE` if no option is correct.

For parents and teachers: report a problem

Exercise 6: Simplify Again

  • Division: Elementary

Simplify ~(~A * B) * A + ~B as a sum of products.

How this is checked: Checked by comparing your answer with the expected answer.

Answer format: A sum of products: `~` NOT, `*` AND, `+` OR, with one space around each `*` and `+`; the fewest terms, then the fewest letters; letters in alphabetical order inside each term, and the terms in alphabetical order too, with a plain letter before its `~` form (as in `A * B + ~A * C`); `1` or `0` if it is always TRUE or always FALSE.

For parents and teachers: report a problem

Exercise 7: Pairs That Fail

  • Division: Elementary

Find all ordered pairs (A,B) that make ~A * B + A * ~B FALSE.

How this is checked: Checked by comparing your answer with the expected answer.

Answer format: Ordered pairs in counting-up order, with no spaces inside a pair, and a comma and a space between pairs, as in `(0,1), (1,0), (1,1)`; `NONE` if there are none.

For parents and teachers: report a problem

Exercise 8: Spot the Tautology

  • Division: Elementary

Which of the following expressions are tautologies?

  • (A) A * ~A + B
  • (B) ~(A * B) + A
  • (C) A + ~A * B
  • (D) ~(A * ~A)

How this is checked: Checked by comparing your answer with the expected answer.

Answer format: The labels of every correct option, in order, separated by a comma and a space, as in `A, C`; `NONE` if no option is correct.

For parents and teachers: report a problem

Exercise 9: De Morgan at Work

  • Division: Elementary

Find all ordered pairs (A,B) that make ~(A * B) * (A + ~B) TRUE.

How this is checked: Checked by comparing your answer with the expected answer.

Answer format: Ordered pairs in counting-up order, with no spaces inside a pair, and a comma and a space between pairs, as in `(0,1), (1,0), (1,1)`; `NONE` if there are none.

For parents and teachers: report a problem

Exercise 10: A Real Paper

  • Division: Junior

An ACSL paper prints this expression:

AB‾+C‾+AB\overline{A\overline{B} + C} + AB

Find its value when A = 1, B = 0 and C = 0.

How this is checked: Checked by comparing your answer with the expected answer.

Answer format: a number

For parents and teachers: report a problem

Exercise 11: Three Variables, One Column

  • Division: Junior

Build the truth table of A ⊕ B * C + ~(A + C). Give its result column, with the eight rows in counting-up order, from (0,0,0) to (1,1,1).

How this is checked: Checked by comparing your answer with the expected answer.

Answer format: a number

Also check yourself

The automatic check cannot see these, so tick each one when your work does it.

For parents and teachers: report a problem

Exercise 12: Shrink It

  • Division: Junior

Simplify A * B + A * ~B * C + ~A * C as a sum of products.

How this is checked: Checked by comparing your answer with the expected answer.

Answer format: A sum of products: `~` NOT, `*` AND, `+` OR, with one space around each `*` and `+`; the fewest terms, then the fewest letters; letters in alphabetical order inside each term, and the terms in alphabetical order too, with a plain letter before its `~` form (as in `A * B + ~A * C`); `1` or `0` if it is always TRUE or always FALSE.

For parents and teachers: report a problem

Exercise 13: Two Bars

  • Division: Junior

Simplify ~(~A + B) + ~(A + ~C) as a sum of products.

How this is checked: Checked by comparing your answer with the expected answer.

Answer format: A sum of products: `~` NOT, `*` AND, `+` OR, with one space around each `*` and `+`; the fewest terms, then the fewest letters; letters in alphabetical order inside each term, and the terms in alphabetical order too, with a plain letter before its `~` form (as in `A * B + ~A * C`); `1` or `0` if it is always TRUE or always FALSE.

For parents and teachers: report a problem

Exercise 14: Every Triple

  • Division: Junior

Find all ordered triples (A,B,C) that make (A + ~B) * (~A + C) * ~(B * C) TRUE.

How this is checked: Checked by comparing your answer with the expected answer.

Answer format: Ordered triples in counting-up order, with no spaces inside a triple, and a comma and a space between triples, as in `(0,1,0), (1,1,1)`; `NONE` if there are none.

For parents and teachers: report a problem

Exercise 15: Exclusive or Not

  • Division: Junior

Simplify A ⊕ B + A * B as a sum of products.

How this is checked: Checked by comparing your answer with the expected answer.

Answer format: A sum of products: `~` NOT, `*` AND, `+` OR, with one space around each `*` and `+`; the fewest terms, then the fewest letters; letters in alphabetical order inside each term, and the terms in alphabetical order too, with a plain letter before its `~` form (as in `A * B + ~A * C`); `1` or `0` if it is always TRUE or always FALSE.

For parents and teachers: report a problem

Exercise 16: Count the Triples

  • Division: Junior

How many ordered triples (A,B,C) make A ⊙ B + ~C TRUE?

How this is checked: Checked by comparing your answer with the expected answer.

Answer format: a number

For parents and teachers: report a problem

Exercise 17: Translate, Then Solve

  • Division: Junior

An ACSL paper prints this expression:

A‾+B‾ C+AC‾\overline{\overline{A} + B}\,C + A\overline{C}

Find all ordered triples (A,B,C) that make it TRUE.

How this is checked: Checked by comparing your answer with the expected answer.

Answer format: Ordered triples in counting-up order, with no spaces inside a triple, and a comma and a space between triples, as in `(0,1,0), (1,1,1)`; `NONE` if there are none.

For parents and teachers: report a problem

Exercise 18: From Column to Solutions

  • Division: Junior

The input is one line holding the result column of a truth table: a string of 0s and 1s whose length is 4, 8 or 16. A length of 4 means two variables (A,B), 8 means three (A,B,C), and 16 means four (A,B,C,D). The rows are in counting-up order, so the first digit belongs to all 0s and the last to all 1s.

Print every ordered tuple whose digit is 1, in order, in the book's form: no spaces inside a tuple, and a comma and a space between tuples. If there are none, print NONE.

Constraints

  • The column has length 4, 8 or 16, and holds only 0 and 1.

Sample Input

0111

Sample Output

(0,1), (1,0), (1,1)

This is the column of A + B.

How this is checked: Checked by running your program on 7 test inputs (1 sample, 6 hidden) and comparing its output line by line.

For parents and teachers: report a problem

Exercise 19: The Sum-of-Products Solver

  • Division: Junior

The input is one line holding a sum of products over the variables A, B and C. Each literal is a capital letter A, B or C, or one of them with a ~ in front and no space (~B). Literals in a term are joined by * and terms are joined by +, always with one space on each side of the operator. There are no brackets. An expression does not have to use all three letters.

Print two lines:

  1. the number of ordered triples (A,B,C) that make the expression TRUE;
  2. those triples in counting-up order, in the book's form, or NONE if there are none.

Constraints

  • The expression has between 1 and 8 terms, and each term has between 1 and 3 literals.
  • A letter may appear more than once in a term.

Sample Input

A * ~B + ~A * C

Sample Output

4
(0,0,1), (0,1,1), (1,0,0), (1,0,1)

How this is checked: Checked by running your program on 8 test inputs (1 sample, 7 hidden) and comparing its output line by line.

For parents and teachers: report a problem

Exercise 20: NOTs Inside NOTs

  • Division: Intermediate

Simplify ~(~(A * ~B) + ~(B + C)) + ~A * ~~C as a sum of products.

How this is checked: Checked by comparing your answer with the expected answer.

Answer format: A sum of products: `~` NOT, `*` AND, `+` OR, with one space around each `*` and `+`; the fewest terms, then the fewest letters; letters in alphabetical order inside each term, and the terms in alphabetical order too, with a plain letter before its `~` form (as in `A * B + ~A * C`); `1` or `0` if it is always TRUE or always FALSE.

For parents and teachers: report a problem

Exercise 21: Sixteen Rows

  • Division: Intermediate

How many ordered quadruples (A,B,C,D) make (A ⊕ B) * (C + ~D) + ~(A + C) * B * D TRUE?

How this is checked: Checked by comparing your answer with the expected answer.

Answer format: a number

For parents and teachers: report a problem

Exercise 22: Unpack the XNOR

  • Division: Intermediate

Simplify ~(A ⊕ B) * C + A * ~(~B + C) as a sum of products.

How this is checked: Checked by comparing your answer with the expected answer.

Answer format: A sum of products: `~` NOT, `*` AND, `+` OR, with one space around each `*` and `+`; the fewest terms, then the fewest letters; letters in alphabetical order inside each term, and the terms in alphabetical order too, with a plain letter before its `~` form (as in `A * B + ~A * C`); `1` or `0` if it is always TRUE or always FALSE.

For parents and teachers: report a problem

Exercise 23: Triples That Fail

  • Division: Intermediate

Find all ordered triples (A,B,C) that make (A ⊙ B) ⊕ C * ~A FALSE.

How this is checked: Checked by comparing your answer with the expected answer.

Answer format: Ordered triples in counting-up order, with no spaces inside a triple, and a comma and a space between triples, as in `(0,1,0), (1,1,1)`; `NONE` if there are none.

For parents and teachers: report a problem

Exercise 24: A Flipped XOR

  • Division: Intermediate

Simplify ~(A ⊕ ~B) + ~(~A * ~C) as a sum of products.

How this is checked: Checked by comparing your answer with the expected answer.

Answer format: A sum of products: `~` NOT, `*` AND, `+` OR, with one space around each `*` and `+`; the fewest terms, then the fewest letters; letters in alphabetical order inside each term, and the terms in alphabetical order too, with a plain letter before its `~` form (as in `A * B + ~A * C`); `1` or `0` if it is always TRUE or always FALSE.

For parents and teachers: report a problem

Exercise 25: Order of Operations

  • Division: Intermediate

The input is one line holding a Boolean expression over the variables A, B, C and D, with no brackets. Each literal is one of the four letters, or a letter with a ~ in front and no space (~D). The operators are * (AND), + (OR), and XOR and XNOR, which are typed as the words XOR and XNOR because ⊕ and ⊙ are not on the keyboard. Every literal and operator is separated from the next by one space, and literals and operators alternate.

Use ACSL's order of operations: first ~, then *, then XOR and XNOR (on the same level, left to right), and last +. Print the number of ordered quadruples (A,B,C,D) that make the expression TRUE.

Constraints

  • The expression has between 1 and 40 literals.
  • An expression does not have to use all four letters; a letter it leaves out can be 0 or 1 freely.

Sample Input

A * B XOR C + ~D

Sample Output

12

This is (A * B) ⊕ C + ~D. It is TRUE on the 8 quadruples with D = 0, and on 4 of the 8 with D = 1.

How this is checked: Checked by running your program on 8 test inputs (1 sample, 7 hidden) and comparing its output line by line.

For parents and teachers: report a problem

Exercise 26: The Long Simplification

  • Challenge
  • Division: Senior

Simplify ~(A * ~(B + ~C)) * ~(~A * B * ~D) * (A + B + C + D) as a sum of products.

How this is checked: Checked by comparing your answer with the expected answer.

Answer format: A sum of products: `~` NOT, `*` AND, `+` OR, with one space around each `*` and `+`; the fewest terms, then the fewest letters; letters in alphabetical order inside each term, and the terms in alphabetical order too, with a plain letter before its `~` form (as in `A * B + ~A * C`); `1` or `0` if it is always TRUE or always FALSE.

For parents and teachers: report a problem

Exercise 27: Same or Different?

  • Challenge
  • Division: Senior

The input is two lines, each holding a Boolean expression in the form of Exercise 25: literals over A, B, C and D, the operators *, +, XOR and XNOR, single spaces, and no brackets.

If the two expressions are equivalent, print EQUIVALENT. Otherwise, print the first ordered quadruple (A,B,C,D), in counting-up order, on which they have different values, in the book's form.

Constraints

  • Each expression has between 1 and 40 literals.

Sample Input

A XOR B
A * ~B + ~A * B

Sample Output

EQUIVALENT

For an input of A + B and A XOR B, the answer would be (1,1,0,0): the first quadruple with A = 1 and B = 1.

How this is checked: Checked by running your program on 7 test inputs (1 sample, 6 hidden) and comparing its output line by line.

For parents and teachers: report a problem

Back to Unit 8 — Boolean Algebra