CPSC120A
Fundamentals of Computer Science I

Lab 15

Logic

Use the command line to create a new directory called lab15 in your labs directory. Make sure all of the files that you create for this activity are in that directory.

Boolean Expressions

In Emacs, create a text file called booleans.txt. In that file write Python Boolean expressions that are equivalent to the following expressions, but put variables on the left side of operands.

  1. 100 == a
  2. 99 >= a
  3. 1 == b and 1 < a

Also write Python boolean expressions that are equivalent to the following expressions, but do not use not.

  1. not (a != 0)
  2. not (a < 10)
  3. not (a < 0 or a > 10)

Also write Python Boolean expressions that are equivalent to the following expressions, but are simplified.

  1. a < 1 or a < 10
  2. a > 100 and a < -100
  3. not (not (a <= 0) and (a < 10))

Overlap

Download the collide.py Python file. The file contains a function that returns True if two Pygame images are colliding and False otherwise. That is, the function tests if the bounding rectangles of two images overlap. The function is ugly because the return statement is too long and complicated. Use Boolean expression negation to re-write the function. Instead of testing if the two images overlap, write four if statements that test if the images do not overlap. That is, write if statements that test:

  1. if the first image is to the left of the second image
  2. if the first image is to the right of the second image
  3. if the fist image is above the second image
  4. if the first image is below the second image

If any of the if statements are true, the function should return False. If none of the if statements are true, then the images must overlap and the function should return True.

Be sure to create test cases that cover all of the if statements and print the actual and expected values of the function.

Submission

Please show your source code and run your program for the instructor or lab assistant. Only a program that has perfect style and flawless functionality will be accepted as complete.