8.13. Exercises

  1. Add a print statement to Newton’s sqrt function that prints out better each time it is calculated. Call your modified function with 25 as an argument and record the results.
    Show Comments
  1. Write a function print_triangular_numbers(n) that prints out the first n triangular numbers. A call to print_triangular_numbers(5) would produce the following output:

    1       1
    2       3
    3       6
    4       10
    5       15
    

    (hint: use a web search to find out what a triangular number is.)

  1. Write a function, is_prime, that takes a single integer argument and returns True when the argument is a prime number and False otherwise.
    Show Comments
  1. Modify the walking turtle program so that rather than a 90 degree left or right turn the angle of the turn is determined randomly at each step.
  1. Modify the turtle walk program so that you have two turtles each with a random starting location. Keep the turtles moving until one of them leaves the screen.
    Show Comments
  1. Modify the previous turtle walk program so that the turtle turns around when it hits the wall or when one turtle collides with another turtle (when the positions of the two turtles are closer than some small number).
  1. Write a function to remove all the red from an image.

    For this and the following exercises, use the luther.jpg photo.

    Show Comments
  1. Write a function to convert the image to grayscale.
  1. Write a function to convert an image to black and white.
    Show Comments
  1. Sepia Tone images are those brownish colored images that may remind you of times past. The formula for creating a sepia tone is as follows:

    newR = (R × 0.393 + G × 0.769 + B × 0.189)
    newG = (R × 0.349 + G × 0.686 + B × 0.168)
    newB = (R × 0.272 + G × 0.534 + B × 0.131)
    

    Write a function to convert an image to sepia tone. Hint: Remember that rgb values must be integers between 0 and 255.

  1. Write a function to uniformly enlarge an image by a factor of 2 (double the size).
    Show Comments
  1. After you have scaled an image too much it looks blocky. One way of reducing the blockiness of the image is to replace each pixel with the average values of the pixels around it. This has the effect of smoothing out the changes in color. Write a function that takes an image as a parameter and smooths the image. Your function should return a new image that is the same as the old but smoothed.
  1. Write a general pixel mapper function that will take an image and a pixel mapping function as parameters. The pixel mapping function should perform a manipulation on a single pixel and return a new pixel.
    Show Comments
  1. When you scan in images using a scanner they may have lots of noise due to dust particles on the image itself or the scanner itself, or the images may even be damaged. One way of eliminating this noise is to replace each pixel by the median value of the pixels surrounding it.
  1. Research the Sobel edge detection algorithm and implement it.
    Show Comments
Next Section - 9. Strings