CSC111 Practice Exercises

From CSclasswiki
Jump to: navigation, search

--Thiebaut 15:46, 18 February 2009 (UTC)

These exercises are for you to practice writing simple Python programs. You may write them in the console area of JES (scratchpad), or in the programming area, depending on the complexity. Loops are often easier to write in the programming area, because the indentation is easier to manage there.

Procedure

  1. Open JES
  2. Select a problem from this page, and try to solve it.
  3. If, and only if, you get stuck, highlight the area to the right of the problem to discover a solution. Look at the solution and make sure you understand it. Then return the solution to its hiding place and try again to write code, this time from memory.
  4. If you are able to figure out the problem on your own, then great! Still, take a look at the solution to see how your version differs from the one suggested.
  5. Do this over, and over. As you practice, you will find that "talking Python" will become easier!

All the problems assume that you have a picture loaded in memory:

pict = makePicture( pickAFile() )

Problem #1

Problem Solution

Remove the red component of all the pixels of a picture. Then show the picture.

def prob1( pict ):
  # a simple solution with 1 loop.  Get all the pixels and change each one.
  for pixel in getPixels( pict ):
    setRed( pixel, 0 )
  show( pict )

def prob1b( pict ):
  # a more complicated solution.  Get the pixels by specifying all the x and y
  # coordinates and change them, one at a time.  We use a double loop here.
  for x in range( 1, getWidth( pict )+1 ):
    for y in range( 1, getHeight( pict )+1 ):
      pixel = getPixel( pict, x, y )
      setRed( pixel, 0 )

  show( pict )


Problem #2

Problem Solution

Make the left side (left half) of the image all white.

# we have two different approaches here, one with a double loop,
# one with a single loop and an if statement

def makeHalfLeftWhite( pict ):
  width = getWidth( pict )
  for x in range( 1, width/2 ):
    for y in range( 1, getHeight( pict )+1 ):
      setColor( getPixel( pict, x, y ), white )

  show( pict )

def makeHalfLeftWhite2( pict ):
  width = getWidth( pict )
  for pixel in getPixels( pict ):
    x = getX( pixel )
    if x < width/2:
      setColor( pixel, white )

  show( pict )


Problem #2b

Problem Solution

Make the bottom half of the image all white.

# we have two different approaches here, one with a double loop,
# one with a single loop and an if statement

def makeLowerHalfWhite( pict ):
  height = getHeight( pict )
  for x in range( 1, getWidth( pict ) + 1 ):
    for y in range( height/2, height+1 ):
      setColor( getPixel( pict, x, y ), white )

  show( pict )

def makeLowerHalfWhite2( pict ):
  height = getHeight( pict )
  for pixel in getPixels( pict ):
    if  getY( pixel ) >= height/2:
      setColor( pixel, white )

  show( pict )

Problem #3

Problem Solution

Flip the image vertically, around a vertical axis splitting the image in half. The pixel that is originally in the top left corner of the image will find itself in the top right corner. The one in the top right corner originally will end up in the top left corner.


def prob3( pict ):
  # use nested loops to get all the pixels in the
  # left half of the picture, and flip them with
  # their counter part in the right side.
  # Pixel at 1,y will be swapped with pixel at 
  # width,y.
  # Pixel at 2,y will be swapped with pixel at 
  # width-1,y, etc.

  width = getWidth( pict )
  for xLeft in range( 1, width/2 ):
    for y in range( 1, getHeight( pict )+1 ):
      # get the left and right (mirror) pixels
      pixelLeft = getPixel( pict, xLeft, y )
      xRight = width + 1 - xLeft
      pixelRight = getPixel( pict, xRight, y )

      # get the color of the pixels
      colorLeft = getColor( pixelLeft )
      colorRight = getColor( pixelRight )

      # swap the colors 
      setColor( pixelLeft, colorRight )
      setColor( pixelRight, colorLeft )

  show( pict )


Problem #4

Problem Solution

Flip the image horizontally. The color of the top-left pixel will be swapped with that of the bottom-left one.

def flipVertically( pict ):
  # use nested loops to get all the pixels in the
  # top half of the picture, and flip them with
  # their counter part in the bottom side.
  height = getHeight( pict )
  for x in range( 1, getWidth( pict )+1 ):
    for yTop in range( 1, height/2 ):
      # get the left and right (mirror) pixels
      pixelTop = getPixel( pict, x, yTop )
      yBottom = height + 1 - yTop
      pixelBottom = getPixel( pict, x, yBottom )

      # get the color of the pixels
      colorTop = getColor( pixelTop )
      colorBottom = getColor( pixelBottom )

      # swap the colors 
      setColor( pixelTop, colorBottom )
      setColor( pixelBottom, colorTop )

  show( pict )


Problem #5

Problem Solution

Color all the pixels in a rectangular region of the image red. The top-left corner of the box is at coordinates 10,10, and the bottom-right corner is at 100, 200.

The simplest solution first:

def colorBox2( pict, x1, y1, x2, y2 ):
  # x1,y1 are the coordinates of the top left corner
  # of the box
  # x2, y2 are the coordinates of the bottom right
  # corner of the box
  for x in range( x1, x2+1 ):
    for y in range( y1, y2+1 ):
      setColor( getPixel( pict, x, y ), red )

  show( pict )

Another solution, which is less attractive, as it is longer and will touch all the pixels of the picture, not just the one we want to color.

def colorBox( pict, x1, y1, x2, y2 ):
  # x1,y1 are the coordinates of the top left corner
  # of the box
  # x2, y2 are the coordinates of the bottom right
  # corner of the box
  for x in range( 1, getWidth( pict ) + 1 ):
    for y in range( 1, getHeight( pict ) + 1 ):
      if x >= x1:
        if x <= x2:
          if y >= y1:
            if y <= y2:
              setColor( getPixel( pict, x, y ), red )

  show( pict )

You would call the function as follows:

 colorBox( pict, 10, 20, 100, 50 )

To draw a box with a top-left corner at 10, 20 and a bottom-right corner at 100,50.


Problem #6

Problem Solution

Change the color of the pixels in a rectangular region to black only if they are *very* red. By very red we mean an intensity of red larger than 128.

def changeRedInBox( pict, x1, y1, x2, y2 ):
  # x1,y1 are the coordinates of the top left corner
  # of the box
  # x2, y2 are the coordinates of the bottom right
  # corner of the box
  for x in range( x1, x2+1 ):
    for y in range( y1, y2+1 ):
      pixel = getPixel( pict, x, y )
      redIntensity = getRed( pixel )
      if redIntensity > 128:
        setColor( pixel, black )
  show( pict )