Average Arrival Time of Trains
- Extend the Time class you defined in
a previous exercise.
Add a method
subtract(self, t),
where t is a time of the class Time.
This method calculates the difference (in minutes) between itself and t.
For example, if itself represents the time "21:30" and t represents
"20:40", subtract(self, t) should return 50.
- Design a main program which will read a text file whose contents
are the arrival time of trains.
Print out each line (so that it is easier for you to validate the
result of this exercise).
After that, print the intervals between two successive trains.
- Each line in the data file contains
a time in the format "hh:mm". You may assume that the file is sorted in
ascending order.
- You may test your program with train1.txt
or train2.txt.
- If you encounter the error message "SyntaxError: invalid token", it
is quite possible you provided a string with leading zeroes to the
eval() function. Please read P.144 again and use the int() function to
convert the string.
Your program may run as follows:
Filename? train1.txt
=== Arrival Time of Trains ===
07:25
07:34
07:58
08:12
08:32
08:36
08:58
09:12
09:16
09:32
=== Intervals between arriving trains ===
(1): 9
(2): 24
(3): 14
(4): 20
(5): 4
(6): 22
(7): 14
(8): 4
(9): 16
Average: 14.1 minutes