Test 1 Review- Fall 2007
These are the review questions for midterm #1 which is on September 21st. These questions cover more ground and are more in-depth than the actual test. These are not the same format that the test will be in. These are merely intended as a conceptual "check list" for preparing. If you understand the essence of what these questions are getting at, you should be good for the exam.
Please try these questions! Post your answers, questions, or comments on the pages for each question. Those comments, questions, etc. can also be about each others' answers. If someone posts an answer that you don't understand, ask about it! If you see a question here that you know the answer to, don't keep it to yourself – help your fellow students!
We will be reading your answers. Here are the ground rules for the interaction.
- If you don't post, neither will we. We will not be posting official solutions to these problems at all! If one of you gets it right, terrific!
- We will try to always point out if an answer is wrong. We won't always point out when the answer is right.
- We are glad to work with you toward the right answer. We can give hints, and we're glad to respond to partial guesses.
If you post code, please surround your code with <code> and </code> tags so that indentations will be preserved!
For example, to make your code appear correctly indented on the Coweb, you need to type something that looks like this:
<code>
def myFunction(a):
if (a == 3):
print "All this obsessing about Chuck Norris is starting to weird me out"
</code>
Make Sunset
Consider this program from class, that makes an input picture look like it’s sunset:
def makeSunset(picture):
for p in getPixels(picture):
value=getBlue(p)
setBlue(p, value*0.7)
value = getGreen(p)
setGreen(p, value*0.7)
In each of the below situations, we show you a set of pixel RGB values on the top that you should presume are in some input picture, and what the RGB values should be after a given transformation function (like makeSunset above). Write the given function.
(a)
Pixels coming in: (red, green, blue)
(10,25,75)
(250,19,100)
(55,200,5)
Pixels going out: (red, green, blue)
(0,25,75)
(0,19,100)
(0,200,5)
def functionA(picture):
(b)
Pixels coming in: (red, green, blue)
(10,25,75)
(250,19,100)
(55,200,5)
Pixels going out: (red, green, blue)
(10,75,25)
(250,100,19)
(55,5,200)
def functionB(picture):
(c)
Pixels coming in: (red, green, blue)
(10,25,75)
(250,19,100)
(55,200,5)
Pixels going out: (red, green, blue)
(10,10,10)
(250,250,250)
(55,55,55)
def functionC(picture):
Mangle
Here’s the input picture:
And here’s what it looked like afterward:

