CSC108H/A08H Week 3 Lab To earn your lab marks, you must actively participate in the lab. You do not need to finish in the time alloted, you just need to arrive on time and work hard. At the end of the lab, please return this handout to your TA. We will post the handout on the course website at the end of the week. 1 Objectives 1. Explore module math using help and dir 2. Use type bool 3. Write if statements 4. More image manipulation 5. Practice submitting an assignment 2 Driver and navigator driver: The person typing at the keyboard. navigator: The person watching for mistakes, and thinking ahead. The rest of these instructions call you s1 and s2. Pick which one is which. s1 should log in, start up Wing, and be the first driver. 3 math Module Python has a set of built-in functions, and many other functions are saved in separate modules. In this lab, we will explore the math module. You are given some tasks to complete and you must use the functions from the math module to do so. Hint: use dir(math) to find out which functions math contains and use help(math.function) (where function is replaced by the name of a function from math) to find out more about a particular function. Another hint: try help(math). To begin, import math. In the shell, write code to do the following: • Take the floor and ceiling of 56.4. • Calculate the square root of the logarithm of 244. • Calculate the base 2 logarithm of 256. • Calculate the absolute value of -34.84. • Calculate 2π. 4 Type bool: Poisonous Potions There are two Boolean values, True and False, and there are three Boolean operators, and, or, and not: a and b a or b not a Returns True if both a and b are True, and False otherwise. Returns True if at least one of a and b are True, and False otherwise. Returns the negated value of a; True if a is False, and False if a is True. 1 Download puzzle.py from the Labs page of the course website: (http://www.cdf.toronto.edu/~csc108h/fall/labs.shtml). This file contains the start of a logic puzzle (written in Python): which (if any) of four potions is poisonous? It relies on five hints, only one of which has been written. Your task is to write more hints (in Python) to help determine whether each of the four potions is poisonous. The file contains two functions, hint1 and solution, which use Boolean variables p1, p2, p3, and p4 to represent four potions: True indicates that the potion is poisonous, and False indicates that the potion is not poisonous. For example, if p1 is False, then the potion p1 is not poisonous. Function hint1 contains the first hint. Once you have written the rest of the hint functions, you can call solution with a guess about which potions are poisonous; it will return True if your guess is correct, and returns False otherwise. Add the following functions to puzzle.py, switching driver and navigator after each one. hint2(p1) hint3(p1, p3) hint4(p3, p4) hint5(p1, p3, p4) Return Return Return Return True True True True if if if if potion p1 is poisonous, and return False otherwise. at least one of potions p1 and p3 is poisonous, and return False otherwise. at least one of potions p3 and p4 is not poisonous, and return False otherwise. exactly one of the given potions is poisonous, and return False otherwise. Try to solve the puzzle by reading the hints and verify your answer by calling puzzle with your proposed solution (e.g., solution(False, True, True, False)). 5 Practice submitting an assignment The assignments in this course will be submitted electronically using the UTM submission system. Visit the UTM page of the course website to find a link to the assignment submission site: http://www.cdf.utoronto.ca/~csc108h/fall/utm/index.shtml Submit puzzle.py for assignment LabPractice (this is the only time that you will need to submit your lab work electronically and you will not be marked on it; this is just for practice submitting). Switch roles: s1 drives and s2 navigates: s1 should also practice submitting puzzle.py. (Just download the file from the website, save it, and submit it. You don’t need to complete it again — it won’t be marked.) 6 if statements and image manipulation In last week’s lab, you wrote several functions to manipulate images. Those functions had something in common: they performed the same action on each pixel in the image. This week you will manipulate images, but you will not necessarily perform the same action (or any action) on each pixel. In this section of the lab, you will use if statements. Download w3.py from the course website, and add the following functions to it: reduce blue(pic) grey posterize(pic) swap black white(pic) Set the blue color component of each pixel in picture pic to zero, if the current blue component of the pixel is greater than 100. Hint: read function reduce red. Set the color of each pixel in picture pic to black if the luminance is less than 128, and to white otherwise. Note: The luminance of a pixel is the average of its red, green, and blue color components. For each pixel in pic, if it is currently white, set it to black, and if it is currently black, set it to white. 2