Preparation

a. Make sure you are booted into the Mac OSX side of the computer.

b. Mount the COURSES network drive by double clicking the alias icon you created previously. If you were unable to set up the alias, you can follow the Course Directory Setup instructions to mount the drive and create the alias.

c. Open up a Terminal, navigate to your StuWork directory, create a folder named lab-09-19, and change your directory to the newly created folder. (Remember that you should replace USERNAME with your actual username!)

$ cd /Volumes/COURSES/cs111-03-f18/StuWork/USERNAME/
$ mkdir lab-09-19
$ cd lab-09-19

d. Open Brackets and make sure you have your StuWork/USERNAME directory opened. (You will see all of its subfolders in the left panel if so.) If the folder is not opened, you can open it by selecting File -> Open Folder and navigating to COURSES, then cs111-03-f18, then StuWork, then USERNAME.

Exercise 1

In the last lab, you were asked to examine the following expression where val, lower, and upper are previously defined integers.

bounded_val = min(max(val,lower), upper)

a. Discuss with your partner and make sure you both understand what bounded_val will be assigned. (You might consider thinking about what happens when lower = 0 and upper = 100.)

b. Examine the code below and discuss with your partner whether you think the value of bounded_val will be the same after the code is evaluated or different. Why?

if val < lower:
    bounded_val = lower
elif val > upper:
    bounded_val = upper
else:
    bounded_val = val

c. Which of the two ways of computing bounded_val do you prefer? Discuss with your partner.

Exercise 2

In this exercise, we will practice reading code and determining how the if statements will be evaluated. Look through the following piece of code and then answer the following questions.

if val == 5:
    print("red")
elif val < 5:
    if val >= 0:
        print("green")
    else:
        print("blue")
else:
    if val > 10:
        print("purple")

a. Under what values of val will red be printed? Verify your answer by executing your code on that value of val.

b. Under what values of val will green be printed? Verify your answer.

c. Under what values of val will blue be printed? Verify your answer.

d. Under what values of val will purple be printed? Verify your answer.

e. Under what values of val will nothing be printed? Verify your answer.

f. Examine the following piece of code and determine if it is equivalent to the code above.

if val < 0:
    print("blue")
elif val < 5:
    print("green")
elif val == 5:
    print("red")
elif val > 10:
    print("purple")

g. Do you think the first or the second conditional is easier to read? Why? Discuss with your partner.

Exercise 3

Write a program that asks the user to type in a number and outputs negative if it is negative, positive if it is positive, and zero if it is zero.

Exercise 4

It is useful to be able to check when certain shapes are colliding with another shape or point. (For example, a character bumping into a wall in a video game.) In this exercise, we will do a simple check if a point falls within a circle.

Write a program that asks the user to input two numbers—an x-coordinate and a y-coordinate and outputs “inside” if the point (x,y) lies inside the unit circle and outputs “outside” if (x,y) is outside the unit circle.

Remember that the unit circle is a circle with radius 1 that is centered at the point (0,0). You can check if (x,y) is inside the unit circle by computing the distance of (x,y) to the origin (0,0) and then checking if it is less than or equal to 1.

Exercise 5

a. Examine the following two pieces of code to make sure you understand that they are equivalent. Discuss with your partner why this is the case.

if condition1:
    if condition2:
        do_something()
if condition1 and condition2:
    do_something()

b. Examine the following two pieces of code to make sure you understand that these are not equivalent. Discuss with your partner why this is the case.

if condition1:
    do_something()

if condition2:
    do_something()
if condition1 or condition2:
    do_something()

Exercise 6

Here is a common interview question in CS.

a. Write a program called fizzbuzz.py that asks the user to input a number num and outputs the following.

  • Outputs “fizz” if num is divisible by 3,
  • Outputs “buzz” if num is divisible by 5,
  • Outputs “fizzbuzz” if num is divisible by both 3 and 5, and
  • Outputs num otherwise.

b. Modify your program so that it also outputs “jazz” if num is divisible by 7. Note that it should also output “fizzjazz” if it is divisible by 3 and 7, and so on for all other combinations above.

c. How would you modify your program to support additional number-string combinations? For example, printing “pezz” if num is divisible by 9 and printing “tyzz” if it is divisible by 11.