Write a function called count_words(text)
, which prints the
number of words contained in the text. Assume each word is
separated by exactly one space.
>>> count_words("INQ241A") 1 >>> count_words("INQ 241A") 2 >>> count_words("Digital Media is the worst class I've ever taken. Coding is literally impossible") 13
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 counting, so
your accumulator should normally start at 0
.
However, in this case we can assume there's always at least 1
word, we we will start it at 1
.
Inside of your for loop, you need to count the spaces.
You will use the accumulator pattern to make this count work. In this
case, the accumulation will be of the form accumulator =
accumulator + some_value
.
In this case, you want to count the number of space characters (" ") that exist in the string. So, any time you come across a space character, inrement the accumulator by 1.
Use an if statement to determine if the character you
have from the string is a space. Remember, comparisons of
equality use the ==
operator.
Write a function called perfect_power(x)
. This
function should return the result of \(x ^ x\).
>>> perfect_power(1) 1 >>> perfect_power(2) 4 >>> perfect_power(3) 27 >>> perfect_power(5) 3125
There are two ways to accomplish this problem. If you remember how to raise a number to an exponent in python, you can directly use that. Otherwise, you'll need to use a for loop and the accumulator pattern.
Every time you go through the for loop, you want to multiply the accumulator by \(x\). This means you should probably start your accumulator at 1!
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.
Note: This image has actually been blurred 4 times, to make it obvious what is happening. Your image will only be slightly fuzzy.
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.