Change Contents of the Bubble
Welcome to CS1315. Click on the python to add comments.

Looking for the book? They have it at the Engineer's Bookstore at 748 Marietta St NW. Here is there website: http://www.engrbookstore.com/ - Monica

Hotspots: Slides and CodeTA CornerComments?AnnouncementsFAQStatic Webspace
View this PageEdit this Page (locked)Uploads to this PageHistory of this PageHomeRecent ChangesSearchHelp Guide

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