Ch 3. Logic and Proofs

Ch 3. Programming Exercises

Exercise 1: Truth Table Generator

The objective of this exercise is to practice with propositions and logical connectives. Your task is to write a program (using pseudocode or a programming language of your choice) that takes two Boolean variables [latex]p[/latex] and [latex]q[/latex], and prints the truth table for the following expressions:

  • [latex]\neg p[/latex]
  • [latex]p \land q[/latex]
  • [latex]p \lor q[/latex]
  • [latex]p \rightarrow q[/latex] (use [latex]\neg p \lor q[/latex])
  • [latex]p \leftrightarrow q[/latex] (use [latex](p \land q) \lor (\neg p \land \neg q))[/latex]

 

Exercise 2: Access Control Logic

The objective of this exercise is to apply conditional logic to a real-world scenario. Your task is to create a function (using pseudocode or a programming language of your choice) called has_access(authenticated, is_admin, owns_data) that returns True if:

  • The user is authenticated, and
  • The user is either an admin or is accessing their own data.

Test the function with all combinations of Boolean inputs.

 

Exercise 3: Argument Validator

The objective of this exercise is to implement the rules of inference. Your task is to write a function (using pseudocode or a programming language of your choice) that takes three Boolean values representing:

  • [latex]p \rightarrow q[/latex]
  • [latex]p[/latex]
  • [latex]q[/latex]

Use Modus Ponens to determine if the argument is valid. Print whether the conclusion [latex]q[/latex] logically follows from the premises.

 

Exercise 4: Quantifier Simulation

The objective of this exercise is to simulate universal and existential quantifiers. Your task is, given a list of users and a set of users who accepted terms, write two functions (using pseudocode or a programming language of your choice):

  • all_accepted(users, accepted) → returns True if all users accepted.
  • some_accepted(users, accepted) → returns True if at least one user accepted.

Use sets and list comprehensions.

 

Exercise 5: Inductive Loop Invariant

The objective of this exercise is to model mathematical induction using a loop. Your task is to write a loop (using pseudocode or a programming language of your choice) that computes the sum of the first [latex]n[/latex] positive integers. Add a print statement inside the loop to show that the loop invariant “sum equals the total of numbers from [latex]1[/latex] to [latex]i[/latex]” holds at each step.