Excersise Set 2 (Part 2)
Here are some more excersises to help you with your code tracing.
Part 2 Answers
If the original picture looks like this
,
Match these pictures to the code that created them when it was run on the original picture.
(1)
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)
(2)
def changeColorSquares(file):
picture = makePicture(file)
show(picture)
for pixel in getPixels(picture):
setRed(pixel, 0)
repaint(picture)
(3)
def changeColorSquares(file):
picture = makePicture(file)
show(picture)
for pixel in getPixels(picture):
setGreen(pixel, 255)
setBlue(pixel, 255)
repaint(picture)
(4)
def changeColorSquares(file):
picture = makePicture(file)
show(picture)
for pixel in getPixels(picture):
setGreen(pixel, 0)
repaint(picture)
(5)
def changeColorSquares(file):
picture = makePicture(file)
show(picture)
for pixel in getPixels(picture):
setRed(pixel, 255)
setBlue(pixel, 255)
repaint(picture)
Link to this Page