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

Test 1 Review- Spring 2008


Topics


Fundamentals of programming in JES and Python



Programming constructs



Pictures


– Note, you will NOT have to write any code from scratch in the test. You will either be given code to correct/trace, template (fill-in-the-blanks) code to complete, or will be given related code examples that you can use as hints.

Example Questions


We will be reading your answers. Here are the ground rules for the interaction.

If you post code, you must 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):
   for line in range(1000):
      print "I must remember that there are three bytes per pixel, not eight!"

</code>
We will not comment on code that is missing these code tags. We're not being personal or arbitrary about this; it's just something we all have to remember to do on the CoWeb to communicate effectively.

Conceptual Questions



15 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. 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? (I.e. how many bytes are required to store a single pixel?)
  5. Is there a limit to the number of colors that we can represent in RGB? About how many colors can there be (or, alternately, how would you calculate the number?)
  6. Are there enough colors representable in RGB? (Can our output devices render more colors than this? Can our eyes/brain distinguish more colors?)
  7. What is an algorithm? Give an example of an algorithm that is not a computer program.
  8. If comments are ignored by the computer, why would you want to put comments in a Python program?
  9. What is an encoding? Give two examples of encodings of color in digitized pictures. (We are only using one of them in this course, but you should know that there are others.)
  10. Where do colons appear in Python code?
  11. If you have four lines of code, one starting with the keyword def, and three lines of code that define the function, how should the following three lines be indented?
  12. In "for p in getPixels(pic):" what does p refer to? If there are 1,000 pixels in the picture pic, how many times would the block following that line normally be executed?
  13. What is fundamentally wrong with saying "getRed gets all the red pixels"?
  14. What does it mean for a function to "take in" a value?
  15. If pic is a picture variable, what is the difference between show(pic) and print pic?


Picture Questions



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)

Why do we reduce the blue and green values instead of increasing the red values?

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. [Note: this was from a longer test in a previous semester. We will not ask you to write code from scratch like this. However, we will test your knowledge of programming and color encoding by asking you to modify existing code or fill in the blanks in code templates, so this is a good practice exercise for you to do.]

(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):






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.

Here is a program for making a picture negative. You may find it useful guide when writing your grayscale function.
def negative(picture):
  for pixel in  getPixels(picture):
    setRed(pixel, 255-getRed(pixel))
    setGreen(pixel, 255-getGreen(pixel))
    setBlue(pixel, 255-getBlue(pixel))


Generalized Color Changing


Here are a couple of functions, increaseRed and decreaseRed that increase and decrease, respectively, the red value of all pixels in a picture by 20%.
def decreaseRed(pic):
  allThePixels = getPixels(pic)
  for eachPixel in allThePixels:
    value = getRed(eachPixel)
    setRed(eachPixel, value*0.8)

def increaseRed(pic):
  allThePixels = getPixels(pic)
  for eachPixel in allThePixels:
    value = getRed(eachPixel)
    setRed(eachPixel, value*1.2)


Write a new function called changeColor that does both and more. It takes as its 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...


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...
Hint: The blast function in the codes/slides page is similar. Use it for inspiration if you are stuck.

Questions?

Link to this Page