Which of the below functions did this?
(a)
def mangle(picture):
for x in range(1,getWidth(picture)/2):
for y in range(1,getHeight(picture)/2):
setColor(getPixel(picture,x,y),black)
for x in range(getWidth(picture)/2,getWidth(picture)):
for y in range(getHeight(picture)/2,getHeight(picture)):
setColor(getPixel(picture,x,y), white)
(b)
def mangle(picture):
for x in range(1,getWidth(picture)):
for y in range(1,getHeight(picture)):
setColor(getPixel(picture,x,y),white)
for x in range(getWidth(picture)/2,getWidth(picture)):
for y in range(getHeight(picture)/2,getHeight(picture)):
setColor(getPixel(picture,x,y),black)
(c)
def mangle(picture):
for x in range(1,getWidth(picture)/2):
for y in range(1,getHeight(picture)/2):
setColor(getPixel(picture,x,y),white)
for x in range(getWidth(picture)/2,getWidth(picture)):
for y in range(getHeight(picture)/2,getHeight(picture)):
setColor(getPixel(picture,x,y),black)
(d)
def mangle(picture):
for x in range(1,getWidth(picture)/2):
for y in range(1,getHeight(picture)/2):
setColor(getPixel(picture,x,y),white)
for x in range(1,getWidth(picture)):
for y in range(1,getHeight(picture)):
setColor(getPixel(picture,x,y),black)
Descriptions
For each of the below programs, write a one sentence description of what it does, where each sentence must be 10 words or less.
(a)
def alley(aPicture):
for num in range(1,100):
setColor(getPixel(aPicture,num,num),red)
(b)
def jespart(aPicture):
for x in range(1,getWidth(aPicture)/2):
for y in range(1,getHeight(aPicture)/2):
pixel = getPixel(aPicture,x,y)
setRed(pixel,0)
(c)
def crisscross(aPicture):
halfx = getWidth(aPicture)/2
halfy = getHeight(aPicture)/2
allx = getWidth(aPicture)
ally = getHeight(aPicture)
addLine(aPicture,halfx,1,halfx,ally)
addLine(aPicture,1,halfy,allx,halfy)
Greyscale
Create a program that generates a greyscale photo. Write a function greyscale() which takes a picture as input. For each pixel, set the red, the green, and the blue values of that pixel to the average of the red, green, and blue values.
Generalized Color Changing
Write a new version of the function increaseRed and decreaseRed (and blue and green) called changeColor that takes as input a picture AND an amount to increase or decrease a color by (a number between -.99 and .99) AND the word 'red', 'green', or 'blue'.
Here are some examples of what your function should be able to do...
- changeColor(pic, -.10, 'red') should decrease the amount of red in the picture by 10%.
- changeColor(pic, .30, 'green') should increase the amount of green in the picture by 30%
- changeColor(pic, 0, 'blue') should do nothing at all to the amount of blue in the picture (or red or green for that matter).
(By the way, when it was first posted, that last call was incorrectly given as 'changeRed' not 'changeColor'.)
Generalized Image Filter
Now go one step further. Write a function called changeRGB that takes four inputs: A picture, and an amount to change the red, green, and blue (in that order) pixels. Like before, each amount should be between -.99 and .99.
Thus, changeRGB(picture, -.75, .25, -.25) would...
- Decrease the red by 75%
- Increase the green by 25%
- Decrease the blue by 25%
Which Does Which?
Which of the functions below takes a picture and removes all the blue from every pixel of that picture that already has a blue value of more than 100?
- A only
- D only
- B and C
- C and D
- None
- All
What do the other ones do?
A | def blueOneHundred(picture):
for x in range(1,100):
for y in range(1,100):
pixel = getPixel(picture,x,y)
setBlue(pixel,100) |
B | def removeBlue(picture):
for p in getPixels(picture):
if getBlue(p) > 0:
setBlue(p,100) |
C | def noBlue(picture):
blue = makeColor(0,0,100)
for p in getPixels(picture):
color = getColor(p)
if distance(color,blue) > 100:
setBlue(p,0) |
D | def byeByeBlue(picture):
for p in getPixels(picture):
if getBlue(p) > 100:
setBlue(p,0) |
Check Your Luminance
Write a program called checkLuminance that will take in 3 numbers representing red, green, and blue values. Compute the luminance using the weighted average. Finally print out a warning to the user based on the computed luminance:
Weighted color values (as opposed to the typical 33.33% each):
- Red → 30%
- Green → 59%
- Blue → 11%
Hint: you'll probably have something in your code like luminance = r * .3 + g * .59 + b * .11
| If the luminance is... | Print out the statment... |
| Less then 10 | That's going to be awfully dark |
| Greater than or equal to 10 or less than or equal to 50 | A little dark. |
| Between 50 and 200 | Looks like a good range |
| Greater than or equal to 200 and less than 250 | Getting kinda bright. |
| Over or equal to 250 | AHHH! MY EYES! |
20 Questions
- What does def mean? What does the statement def someFunction(x,y): do?
- What happens, then, when you type in the command area:
| >>> someFunction(2, 'fred') |
- What does the statement for x in range(1, 50): do?
- Why don't we see red, green, and blue spots at each position in our picture?
- What is luminance and how does it differ from brightness?
- Why is the maximum value of any color channel 255?
- The color encoding we're using is "RGB". What does that mean, in terms of the amount of memory required to represent color? Is there a limit to the number of colors that we can represent? Are there enough colors representable in RGB?
- What is an algorithm?
- What is hierarchical decomposition? What is it good for?
- Why would you want to put comments in a program?
- What's an encoding?
- What is the general rule-of-thumb of when to use indentation and colons?
- How does a for loop technically work? What do you need to be able to set up a for loop? What do we mean by "a for loop works iteratively?"
- What is fundamentally wrong with saying "getRed gets all the red pixels"?
- What does it mean for a function to "take-in" a value?
- What does it mean for a function to "return" a value?
- What is the difference between show() and return?
(Yeah, I know there's only 17, but that last one is so important, that I decided to count it as four questions. MAKE SURE YOU KNOW IT.)
What Gets Printed?
Consider the below program:
def testme(p,q,r):
if q > 50:
print r
value = 10
for i in range(1,p):
print "Everybody"
value = value - 1
print value
print r |
What happens when we do this...
| >>> testme(5, 51, "Homestar Rocks!") |
Trying it out in JES is Bad! Try to figure it out by just reading through the code. On the exam, you will NOT have access to JES.
Compute Pay Rates
Write a function called pay that takes in as input a number of hours worked and the hourly rate to be paid. Compute the gross pay as the hours * the rate. But then compute a tax.
| If the pay is... | Charge a tax of... |
| Less then 100 | 25% |
| Greater than or equal to 100 and less than 200 | 30% |
| Greater than or equal to 200 and less than 300 | 35% |
| Greater than or equal to 300 and less than 400 | 45% |
| 400 or over | 50% |
Compute the taxed amount as tax rate * gross pay.
Print the gross pay and the net pay (gross - tax).
Tracing the Printing
def newFunction(a, b, c):
print a
list1 = range(1,5)
value = 0
for x in list1:
print b
value = value +1
print c
print value |
If you call the function above by typing...| >>> newFunction("I", "you", "walrus") |
...what will the computer print?
Match the Meaning to the Program
Here are four possible outcomes of programs:
- Mirror the input picture's leftmost 20 pixels to pixels 21 to 40.
- Copy the good part of the temple pediment to the broken part.
- Copy the broken part of the temple pediment to the working part.
- Mirror the input picture right to left
- Mirror the input picture left to right
Which of the below programs does which of the above outcomes?
Note: temple.jpg refers to the temple picture in MediaSources.
A | def mirrorTemple():
source = makePicture(getMediaPath("temple.jpg"))
mirrorpoint = getWidth(source) / 2
lengthToCopy = mirrorpoint - 14
for x in range(1, lengthToCopy):
for y in range(28, 98):
p = getPixel(source, mirrorpoint - x, y)
p2 = getPixel(source, mirrorpoint + x, y)
setColor(p2, getColor(p))
show(source)
return source |
B | def mirrorTemple():
source = makePicture(getMediaPath("temple.jpg"))
mirrorpoint = getWidth(source) / 2
lengthToCopy = mirrorpoint - 14
for x in range(1, lengthToCopy):
for y in range(28, 98):
p = getPixel(source, mirrorpoint - x, y)
p2 = getPixel(source, mirrorpoint + x, y)
setColor(p, getColor(p2))
show(source)
return source |
C | def mirrorVertical(source):
mirrorpoint = int(getWidth(source) / 2)
for y in range(1, getHeight(source) + 1):
for xOffset in range(1, mirrorpoint):
pright = getPixel(source, xOffset + mirrorpoint, y)
pleft = getPixel(source, mirrorpoint - xOffset, y)
c = getColor(pleft)
setColor(pright, c) |
D |
def mirrorVertical(source):
mirrorpoint = int(getWidth(source) / 2)
for y in range(1, getHeight(source) + 1):
for xOffset in range(1,mirrorpoint):
pright = getPixel(source, xOffset + mirrorpoint, y)
pleft = getPixel(source, mirrorpoint - xOffset, y)
c = getColor(pright)
setColor(pleft, c) |
E | def mirrorVertical(source):
mirrorpoint = 20
for y in range(1, getHeight(source) + 1):
for xOffset in range(1, mirrorpoint):
pright = getPixel(source, xOffset + mirrorpoint, y)
pleft = getPixel(source, mirrorpoint - xOffset, y)
c = getColor(pleft)
setColor(pright,c) |
Scaling With Input
Note: food.jpg refers to this and 640x480.jpg is a blank, white image that's 640 pixels wide and 480 pixels tall. Assume that the media path has already been set to the folder containing these two images.
The face on food.jpg falls in the region denoted by 50 < x < 110 and 70 < y < 123.
So with that said, consider the program below...
def scaleAndCrop(ratio):
pic = makePicture(getMediaPath("food.jpg"))
canvas = makePicture(getMediaPath("640x480.jpg"))
sourceX = 50
for targetX in range(300, 300 + int((110 - 50) * ratio)):
sourceY = 70
for targetY in range(200, 200 + int((123 - 70) * ratio)):
print "Copying from", sourceX, sourceY, "to", targetX, targetY
color = getColor(getPixel(pic, int(sourceX), int(sourceY)))
setColor(getPixel(canvas, targetX, targetY), color)
sourceY = sourceY + (1.0 / ratio)
sourceX = sourceX + (1.0 / ratio)
show(canvas)
return canvas
|
Describe the picture that will appear when you execute the following. Also describe the first four lines that will be printed out.
scaleAndCrop(.5)
scaleAndCrop(2.0)
scaleAndCrop(1.5)
What Happens?
Suppose p is a pixel...
>>> setRed(p, 300)
>>> print getRed(p)
|
Suppose p is a pixel...
>>> setColor(p, white)
>>> setRed(p, 0)
>>> setBlue(p, getRed(p))
|
What color is the pixel now?
Tracing Fun!
What do the following functions do?
def tomato(pictureA, pictureB):
for x in range(1, getWidth(pictureB) + 1):
for y in range(1, getHeight(pictureB) + 1):
targetPixel = getPixel(pictureA, x, y)
sourcePixel = getPixel(pictureB, x, y)
color = getColor(sourcePixel)
setColor(targetPixel, color)
|
def cucumber(picture):
for p in getPixels(picture):
color = getColor(p)
if distance(color, white) < distance(color, black):
setColor(p, white)
else:
setColor(p, black)
|
def hashbrowns(number):
foo = 0
for i in range(number + 1)
foo = foo + i
return foo
|
def nothingPolitical(picture):
for xoffset in range(1, getWidth(picture) / 2):
for y in range(1, getHeight(picture) + 1):
liberal = getPixel(picture, xoffset, y)
conservative = getPixel(picture, getWidth(picture) - xoffset, y)
colorA = getColor(liberal)
colorB = getColor(conservative)
setColor(liberal, colorB)
setColor(conservative, colorA)
|
Links to this Page