< Back

Lab 16: Practice and Test 2 Makeup


Practice Problem 1

Write a function called remove_hashtags(text), which prints a version of text with all of the hashtags (\(\#\)) removed

Example

>>> remove_hashtags("#INQ241A")
INQ241A
>>> remove_hashtags("##255, 118, 202", 3)
255, 118, 202
>>> remove_hashtags("#this #is #a #tumblr #post")
this is a tumblr post

Hint

All lines that belong to the function must be indented one tab from the left margin. This signifies that you are including that line of code with the function.

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 accumulating strings, so your accumulator should start at "".

Inside of your for loop, you need to accumulate. Accumulation is usually of the form accumulator = accumulator + some_value, but the operation can sometimes change.

In this case, you want to accumulate any character that is not the "#". Use an if statement in order to facilitate this.


Practice Problem 2

Write a function count_divisors(some_integer), which takes an integer as a parameter. This function should return the number of positive integers that divide some_integer.

Example

>>> count_divisors(1)
0
>>> count_divisors(2)
1
>>> count_divisors(7)
1
>>> count_divisors(10)
3

Hint

This activities is also going to use a for loop. In this case, you don't have a string you are executing on. However, you do know the range of integers you want to iterate over.

You again need to use the accumulator pattern here. Since we are counting, we want our accumulator to start at 0.

You only want to increment the accumulator if the loop variable divides some_integer. You can tell if a number \(x\) divides \(some\_integer\) if \(some\_integer \mbox{ } \% \mbox{ } x \mbox{ } == \mbox{ } 0\).


Test Makeup

You are not allowed to view any additional files when working on this exercise, nor are you allowed to consult with your neighbors. When you are done, submit your file on inquire.


Checkers

In a file called checkers.py, write a function called replace_black(a_picture). This function should replace all black pixels with blue pixels.