Store Statistics in Python
Overview
- Due:
- 11:00pm, Friday September 18, 2020
- Max grace days:
- 2
The purpose of this assignment is to write a basic Python program.
Description
A store needs a program to calculate the sales statistics for each department this year. The store has standard sales amount (in thousand dollars) for each month based on data from previous years.
The monthly sales amounts for departments are stored in a file sales.dat
. Each line in the file contains twelve sales amounts for that department.
Month | Standard |
---|---|
Jan | 23.0 |
Feb | 33.1 |
Mar | 21.0 |
Apr | 23.5 |
May | 54.0 |
Jun | 34.3 |
Jul | 35.0 |
Aug | 45.0 |
Sep | 56.3 |
Oct | 45.6 |
Nov | 34.0 |
Dec | 55.0 |
Write a program to calculate statistics for each department in the store. Your program should do the following:
- Prompt the user for the input file.
- Read the file data into a list of lists – a 2D list.
- Compute the average monthly sale for each department.
- Compare each monthly sales amount with the standard.
- Write the statistics for the department, including department number, average sales amount, numbers of months above and below the standard, and performance to a file. The program should output “unsatisfied” as the performance of the department if more than four months are below the standard and “satisfied” otherwise.
The output file must have the following format: the first line must be
Department,Average,Above,Below,Performance
and each subsequent line contains the appropriate values in the correct order separated by commas. The average value must be output with one decimal point of precision.
Your program must be composed of the following functions with the exact names, formal parameters and return values:
get_data
: takes a string representing a filename and returns a Python list of lists of floats read from the file. The outer list has the same length as the number of rows in the files and each inner list has a length of 12. That is, each inner list represents a single line of the file.process_data
: takes a list of lists of floats and returns a list of of dictionaries. Each dictionary must have the following key names and values where the values correspond to the respective inner list:"Department"
: (int) the department number (the inner list number where the first inner list is 1)."Average"
: (float) the average of the list"Above"
: (int) the number of entries greater than or equal to the standard"Below"
: (int) the number of entries less than the standard"Performance"
: (string) “unsatisfied” if more than four months are below the standard, otherwise “satisfied”. The standard values must be local to this function.
write_to_file
: takes a list of dictionaries of the form returned fromprocess_data
function and writes the values to a file. The file format is described above. This function must use at least one Python format string as part of the implementation. The output filename must beout.dat
.main
: prompts the user for a filename and calls the other functions with the appropriate arguments.
Note that you may create your own helper functions if you wish. The last line in the script should call the main
function.
The use of any of the following Python features will result in a grade of zero for this assignment:
import
statements- list comprehensions
- generators
- higher-order functions
Sample input file
sales.dat
23 33.5 21 23 25 56 54 43 34.2 35.4 34 69.5
24 35.2 24 26 43 56.7 54 32 43 34 34 57.9
24 42 43 35 52 56 67 54 56 45.3 32 32
20 32 45 72 45.4 63.2 45 56 52 65 53 65
34 35 37.5 32 23 45 31 43 52 43 76 65
35 56 63.4 45.2 45.6 56 67.3 45 56.3 67 78 76
34.2 45 62 19 45 39 38 37 82 74 45 58.4
Sample output file
out.dat
Department,Average,Above,Below,Performance
1,37.6,7,5,unsatisfied
2,38.6,8,4,satisfied
3,44.9,7,5,unsatisfied
4,51.1,8,4,satisfied
5,43.0,7,5,unsatisfied
6,57.6,11,1,satisfied
7,48.2,9,3,satisfied
Turning in the Assignment
You must turn in a file named main.py
. Submit the program source file to the appropriate folder on D2L.
Grading Criteria
Grading (out of 100 points):
- 5 points – correct file name
- 20 points –
main
function- 5 points – correct function name
- 5 points – correct function documentation
- 10 points – correct implementation of the specification
- 25 points – correct
get_data
function- 5 points correct function name
- 5 points correct function documentation
- 15 points – correct implementation of the specification
- 25 points – correct
process_data
function- 5 points correct function name
- 5 points correct function documentation
- 15 points – correct implementation of the specification
- 25 points – correct
write_to_file
function- 5 points correct function name
- 5 points correct function documentation
- 15 points – correct implemenation of the specification