To start this assignment, download this zip file.
The following guide pages cover material needed for this assignment:
Homework 4d — Files
For each of these problems, you will be using the file processing pattern. See the guide on reading and writing files for help. You will be starting with a blank file for each of these problems. The accompanying lab was designed to get you ready for this. Good luck! :-)
1. Add bullets
Write a program named add_bullets.py
that takes the following arguments, in
this order:
- an input file
- an output file
- a string to use for bullets
The program takes the input file and adds the supplied string, plus a space, before each line.
We have given you an input file called bullets.input.txt
that contains:
here are some lines
add some bullets to them
and have a great day!
When you run your add_bullets.py
program like this:
python add_bullets.py bullets.input.txt bullets.output.txt "@"
Then the file called bullets.output.txt
should contain:
@ here are some lines
@ add some bullets to them
@ and have a great day!
2. Filter NaN
Write a program named filter_nan.py
that takes the following arguments, in
this order:
- an input file
- an output file
The input file will have a set of lines, each one containing either an integer, a float, or “NaN”, which means “not a number”. You need to remove any lines that contain “NaN” and write the output file with all the other lines.
We have given you an input file called nan_input.txt
that contains:
NaN
42
NaN
NaN
3.0
1.23456789
NaN
When you run your filter_nan.py
program like this:
python filter_nan.py nan_input.txt nan_output.txt
then the file nan_output.txt
should contain:
42
3.0
1.23456789
3. Ooooo
Write a program named ooooo.py
that takes the following arguments, in this
order:
- an input file
- an output file
The input file will have a set of lines. You need to replace all vowels with the
letter o
, then write the result to the output file.
We have given you an input file called peter_piper.txt
that contains:
Peter Piper picked a peck of pickled peppers,
A peck of pickled peppers Peter Piper picked;
If Peter Piper picked a peck of pickled peppers,
Where’s the peck of pickled peppers Peter Piper picked?
When you run your ooooo.py
program like this:
python ooooo.py peter_piper.txt peter_ooooo.txt
then the file peter_ooooo.txt
should contain:
Potor Popor pockod o pock of pocklod poppors,
o pock of pocklod poppors Potor Popor pockod;
of Potor Popor pockod o pock of pocklod poppors,
Whoro’s tho pock of pocklod poppors Potor Popor pockod?
4. Redaction
Write a program named redaction.py
that takes the following arguments, in this
order:
- an input file
- an output file
- a string pattern
The input file will have a set of lines. You need to replace all substrings
equal to the pattern with ****
, then write the result to the output file.
We have given you an input file called peter_piper.txt
that contains:
Peter Piper picked a peck of pickled peppers,
A peck of pickled peppers Peter Piper picked;
If Peter Piper picked a peck of pickled peppers,
Where’s the peck of pickled peppers Peter Piper picked?
When you run your redaction.py
program like this:
% python redaction.py peter_piper.txt peter_redacted.txt "Peter Piper"
then the file peter_redacted.txt
should contain:
**** picked a peck of pickled peppers,
A peck of pickled peppers **** picked;
If **** picked a peck of pickled peppers,
Where’s the peck of pickled peppers **** picked?
This is only fitting, since Peter Piper is a suspect but has not yet been convicted.
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 |
---|---|
Add bullets | 5 |
Filter NaN | 5 |
Ooooo | 5 |
Redaction | 5 |
Manual Grading
Refer to the Quality Code guide page for detailed explanations and examples on each of these rubric criteria.
==For the purpose of decomposition in this class, we expect reading lines, processing lines, and writing lines to happen in at least three distinct functions.==
Intent
The intent of this assignment is that you can read and write to files, using list patterns on lists of lines, as well as characters within a line.
Follow the guidelines in the quality code section on if __name__ == "__main__":
.
Reading a file using read_lines() will give you a list of strings. Your for loops should show that you know if you are iterating through strings in a list or characters in a string.
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 |