BYU logo Computer Science

To start this lab, download this zip file.

Lab 12 - File Write

Activities

add_bullets.py

Write a program that writes an output file that has the same contents as the input file but adds bullets ”*” to each line of the input file.

input_file.txt

eggs
milk
apples
cereal
bread
bananas
cheese

For example:

python add_bullets.py input_file.txt output_file.txt

Would write the file: output_file.txt

* eggs
* milk
* apples
* cereal
* bread
* bananas
* cheese

floor_and_ceiling.py

Write a program that writes an output file that has the same contents as the input file with the following modifications:

  • If an integer in the input file is less than 5, replace it with 5
  • If an integer in the input file is greater than 10, replace it with 10

The input file should a text file containing integers separated by spaces

input_numbers.txt

1 2 3 4 5
19 11 10 18 13
3 16 9 8 11

For example:

python floor_and_ceiling.py input_numbers.txt output_numbers.txt

Would write the file:

output_numbers.txt

5 5 5 5 5
10 10 10 10 10
5 10 9 8 10

TIP

To convert a list of numbers to a string, you can use the function list_of_numbers_to_string found in helpful.py. list_of_numbers_to_string takes a list of strings as its input.

Given the following script named example.py:

from helpful import list_of_numbers_to_string

numbers = ['1', '2', '3', '4']
numbers_as_string = list_of_numbers_to_string(numbers)
print(numbers_as_string)

Running python example.py will print:

1 2 3 4

greater_than_one_hundred.py

Write a program that writes an output file that has the same contents as the input file but every line that sums to greater than 100 is removed.

input_numbers.txt

30 30 30 30
10 10 10 10 10
5 5 5
40 40 40
1 2 3 4 5
55 55

For example:

python greater_than_one_hundred.py input_numbers.txt output_numbers.txt

Would write the file: output_numbers.txt

10 10 10 10 10
5 5 5
1 2 3 4 5

less_than_ten_words.py

Write a program that writes an output file that has the same contents as the input file but every line that has less than ten words is removed.

input_file.txt

one two three four five six even eight nine ten eleven
one two three four five six
one two three
one two three four five six even eight nine ten eleven twelve thirteen
one two three four five six

For example:

python less_than_ten_words input_file.txt output_file.txt

Would write the file: output_file.txt

one two three four five six even eight nine ten eleven
one two three four five six even eight nine ten eleven twelve thirteen

replace_with_fraction.py

Write a program that writes an output file that has the same contents as the input file but each integer in the input file is divided by the sum of the integers in its line.

numbers.txt

5 5 5 5 5
5 15
4 4 4 4

For example:

python replace_with_fraction numbers.txt fractions.txt

Would write the file: fractions.txt

0.2 0.2 0.2 0.2 0.2
0.25 0.75
0.25 0.25 0.25 0.25

Note: The decimals in each line will add up to one.

smallest_largest.py

Write a program that writes an output file that has the same contents as the input file with the following modifications.

For each line:

  • Subtract the smallest number in the line from every number in the line
  • Divide each number in the line by the maximum number in the line
  • Print the resulting list of numbers to the output file with a space between each number

input.txt

5 15 15 20
2 4 8 4 2 4

For example:

python smallest_largest.py input.txt output.txt

Would write the file: output.txt

0.0 0.5 0.5 0.75
0.0 0.25 0.75 0.25 0.0 0.5

Grading

ActivityPoints
add_bullets.py5 points
floor_and_ceiling.py5 points
greater_than_one_hundred.py5 points
replace_with_fraction.py5 points
less_than_ten_words.py5 points
smallest_largest.py5 points