Question: Goal : refresher of Python and hands-on experience with file input/output, string manipulation, and en/deciphering. Assignment Specifications In this assignment, you will read and summarize
Goal: refresher of Python and hands-on experience with file input/output, string manipulation, and en/deciphering.
Assignment Specifications
In this assignment, you will read and summarize data from a file containing information about different animals and when they visited certain feeding stations. Animals have radio frequency tags and there are two feeding stations (at different locations), at which the timestamp and the id of an animal is recorded when it visits a station.
Your program will be given the name of a file containing the data and it will need to write some statistics computed from the data to the output. The data in the file is stored as text. An example of an input file looks like as follows:
a01:01-24-2017:s1 a03:01-24-2017:s2 a03:09-24-2017:s1 a03:10-23-2017:s1 a04:11-01-2017:s1 a04:11-02-2017:s2 a04:11-03-2017:s1 a04:01-01-2017:s1 a02:01-24-2017:s2 a03:02-02-2017:s2 a03:03-02-2017:s1 a02:04-19-2017:s2 a04:01-23-2017:s1 a04:02-17-2017:s1 a01:05-14-2017:s2 a02:06-11-2017:s2 a03:07-12-2017:s1 a01:08-19-2017:s1 a03:09-19-2017:s1 a03:10-19-2017:s2 a03:11-19-2017:s1 a03:12-19-2017:s2 a04:12-20-2017:s2 a04:12-21-2017:s2 a05:12-22-2017:s1 a04:12-23-2017:s2 a04:12-24-2017:s2
Each line has three "fields, separated by colons (':'). The first field is the identifier of an animal (in the first line, the identifier is "a01), the second is a date ("01-24-2017 in the first line) and the last one is the name of one of the two stations ("s1 in the first line). Each line corresponds to an animal visit to a station on a given date. The first line means that animal "a01 visited station "s1 on January 24, 2017.
Once the program reads in the file, your program has to print the following information:
1.A table, sorted by animal id, of the number of times each animal visited each station.
2.A sort list of animal id(s), of all the animals that visited station s1 AND station s2 (i.e. BOTH stations) at least 4 times.
3.The total number of times each animal visited the station sorted by animal id.
4.The month that has the highest number of visits to the station(s) and the total number of visits in that month.
The printed output of your program for the above example input file should be as follows:
Input and Output Details
In order to succeed, your program must follow carefully the format of the above output. Further details of the formatting requirements are found below.
The Input File
The following formatting rules apply to the contents of the file:
1.The input file is a file with textual contents
2.Each data line of the file has the following format:
a.
b.
c.The
3.The lines in the input files do not have any particular order.
4.There are no other restrictions on the contents of the lines. In particular, you can have repeated lines (an animal can visit a station more than once on the same day).
5.There could be blank lines and comment lines in the file. Comment lines will always start with a '#'.
The Printed Output
The output must have exactly four sections. After each section, except the last, you need to print sixty (60) '-' characters. Each section must start with a heading. The section heading must be printed even if the section does not have actual contents (hence, the output will always have exactly four sections).
In the first section, you need to print a table specifying the number of times each animal visited each station. The table starts with a heading (as shown above). The rest of the table is sorted by animal ids. In the first column print the animal id, in the second print the number of visits of the animal with the given id to station "s1, while in the third print the number of visits to station "s2. You need to produce the so-called fixed-width column format. In this table, each column must have a fixed width of exactly 20 characters, including the last column. Pad the output (from the right) with the necessary number of spaces to achieve this. For example, if the value that you want to print in the cell has a length of 5 characters, add 15 spaces to the string of 5 characters.
In the second section, you need to print the identifiers of those animals that visited both stations at least 4 times each, each identifier in a new line. Again, the output must be sorted by the animal identifiers. If there are no animals to print, you should not print anything after the heading.
In the third section, print the heading followed by the animal identifier and the total number of times the animal visited a station. There should be only a space between the animal id and the count.
In the fourth section, print the month that has the most number of visits to the stations and the number of visits in that month. If the file has more than one month that has the highest number of visits, then the last month should be printed out. For example if both January and November have 4 visits then the output should be Month 11 has 4 visits, where the number 11 represents November. You may assume that the input file represents only one year, and so you will not have to differentiate between the same months in different years.
Error Handling
The input file may have blank lines and comments that start with the symbol "#'. Your program should ignore these lines. The information regarding the animal id, date and station will always be in the correct format.
Required Data Structures and Functions in the Program
You must create the following data structures in your program:
station1- a dictionary that records frequency of visit of each animal to station 1
station2- a dictionary that records frequency of visit of each animal to station 2
monthly_visits - a dictionary that records the number of visits that occurred for station1 and station2 for each month that exists in the file.
animal_list- a list of all animals that exists in the file
month_list - a list of all months that exists in the file
You must create the following functions in your program:
In addition to a main function, your program should implement the following four functions:
printsection1(animal_list, station1, station2)
This function must accept the animal_list, station1 and station2 data structures as parameters. This function processes these data structures to produce the output for Section 1.
printsection2(animal_list, station1, station2)
This function must accept the animal_list, station1 and station2 data structures as parameters. This function processes these data structures to produce the output for Section 2.
printsection3(animals,station1,station2)
This function must accept the animal_list, station1 and station2 data structures as parameters. This function processes these data structures to produce the output for Section 3.
print_section4(month_list,monthly_visits)
This function accepts the monthly_list and monthly_visits data structure as parameters. This function processes these data structures to produce the output for Section 4.
PLEASE NOTE
The program you design should be general enough so that if the given input file is replaced with another similar file, the program is still able to produce the desired output. That is, your program should work with any input file that follows the format described above.
The format and content of your program output must match the given sample output.
You must use Python3 to write your program
A sample input file animallog.txt and sample output (as it would be printed to the console) has been provided with the assignment.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
