BYU logo Computer Science

To start this lab, download this zip file.

Project 2 - Statistics

Instructions

You are provided with several files that contain rows of numbers.

Write a program named compute_stats.py that meets the following specifications.

Use the provided test_compute_stats.py script to test your submission.

Submit your compute_stats.py file to Gradescope.

Arguments

  • The first argument is the file to read
  • The second argument is the file to write

Input

  • An input file contains 1 or more rows.
  • Each row contains 1 or more numbers.
    • The numbers may contain decimal points
  • Each row contains only numbers. There will be no alphabetic characters.

Example

1 2 -3 4 5
0 -7.2 8
12345

Output

For each row of numbers in an input file, filter out all negative numbers, then write a corresponding row in the output file that contains the following:

  • First entry (labeled first in the output; float)
  • Last entry (labeled last in the output; float)
  • Minimum (labeled min in the output; float)
  • Maximum (labeled max in the output; float)
  • Average (labeled ave in the output; float)
  • Number of entries (labeled count in the output; unlike the other metrics, this will be an int)

So, for a line of input:

-3 1 2 -7 4 5

You should produce the corresponding output line:

first: 1.0, last: 5.0, min: 1.0, max: 5.0, ave: 3.0, count: 4

Example

input.txt

1 2 3 4 5
10 -5 8 -16 42
-90 -100 20 1
17 18 19 -20 2

The command:

python compute_stats.py test_files/input.txt output.txt

Would write the file:

output.txt

first: 1.0, last: 5.0, min: 1.0, max: 5.0, ave: 3.0, count: 5
first: 10.0, last: 42.0, min: 8.0, max: 42.0, ave: 20.0, count: 3
first: 20.0, last: 1.0, min: 1.0, max: 20.0, ave: 10.5, count: 2
first: 17.0, last: 2.0, min: 2.0, max: 19.0, ave: 14.0, count: 4

Help

  • If you clobber (i.e., overwrite) an input file, you can get a new copy by unzipping the project archive again. However, be careful to unzip the archive in a different directory, or you will overwrite your compute_stats.py file!

Tests

  • positive_integers.input.txt will test whether your program functions correctly with positive integers.
  • positive_and_negative_integers.input.txt will test whether your program filters negative numbers correctly.
  • floats.input.txt will test whether your program handles floats correctly.
  • basic_data.input.txt will test the functionality of your program with basic data.
  • advanced_data.input.txt will test the functionality of your program with advanced data.

Grading

Test FilePoints
positive_integers.input.txt10 points
positive_and_negative_integers.input.txt20 points
floats.input.txt20 points
basic_data.input.txt20 points
advanced_data.input.txt30 points