M03 Assignments

Introduction

The lessons for M03 focus on file and string handling. The ability to analyze and process files is an important skill for any programming language.

When reading text files, each line can be read individually, and then processed as a string.

String handling often involves using the following basic operations:

  • split() the string into individual words
  • count the number of split items: get the length of the array created by split()
  • count the characters in a string using len() or by looping through the characters in the string

M03_Assignments

M03 Assn 1 Exercise #1 (Read a File and Do Counts)

The objective of this assignment is to practice reading a text file, splitting each line to count words and characters.

Download this file: Text File

Create a program that opens the text file for reading. Count the number of lines, the number of words in each line, the number of non-space characters in each line, and keep track of the totals for words and characters.

  1. Use a variable to store the file path and name
  2. Open the file for reading (use the variable)
  3. Read each line in the file
  4. Ignore any lines that have a length of 1 (blank lines)
  5. Count the number of lines
  6. Split each line and count the number of words
  7. Write a loop to count all the non-space characters in the line
  8. Display the number of lines, total number of words, and total characters in the file

Note: It is important to skip counting blank lines and spaces. Blank lines have a length of 1 (return character).

Tips

  • Start by making the program to only open and read the file
  • Print each line to see what they look like (you can comment this out after testing it)
  • Check the length of the line and only do further analysis if the line length is greater than 1
  • Create variables at the top of the program to store the line count, total word count and total character count
  • Split each line to count the number of words for that line; add the result to the total words variable
  • Loop through the characters in each line to count characters that are not a space; add the count to the total characters

Extra Credit (10 points)

  • Prompt the user and ask if the counts should be displayed for each line
  • If the counts should be displayed, show the line number, and the word and character count for each line
  • Hide the counts for each line unless the user answers "y"

Testing: Your results and counts should match those in the image below. (For those doing the extra credit, I will try displaying and not displaying the line information.)

m03 assn1 exercise #1 sample output #1
Figure 1: M02 Assn1 Exercise #1 Sample Output #1
m03 assn1 exercise #1 sample output #2
Figure 2: M02 Assn1 Exercise #1 Sample Output #2

M03 Assn 1 Exercise #2 (Review ACME Employees)

The objective of this assignment is to practice reading a "data" file, splitting each line into a set of data points.

Download this file: ACME Employees.

Open the data file and review the contents. The file extension is ".txt", but it is a data file where each line contains data points separated by spaces. The data points are first name, last name, title/department, and salary.

Your boss wants a printout of all the employees. Include the line number, full name - in the format Last Name, First Name, and the salary. Total the salaries so you can calculate an average salary.

Print a summary of the file, including the number of employees, and the average salary.

Create a program that opens the data file for reading. There are no user prompts needed.

  1. Use a variable to store the file path and name
  2. Open the file for reading (use the variable)
  3. Read each line in the file. You may need to remove any end-of-line character from each line (see below)
  4. Count the number of lines
  5. Aggregate the salaries
  6. Display line number, full name (see above), and salary for each employee
  7. Display a summary

Note: The replace() function can be used to substitute a string or character for another string or character. One strategy to remove the end-of-line (EOL) character from lines read from a file is to replace the string '\n' with an empty string(''):

line = line.replace('\n', '')

Testing: Your results and counts should match those in the image below.

m03 assn1 exercise #2 sample output
Figure 3: M03 Assn1 Exercise #2 Sample Output

M03 Assn 2 Exercise #3 (Analyze Cities)

The objective of this assignment is to practice reading a "data" file, splitting each line into a set of data points.

Download this file: World Cities Data File.

Open the data file and review the contents. The file extension is ".csv", which stands for "Comma Separated Values". The original CSV files were almost always separated only by commas. But nowadays, a CSV file may contain others delimiters, such as a vertical bar (|). One advantage of using a delimiter other than a comma is that the data itself may have commas. You should see that the data file contains 28 rows, uses the vertical bar as the delimiter, and each data row contains 4 values: city name, country name, metropolitan population (the area around and including the city), and the actual city population. For example, New York (line 16) has a city population of 8 million, but the wider metropolitan area around it has a population of 18 million.

Create a program that opens the text file for reading. Prompt the user to display either metro data or city data. Then prompt the user for a country name on which to collect statistics.

  1. Use a variable to store the file path and name
  2. Open the file for reading (use the variable)
  3. Read each line in the file. You will probably need to remove any end-of-line character from each line (see below)
  4. Only process lines that have a length greater than 1
  5. Count the number of lines
  6. Skip the first 4 lines that contain header information; the data lines start at line 5
  7. Split each line on the vertical bar character. The creates a temporary array with 4 cells, containing the city, country,metro population and city population
  8. Display the city, country and the appropriate population for each data line
  9. If the country is the one selected by the user to collect stats on, count the entries for that city and total the population
  10. Display the statistics for the select country, including an average population

Note: It is important to skip counting blank lines and spaces. Blank lines have a length of 1 (return character).

Tips

  • Change the user city entry to lowercase using .lower()
  • Compare the country on each line to the user city, by also converting the data line country using .lower()
  • Check that the number of cities > 0 before calculating an average

Testing: Your results and counts should match those in the image below.

m03 assn1 exercise #3 sample output
Figure 4: M03 Assn2 Exercise #3 Sample Output

M03 Assn 2 Exercise #4 (Enter Sales Records and Save File)

The objective of this assignment is to practice creating a data file.

You work for ACME Sales Corp., in charge of tracking weekly sales for a single store.

Every week, the store manager reports the daily sales for each day in the past week. Create a program to enter the sales, by day, and then display the total sales and average sales for the week.

Create a program that opens a text file for writing. Using a for loop, prompt the user for the sales for seven days, starting with Monday. Depending on the day, display the day name ("Mon","Tue"...). For each day, write the store the day number, day name, and sales amount to the file.

You can choose how to store each line of data. Pick a delimiter (space, comma, vertical bar...) and separate each data point with the delimiter.

When done with data entry, close the file, then open the file for reading. Read in the each line and print the data. Aggregate the sales data so that you can report total sales for the week, and the average daily sales.

Testing: Your results and counts should look similar to the image below.

m03 assn2 exercise #4 sample output
Figure 5: M03 Assn2 Exercise #4 Sample Output