Time Left¶
Let’s consider a str s with a text ended by a dot '.' with two parts separated by the word 'and'.
For instance, consider the text 'Your class starts at 10:05:00 and it is now 07:30:30.'. If we exclude the 'and' and the '.' then the two parts are 'Your class starts at 10:05:00` and 'it is now 07:30:30'.
Each part includes a time right at the end: in the first part it is right before the word 'and' (separated by a space ' ') and in the second part it is right before the dot character ('.'). As you can see in the example, a time is represented by the format hh/mm/ss with three two-digit figures separatd by '/' correspondng to hours, mimutes and seconds respectively.
The first time is when a class will start and the second is the current time.
We wish to get a text indicating the time left until class starts. The format required is 'The class starts in XD hours, XM minutes and XS seconds.', except if the time left is less than 10 minutes in which case the format required is 'Hurry up! The class starts in XM minutes and XS seconds.'. In both texts XH, XM, and XS are the hours, minutes and seconds left until class starts, respectively.
You are required to deliver the following functions (they have the same grade weight) in the module time_module (file time_module.py):
Firstly, an auxiliar function specified as follows:
- extract_time(s)¶
such that
For example:
>>> extract_time('Your class is 10:05:00') (10, 5, 0) >>> extract_time('my clock says now is 07:30:30') (7, 30, 30) >>> extract_time('Your class is 02:00:00') (2, 0, 0)
Doctests are available in the extract_time.test file.
Secondly, the main function:
- time_left(s)¶
such that
For example:
>>> s = 'Your class is at 10:05:00 and it is now is 07:30:30.' >>> time_left(s) 'The class starts in 2 hours, 34 minutes and 30 seconds.' >>> s = 'Your class is at 10:05:00 and my watch says it is now 09:55:00.' >>> time_left(s) 'The class starts in 0 hours, 10 minutes and 0 seconds.' >>> s = 'Your class is at 10:05:00 and and according to my watch it is now 09:55:30.' >>> time_left(s) 'Hurry up! The class starts in 9 minutes and 30 seconds.' >>> s = 'Your class is at 10:05:00 and my watch says now is 10:00:00.' >>> time_left(s) 'Hurry up! The class starts in 5 minutes and 0 seconds.' >>> s = 'Your class is at 10:05:00 and according to my watch it is now 10:00:55.' >>> time_left(s) 'Hurry up! The class starts in 4 minutes and 5 seconds.'
Doctests are available in the time_left.test file.
To code this function you have two code snippets available (which should be very helpful):
The previous function extract_time.
The function time_dif function that computes the difference of two given times. It’s specification is:
- time_dif(h1, m1, s1, h2, m2, s2)¶
such that
given h1, m1, s1, h2, m2, s2,
intwhere h1, m1, s1 represent a time t1 expressed in hours, minutes and seconds, and equally h2, m2, s2 fortime t2, and t1 <= t2.returns three
inth, m ,s that represent the difference in amount of hours, minutes and seconds respectively between t1 and t2 (namely t2-t1) such that 0 <= m < 60 and 0 <= s < 60.
For example:
>>> time_dif(7, 30, 30, 10, 5, 00) (2, 34, 30) >>> time_dif(9, 55, 0, 10, 5, 0) (0, 10, 0) >>> time_dif(9, 55, 30, 10, 5, 0) (0, 9, 30) >>> time_dif(10, 5, 0, 10, 5, 0) (0, 0, 0)
The code of this function can be downloaded from time_dif.py. You can either import it or copy the code in your time_module.