< Back

Lab 18: Compound Images


Practice Problem 1

Write a function called print_words(text), which prints the each word from the text on its own line. Assume each word is separated by exactly one space.

Example

>>> print_words("INQ241A")
INQ241A
>>> count_words("INQ 241A")
INQ 241A
>>> count_words("How much wood could a woodchuck chuck if a woodchuck could chuck wood")
How
much
wood
could
a
woodchuck
chuck
if
a
woodchuck
could
chuck
wood

Hint

You need to use the accumulator pattern here. The accumulator pattern always begins by setting the accumulator variable before your for loop. In this case, you are going to accumulate strings, which means you need your accumulator to start at \(""\).

Inside of your for loop, you need to accumulate the characters that make up words. To accomplish this, you should use the for character in text loop.

Inside of your if statement, you need to determine if you have seen a space or not. if the current character is not a space, then add it to your accumulator. If it is a space, then print your accumulator and set your accumulator back to \(""\).


Practice Problem 2

A power tower is a number \(x\) raised to the \(x\) power \(x\) times. Mathematically, it looks like:

\[ x^{x^{x^{{\cdot^{\cdot^{\cdot^{x}}}}}}} \]

Write a function called power_tower(x), which prints the tower power for the integer \(x\).

Example

>>> power_tower(1)
1
>>> power_tower(2)
4
>>> power_tower(3)
19683

Hint

Powers can be computed using the ** operator. x ** y is how you write \(x ^ y\) in Python.

You need to use an accumulator here. However, in this case your accumulator will not be using the addition operator. It will be using the ** operator!


Blurred Images

In a file called blurred_images.py write a function called simple_blur(a_picture). This function should display a blurred version of the image parameter.

To blur an image, you simply average a pixels color value with the 4 pixels surrounding it.

Example

Before After

Note: This image has actually been blurred 4 times, to make it obvious what is happening. Your image will only be slightly fuzzy.

Hint

You need to iterate over the pixels of your image for this assignment. However, you need to know the x and y locations of each pixel. You can either use the nested for loop to get x and y coordinates, or you can use the getX(pixel) and getY(pixel) functions to get your coordinates.

You also need to get the 4 pixels surrounding your current location. You can use the getPixelAt(a_picture, x, y) function to get a pixel at a specific location from the picture. You simply need to add and subtract 1 from x and y each.

For each of these 4 surrounding pixels, you want to getRed, getGreen, and getBlue. You will compute the average of each of those values, and use setRed, setGreen, and setBlue to set the color of the pixel.