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

Midterm Exam 1 Review Fall 2003: Generalized changeColor

(Back to Fall2003 Midterm Review 1)

Answers? Comments? Comments on answers?


Something along these lines? I haven't actually tried it but I think it should work.
def changeColor(pict,amount,colorcode):
  for p in getPixels(pict):
    if colorcode=1:
      setRed(p,getRed(p)*(amount+1))
    if colorcode=2:
      setGreen(p,getGreen(p)*(amount+1))
    if colorcode=3:
      setBlue(p,getBlue(p)*(amount+1))

That's right. JES only accepts == when testing a value. Lauren Biddle

You could enter a value outside the range given (-.99 to .99) and the program would work fine.

Good observation! Mark Guzdial


Is that program good style? I dont think so, there must be a more readable way to write it. Can anyone tell me what it is? Larry Olson


Should you change (amount + 1) to some other variable which is defined earlier? Or did you mean something else?

Amount is defined earlier. See the inputs. Mark Guzdial




is it ok to do
(px, getRed*(1+#))

...or does it have to be (px, getRed(px)*(1+#))
I don't know what that first one means. getRed takes a pixel as input. It only works with the input pixel. Mark Guzdial


def changeColor(picture,amount,colorcode):
  for p in getPixels(picture):
    if (amount >= -.99)or (amount <= .99):
      if colorcode == 1
        setRed(p,getRed(p)*(amount+1)
      if colorcode == 2:
        setBlue(p,getBlue(p)*(amount+1)
      if colorcode == 3:
        setGreen(p,getGreen(p)(amount+1)

I can't think of a more readable way to write this...

The first if statement will allow any amount in since you are using an or operand. Haile Seifu

does it matter that you left out a on line 9?
or should it read
setGreen(p,getGreen(p)*(amount+1)

Do we need to worry about the constraints on this problem? I saw a post on the second problem (Generalized image filter) that said not to worry about the constraints.

i think you forgot your ending par. on lines 5, 7 and 9...

def changeColor(picture, amount, colorcode):
  for p in getPixels(picture):
    if (-.99<=amount<=.99):
      if colorcode == 1:
        setRed(p,getRed(p)*(amount+1))
      if colorcode == 2:
        setBlue(p,getBlue(p)*(amount+1))
      if colorcode == 3:
        setGreen(p,getGreen(p)(amount+1))

this worked when i ran (file, .5, 2) and didn't work when I ran (file, 2.0, 2) so hopefully its right!
This is good code, but do you really need the first if statement? It sounds to me from the problem that you don't since you are guaranteed to have a value between .99 and -.99 already. Also, couldn't you use "elif" to make your program run faster? Lauren Biddle

hm..messed up my spacing and messed up (.-99 = amount = .99)

well however you get the less than sign in there...

use &lt;


i thought i needed the first if statement, because the first example of code on the page is just what i have without that if statement and they said that it would work fine for a number outside of that range. So how would it not work for a number out of the range?

So do you need to include the if statement with the constraints for this and the generalized image filter or not.It works fine without it.
I was wrong. Yes, you need it. Lauren Biddle



Link to this Page