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 (Part 3)

Here are some simple matching excersises to help you learn how to trace code.

Part 3 Answers
Match the problem to its solution

Problems
(A) If the original green value of the pixel is greater than or equal to 100, set the new green value of the pixel to 255. (B) If the original green value of the pixel is less than 175, swap the values of the red and green for that pixel (e.g., make the new green equal to the old red, and the new red equal to the old green). (C) If the original red value of the pixel is greater than 70 and less than or equal to 175, set the new red value of the pixel to the original blue value. (D) If the original blue value is greater than 175, set the new blue value to 175. (E) If the original blue value is less than or equal to 175, set the new red value to the absolute value (hint: function abs) of the original green value minus the original red value.

Solutions
(1)
def changeColorSquares(file):
  picture = makePicture(file)
  show(picture)

  for pixel in getPixels(picture):
    original_red = getRed(pixel) 
    original_green = getGreen(pixel)
    original_blue = getBlue(pixel)

    if (original_blue  <= 175):
      setRed(pixel, abs(original_green - original_red))

  repaint(picture)

(2)
def changeColorSquares(file):
  picture = makePicture(file)
  show(picture)

  for pixel in getPixels(picture):
    original_red = getRed(pixel) 
    original_green = getGreen(pixel)
    original_blue = getBlue(pixel)

    if (original_green  < 175):
      setGreen(pixel, original_red)
      setRed(pixel, original_green)

  repaint(picture)

(3)
def changeColorSquares(file):
  picture = makePicture(file)
  show(picture)

  for pixel in getPixels(picture):
    original_red = getRed(pixel) 
    original_green = getGreen(pixel)
    original_blue = getBlue(pixel)

    if (original_blue  > 175):
      setBlue(pixel, 175)

  repaint(picture)

(4)
def changeColorSquares(file):
  picture = makePicture(file)
  show(picture)

  for pixel in getPixels(picture):
    original_red = getRed(pixel) 
    original_green = getGreen(pixel)
    original_blue = getBlue(pixel)

    if ( (original_red  > 70) and (original_red <=175) ):
      setRed(pixel, original_blue*0.5)

  repaint(picture)

(5)
def changeColorSquares(file):
  picture = makePicture(file)
  show(picture)

  for pixel in getPixels(picture):
    original_red = getRed(pixel) 
    original_green = getGreen(pixel)
    original_blue = geBlue(pixel)

    if (original_green  >= 100):
      setGreen(pixel, 255)

  repaint(picture)


Link to this Page