CS 140 - Exam 2 Study Guide

Make sure to review the homework assignments, quizzes, and the examples we've written in class.

  1. Assume we have $1000 that earns 1.5% interest per year. Write a Python program that lists the value of the investment every year for the next ten years. For example the output might look like:
     Year   Value
     ----   -----
     0      1000.0
     1      1015.0
     2      1030.22
     3      1045.68
     4      1061.36
     5      1077.28
     6      1093.44
     7      1109.84
     8      1126.49
     9      1143.39
    
    Remember the round function? Hint: This program is only 5-6 lines long.
  2. Assume we have $1000 that earns 1.5% interest per year. Write a Python program that determines how many years it would take for our investment to grow to $10,000.
  3. Assume we have $1000 that earns 1.5% interest per year. Write a Python program that determines how many years it would take for our investment to grow to $10,000 assuming we also added $100 per year to the principal.
  4. Assume we have a Pygame surface named surf. Write a Python statement that will print the value of the red component at coordinate (50,99).
  5. Assume we have a Pygame surface named surf. Write a Python statement that will set the value of the red component at coordinate (50,99) to zero.
  6. Write a Python program that reads an integer from the keyboard and prints "yes" if the integer is divisible by 7 or 15. Otherwise it should print "no".
  7. What does the following Python program do?
    x = input("enter int: ")
    d = 2
    p = True
    
    while p and d < x:
        if x % d == 0:
            p = False
            
        d = d + 1
        
    if p:
        print "yes"
    else:
        print "no"
      
  8. How would you modify the 2-Dimensional random walk (a.k.a. The Drunkard's Walk) so that the particle stays within the window as if the edges of the window were a wall (instead of wrapping around like a torus).
  9. Write a function named makeBlurry that takes an image (surface) as its only parameter and animates displaying the image with random colors at random locations within the picture. One way to do this is to go through every pixel and toss a coin on whether you should change it to a random color or not. Another technique is to randomly generate pixel coordinates and change them.
  10. Write a short program that calculates the minimum number of pixels in an image named image.png.
  11. Write a Python program that generates the 256x256 image below. The upper right color is all green and the lower left image is all blue. Upper left is all black and lower right is teal, which is the color (0,255,255).
    Color Gradiant of mixing green and blue
  12. (Related to Sierpinski's Triangle). Assume we have a line that goes from point (x1,y1) to point (x2,y2) in a Pygame window. Write a short Python code segment that draws a small red circle with radius 5 at the mid-point on the line.
  13. Review the steganography programs used for encrypting and decrypting messages.
  14. Write a function brighten that makes an image brighter by a percentage passed to it. For example brighten(pic,.10); will brighten pic by ten percent. What you have to watch out for is having one of the color values become greater than 255. If a number becomes greater than 255 you should have it max out at 255.
  15. What does the following program print?
    for y in range(3):
       for x in range(y,4):
           print y+x,
       print