BYU logo Computer Science

To start this assignment, download this zip file.

The following guide pages cover material needed for this assignment:

Homework 4e — Split and join

For some of these problems you will be using the file processing pattern. See the guide on reading and writing files for help.

You will also be using split and join to modify words in a line, using concepts taught in the guide on split and join. The previous lab was designed to get you ready for this.

1. Sum numbers

Write a program named sum_nums.py that takes the following arguments, in this order:

  • an input file

The input file will have a set of lines, each one containing various integers. The program should sum all of these numbers.

We have given you an input file called sum_nums.input.txt that contains:

10 20 5
1 7 8
230

When you run your program like this:

python sum_nums.py sum_nums.input.txt

then it should print:

The total is 281

2. Replace a word

Write a program named replace_word.py that takes the following arguments, in this order:

  • an input file
  • an output file
  • an old word
  • a new word

The program takes the input file and replaces each instance of the old word with the new word. This should replace words only, and should not do string replacement. Write the new contents of the file to the output file.

We have given you an input file called replace_word.input.txt that contains:

How much wood would a woodchuck chuck if a woodchuck could chuck wood?
He would chuck (he would) as much as he could, and chuck as much wood
As a woodchuck would if a woodchuck could chuck wood.

When you run your replace_word.py program like this:

python replace_word.py replace_word.input.txt output.txt chuck pluck

Then the file called output.txt should have the same contents as the input file, but the word “chuck” replaced with the word “pluck”.

3. Older customers

Write a program named older_customers.py that takes the following arguments, in this order:

  • an input file
  • an output file
  • an age (integer)

The input file is a CSV file. This means it will have a set of lines, and each line has fields that are separated by a comma ,. The fields are:

  • Customer ID
  • Gender (Male/Female)
  • Age (integer)
  • Annual Income in Dollars (integer)
  • Spending Score (from 1 to 100)
  • Profession
  • Work Experience (integer)
  • Family Size (integer)

You need to write the output file so that it only has lines of individuals older than the specified age. Be sure to also keep the first line. You can identify the first line by checking if the first word, after you split the line, is equal to “CustomerID”, or checking to see if “CustomerID” is in the line.

After checking for the first line, you will need to to split each line into different items. To get a specific item, you could either use list indexing or unpack the list like a tuple.

For example:

split_line = line.split(',')
id, gender, age, income, score, profession, exp, family = split_line

or

age = split_line[2]

You can run your program like this:

% python older_customers.py customers_small.csv older.csv 20

This should keep only two customers in the new file:

CustomerID,Gender,Age,Annual Income ($),Spending Score (1-100),Profession,Work Experience,Family Size
2,Male,21,35000,81,Engineer,3,3
4,Female,23,59000,77,Lawyer,0,2

4. Average income

Write a program named average_income.py that takes the following arguments, in this order:

  • an input file
  • a profession

The input file is the same CSV file as above. The program should find all of the lines where the person has the same profession as given and then calculate the average income for those people. The average should be rounded to an integer.

You can run your program like this:

% python average_income.py customers_small.csv Engineer

This should keep average the income for just the customers who are Engineers and print:

The average income of Engineer is 60500

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

ActivityPoints
Sum numbers2
Replace word4
Older customers7
Average income7

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 use split and join to modify text separated by commas or spaces.

Your code should show that you understand:

  • Split creates a list of strings
  • Join takes a list of strings and makes one big string
  • Join puts a delimiter between the strings, not at the ends
  • Strip removes characters from the ends of strings
RubricPoints
Whitespace1
Naming1
Decomposition4
Intent4
Total10