Preparation

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

b. Once you are logged in, double click the icon named courses.ads.carleton.edu on your desktop. If you encounter any errors, have me or the prefect contact Mike Tie to set up your account.

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

$ cd /Volumes/courses/cs111-01-w19/StuWork/USERNAME/
$ mkdir lab-01-16
$ cd lab-01-16

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-01-w19, 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

A common problem programmers must solve is detecting when two objects are colliding with one another. (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 floats—an x-coordinate and a y-coordinate and outputs the string "inside" if the point (x,y) lies inside the unit circle and outputs the string "outside" if (x,y) is outside the unit circle.

Recall 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. The distance (x,y) is from (0,0) can be computed via the expression:

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.