To start this assignment, download this zip file.
Lab 4e — Split and join
Preparation
1 minute
Download the zip file for this lab, located above. This zip file has code that
you will use for this assignment. Extract the files and put them in your cs110
directory in a folder called lab4e
.
Review
Review this `cats_to_cougars.py” code from the guide on split and join:
def cats_to_cougars(line):
words = line.split()
new_words = []
for word in words:
if word == 'cat':
word = 'cougar'
new_words.append(word)
return ' '.join(new_words)
def main():
lines = ["BYU's mascot is cool, like a cat or something.",
"Just like a cat can, he can catch stuff."]
for line in lines:
print(cats_to_cougars(line))
if __name__ == '__main__':
main()
Discuss with the TA:
- How does
cats_to_cougars()
work? Be sure everyone understands every line of this code. - What does
split()
do in this code? - What does
join()
do in this code?
Cats and dogs
20 minutes
Write a program that:
- reads lines from input
- changes the lines so that
- words containing ‘cat’ are surrounded with a cat emoji
- words containing ‘dog’ are surrounded with a dog emoji
- prints out the new lines
You can find starter code in cats_and_dogts.py
, which also has comments
guiding you through the exerise.
When you run the finished program, you should see something like this:
python cats_and_dogs.py
> I like cats and dogs.
I like 🐈cats🐈 and 🐕dogs.🐕
> You should doggedly concatenate your strings!
You should 🐕doggedly🐕 🐈concatenate🐈 your strings!
> There is peace today -- dogs are sleeping with cats.
There is peace today -- 🐕dogs🐕 are sleeping with 🐈cats.🐈
>
This program has two pieces:
-
The
main()
function handles the input loop. You should recognize this from Unit 3. Review the guide on input loops as needed. -
The
do_subs()
function takes the line and changes it as needed. This follows a new pattern, where you:split()
the line into a list of words- change some of the words
- accumulate original and changed words in a new list
- convert the list of words back into a string using
join()
You can review the code and guide links above to see how to do this.
Discuss with the TA:
- How do you modify a word to have something before and after it?
- Share a solution and see if anyone did it differently.
Doubling a recipe
20 minutes
Write a program to double a recipe. This program will take two arguments:
- the input file
- the output file
The program reads every line of the recipe, finds any words that are numeric and doubles them, then saves all the lines to the output file. You can call the program like this:
python double_recipe.py banana-bread.txt doubled-banana-bread.txt
Don’t worry too much about whether doubling makes sense. So if the recipe says:
1 cup of flour
Bake for 40 minutes
The doubled recipe should say
20 cups of flour
Bake for 80 minutes
This is OK!
You can also ignore any floats, so 1.3 cups can remain undoubled.
We have provided code in double_recipe.py
that has decomposed the problem and
has some comments to guide you. There are two main parts:
-
double_quantities(lines)
should go through all the lines and call a function to do the doubling. It returns the new lines. -
double_quantity(line)
takes just one line and does the modifications needed to double any quantities in that line.
Discuss with the TA:
- How do you modify a word that has a quantity in it?
- How do you make sure a newline is at the end of each line?
- Share a solution and see if anyone did it differently.
Grading
To finish this lab and receive a grade, take the canvas quiz.
Solution
We are providing a solution so you can check your work. Please look at this after you complete the assignment. :-)