8.14. Exercises¶
-
Add a print statement to Newton’s
sqrt
function that prints outbetter
each time it is calculated. Call your modified function with 25 as an argument and record the results.
-
Write a function
print_triangular_numbers(n)
that prints out the first n triangular numbers. A call toprint_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.)
-
Write a function,
is_prime
, that takes a single integer argument and returnsTrue
when the argument is a prime number andFalse
otherwise.
-
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.
-
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.
-
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).
-
Write a function to remove all the red from an image.
For this and the following exercises, use the luther.jpg photo.
-
Write a function to convert the image to grayscale.
-
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.
-
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.
-
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.
-
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.