To start this assignment, download this zip file.
The following guide pages cover material needed for this assignment:
Homework 4b - Substrings
1. Practice Problems
In practice_problems.py
you will find the stubs of several functions, along
with documentation that explains what each function should do:
-
for_reals(text)
— Replace all ’%’ with ’ percent’ and all ’!’ with ’ (for reals).’ Notice there are spaces in these. -
doubles(text)
— Replace all “oo” with “oooooo” and all “ee” with “eeeeee”. -
upper_vowels(text)
— Make all vowels uppercase. -
only_o(text)
— Replace all vowels (aeiou) with ‘o’, preserving the casing of each letter.
You should implement all of these. There are tests provided.
You may write additional functions in the file as needed.
2. Reverse Wordle
Write a program that:
- Prompts the person for a string of characters to block out
- Prompts the person for a replacement character
- Prompts the person for a sequence of inputs
- Replace all blocked characters in the input with the replacement character
Match the prompts and formatting from the example.
Example
Characters to block: blocked
Replacement: *
Word: fish
fish
Word: book
****
Word: CHOOSE
*H**S*
Word:
3. Out of Stock
Write a program that:
- Using a sequence of inputs, prompt the person for a list of out-of-stock items
- Using a sequence of inputs, prompt the person for the items they would like
to purchase
- If the item is out-of-stock, inform the person
- Otherwise add it to the list of items requested
- Print the available items the person indicated they want to purchase
Match the prompts and formatting from the example.
Example
What items are out of stock?
Item: toilet paper
Item: eggs
Item:
What items would you like to purchase?
Item: tuna
Item: eggs
I'm sorry, the item EGGS is out of stock.
Item: spinach
Item:
You have 2 items:
- tuna
- spinach
Tests
Be sure you can pass the tests before you turn in the assignment. Review the guide on using pytest if you need to review using pytest and what to do if a test fails.
Grading
Activity | Points |
---|---|
Practice problems | 6 |
reverse_wordle.py | 6 |
out_of_stock.py | 8 |
Manual Grading
Refer to the Quality Code guide page for detailed explanations and examples on each of these rubric criteria.
Intent
The intent of this assignment is that you can detect and replace substrings, while recognizing when to build strings using the pattern shown in 4a. You should understand how in
applies to strings and lists.
Some problems are best solved using replace()
, like when looking for a substring bigger than a character. Your code might look something like this:
new_string = string.replace(old_substring, new_substring)
Other problems are best solved using a for loop. Replacing single characters with other single characters is typically done by building the string one character at a time. Your code might look something like this:
new_string = ""
for char in string:
if condition(char):
char = change_letter(char)
new_string += char
Rubric | Points |
---|---|
Whitespace | 1 |
Naming | 1 |
Decomposition | 4 |
Intent | 4 |
Total | 10 |