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

Summmer 2006 Midterm 1 Review

Note: The idea of this review is not to tell you exactly what is going to be on the test but to demonstrate concepts that may appear on the upcoming exam. Although we try to comprise all topics, there is a possibility that other topics covered in class and not on this review will be included on 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 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 blindness is red-green color-blindness. Create a program that generates something like what it's like to see with red-green color-blindness. 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...
(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...

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





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):
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 10That's going to be awfully dark
Greater than or equal to 10 or less than or equal to 50A little dark.
Between 50 and 200Looks like a good range
Greater than or equal to 200 and less than 250Getting kinda bright.
Over or equal to 250AHHH! MY EYES!


Check Your Luminance Questions




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 Moore'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 10025%
Greater than or equal to 100 and less than 20030%
Greater than or equal to 200 and less than 30035%
Greater than or equal to 300 and less than 40045%
400 or over50%


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.

Scaling With Input Questions




Data types


Discuss the differences amongst the following:
Which of the above types do each of the following fall under?

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


Link to this Page