Excersise Set 2
Here are some simple matching excersises to help you learn how to trace code.
Part 1 Answers
Match the problem to its solution
Problems
| (A) Make the red values of all pixels equal zero. | (B) Make the green values of all pixels equal zero. | (C) Make both the red and blue values of all pixels equal 255. | (D) Make both the green and blue values of all pixels equal 255. | (E) Make the red, green and blue values of all pixels equal the average of the red, green and blue values for that pixel. |
Solutions
(1)
def changeColorSquares(file):
picture = makePicture(file)
show(picture)
for pixel in getPixels(picture):
setRed(pixel, 255)
setBlue(pixel, 255)
repaint(picture)
(2)
def changeColorSquares(file):
picture = makePicture(file)
show(picture)
for pixel in getPixels(picture):
setGreen(pixel, 255)
setBlue(pixel, 255)
repaint(picture)
(3)
def changeColorSquares(file):
picture = makePicture(file)
show(picture)
for pixel in getPixels(picture):
setRed(pixel, 0)
repaint(picture)
(4)
def changeColorSquares(file):
picture = makePicture(file)
show(picture)
for pixel in getPixels(picture):
r = getRed(pixel)
g = getGreen(pixel)
b = getBlue(pixel)
average = (r + g + b)/3
new_color = makeColor( average, average, average )
setColor(pixel, new_color)
repaint(picture)
(5)
def changeColorSquares(file):
picture = makePicture(file)
show(picture)
for pixel in getPixels(picture):
setGreen(pixel, 0)
repaint(picture)
Link to this Page