## Fall 2006 Midterm Review 1

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.

• 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>

## if Red == Green...

One form of color blinREMOVEDss is red-green color-blinREMOVEDss. Create a program that generates something like what it's like to see with red-green color-blinREMOVEDss. Write a function redGreenOut which takes a picture as input. For each pixel, set the red and the green values of that pixel to the average of the red and green values.

if Red == Green Questions

## 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 Color Changing

## 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%

Generalized Image Filter

## 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?

1. A only
2. D only
3. B and C
4. C and D
5. None
6. 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)```

Which Does Which? Questions

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

1. What does def mean? What does the statement def someFunction(x,y): do?
2. What happens, then, when you type in the command area:
 >>> someFunction(2, 'fred')
3. What does the statement for x in range(1, 50): do?
4. Why don't we see red, green, and blue spots at each position in our picture?
5. What is luminance and how does it differ from brightness?
6. Why is the maximum value of any color channel 255?
7. 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?
8. What is an algorithm?
9. What is hierarchical decomposition? What is it good for?
10. Why would you want to put comments in a program?
11. What's an encoding?
12. What is REMOVED's Law?
13. What is the general rule-of-thumb of when to use indention and colons?
14. 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?"
15. What is fundamentally wrong with saying "getRed gets all the red pixels"?
16. What does it mean for a function to "take-in" a value?
17. What does it mean for a function to "return" a value?
18. What is the difference between print and return?

(Yeah, I know there's only 18, but that last one is so important, that I decided to count it as three questions. MAKE SURE YOU KNOW IT.)

20 Questions...questions

## 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.

What Gets Printed?

## 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).

Compute the Pay Rates

## 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?

Tracing the Printing Questions

## Match the Meaning to the Program

Here are four possible outcomes of programs:
1. Mirror the input picture's leftmost 20 pixels to pixels 21 to 40.
2. Copy the good part of the temple pediment to the broken part.
3. Copy the broken part of the temple pediment to the working part.
4. Mirror the input picture right to left
5. 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)```

Match the Meaning to the Program Questions

## 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)`

Scaling With Input Questions

## Data types

Discuss the differences amongst the following:
• Numbers
• Strings
• Lists
• Objects
Which of the above types do each of the following fall under?
• 42
• "yo, sup?"
• "100"
• a pixel
• a color
• a list of pixels
• the returned value of pickAFile()
• the returned value of range(1, 100)
• the returned value of distance(red, blue)
• the returned value of makePicture(pickAFile())
• the returned value of setRed(pixel, 50)

Data Type Questions

## What Happen?

Suppose p is a pixel...
 >>> setRed(p, 300) >>> print getRed(p)

 >>> print 5 / 2

 >>> print 5 / 2.0

 >>> getPixels(pic)

 >>> 4 < 2

Suppose p is a pixel...
 >>> setColor(p, white) >>> setRed(p, 0) >>> setBlue(p, getRed(p))
What color is the pixel now?

What Happen? Questions

## 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) ```

Tracing Questions