Welcome to CS1315. Click on the python to add comments.
This page removed for FERPA compliance

## Fall2004 Midterm 1 Review

Below are questions like those we plan to ask on Midterm #1 (Sept. 22). The exam will consist of 4 or 5 questions like these.

Please do try these questions! Post your answers, questions, comments, concerns, and criticisms 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.
1. 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!
2. We will try to always point out if an answer is wrong. We won't always point out when the answer is right.
3. 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 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 and, for each pixel px, sets the red and the green values of that pixel to the average of the red and green values. (Sort-of a 2/3 luminance.)

Questions, comments, and answers for Midterm Exam 1 Review Fall 2004: If red were green were red

### Generalized changeColor

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 AND a number 1 (for red), 2 (for green), or 3 (for blue). The amount will be a number between -.99 and .99.

• changeColor(pict,-.10,1) should decrease the amount of red in the picture by 10%.
• changeColor(pict,.30,2) should increase the amount of green in the picture by 30%
• changeRed(pict,0,3) should do nothing at all the amount of blue (or red or green) in the picture.

```def increaseRed(pict):
for p in getPixels(pict):
setRed(p,getRed(p)*1.2)

def decreaseRed(pict):
for p in getPixels(pict):
setRed(p,getRed(p)*.80)
```

Questions, comments, and answers for Midterm Exam 1 Review Fall 2004: Generalized changeColor

### Generalized image filter

Now go one step further. Write a function changeRGB that takes four inputs: A picture, and an amount to change the red, green, and blue (in that order) pixels. Each amount should be between -.99 and +.99.

Thus,
changeRGB(picture,-.75,.25,-.25)
would
• Set the green component to 125% of its current value
• Decrease the blue by 25%
• Decrease the red by 75%

Questions, comments, and answers for Midterm Exam 1 Review Fall 2004: Generalized image filter

### What does it mean?

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 for mean? What does the statement for x in range(1,getWidth(picture)): do?
4. What does print mean? What does the statement print a do?

Questions, comments, and answers for Midterm Exam 1 Review Fall 2004: What does it mean?

### Which does which?

Which of the recipes below takes a picture and removes all the blue from every pixel of that picture that already has a blue value of REMOVED 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)
```

Questions, comments, and answers for Midterm Exam 1 Review Fall 2004: Which does which

### Check your luminance

Write a program checkLuminance that will input red, green, and blue values, and compute the luminance using the weighted average (as below). But then, print out a warning to the user based on the computed luminance:

 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 "That's going to be nearly white"

```def greyScaleNew(picture):
for px in getPixels(picture):
newRed = getRed(px) * 0.299
newGreen = getGreen(px) * 0.587
newBlue = getBlue(px) * 0.114
luminance = newRed+newGreen+newBlue
setColor(px,makeColor(luminance,luminance,luminance))
```

Questions, comments, and answers for Student2256

### Picture concepts

1. Why don't we see red, green, and blue spots at each position in our picture?

2. What is luminance?

3. Why is the maximum value of any color channel 255?
4. 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?

Questions, comments, and answers for Midterm Exam 1 Review Fall 2004: Picture concepts

### CS concepts

1. What is an algorithm?
2. What is hierarchical decomposition? What is it good for?
3. Why would you want to put comments in a program?
4. What's an encoding?
5. What is REMOVED's Law?

Questions, comments, and answers for Midterm Exam 1 Review Fall 2004: CS concepts

### 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 "Hello"
value = value - 1
print value
print r
```

If we execute testme(5,51,"Hello back to you!"), what will print?

Hint: DON'T TRY IT IN JES! Try to figure it out by walking through the code. On the exam, you will NOT have access to JES.

Questions, comments, and answers for Midterm Exam 1 Review Fall 2004: 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... < 100 .25 >= 200 and <300 .35 >=300 and <400 .45 >=400 .50

Compute the taxed amount as tax rate * gross pay.
Print the gross pay and the net pay (gross - tax).

Questions, comments, and answers for Midterm Exam 1 Review Fall 2004: Compute the pay rate

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

Questions, comments, and answers for Midterm Exam 1 Review Fall 2004: Tracing the printing

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

A.
```def mirrorTemple():
source = makePicture(getMediaPath("temple.jpg"))
mirrorpoint = 277
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 = 277
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) Tracing the printing
for y in range(1,getHeight(source)):
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)):
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)):
for xOffset in range(1,mirrorpoint):
pright = getPixel(source, xOffset+mirrorpoint,y)
pleft = getPixel(source, mirrorpoint-xOffset,y)
c = getColor(pleft)
setColor(pright,c)
```

Questions, comments, and answers for Midterm Exam 1 Review Fall 2004: Match the meaning to the program

### Scaling with input

Consider the below program:
```def copyBarbsFaceScaled(factor):
# Set up the source and target pictures
barbf=getMediaPath("barbara.jpg")
barb = makePicture(barbf)
canvasf = getMediaPath("7inX95in.jpg")
canvas = makePicture(canvasf)
# Now, do the actual copying
sourceX = 45
for targetX in range(100,100+int((200-45)*factor)):
sourceY = 25
for targetY in range(100,100+int((200-25)*factor)):
print "Copying from ",sourceX,sourceY," to ",targetX,targetY
color = getColor(getPixel(barb,int(sourceX),int(sourceY)))
setColor(getPixel(canvas,targetX,targetY), color)
sourceY = sourceY + (1.0/factor)
sourceX = sourceX + (1.0/factor)
show(barb)
show(canvas)
return canvas
```

A. What pictures will appear when you execute copyBarbsFaceScaled(0.5)? What will be the first four lines printed?

B. What pictures will appear when you execute copyBarbsFaceScaled(2.0)? What will be the first four lines printed?

C. What pictures will appear when you execute copyBarbsFaceScaled(1.5)? What will be the first four lines printed?

Questions, comments, and answers for Midterm Exam 1 Review Fall 2004: Scaling with input

### Compute the grade

Students in a class have taken three exams. Exam1 and Exam2 are midterms that together are worth 40% of the final grade. Exam3 is a final exam that is worth 60% of the final grade.

Write a function finalGrade that takes in the three exam scores as inputs (e.g., def finalGrade(exam1, exam2, exam3): )and computes the final score. You should print the final score, and print the grade as follows:

```If the final grade is:		the computer should print:
>=90				Congratulations, you got an A!
>=80 and <90			You got a B.
>=70 and <80 			You got a C.
>=60 and <70 			You got a D.
<60				Better luck next time. ```

Questions, comments, and answers for Midterm Exam 1 Review Fall 2004: Compute the grade