Time control

A factory uses a time stamping system that controls its employees entry and exit times and these data is recorded daily into a file. Each entry or exit of an employee results in a line of this file with the following data: NIF, name and surname, the entry or exit time (in format HH:MM) and a character that indicates whether the employee entered or left work ('E' or 'L'). These data are separated by ', ' (comma-whitespace) and lines are ordered chronologically. The file contains two initial lines that describe the fields. For example:

NIF,       Name/Surname,         Time, E/L
-----------------------------------------
11223344F, Ramon Pladevall Homs, 8:00, E
44337799P, Santiago Forns Cheng, 8:01, E
77722212M, Corneli Perez Simon, 8:33, E
11122233V, Anna Pi Fort, 9:30, E
44337799P, Santiago Forns Cheng, 10:31, L 
52458425H, Maria Sans Rocafort, 11:00, E
11223344F, Ramon Pladevall Homs, 11:45, L
77722212M, Corneli Perez Simon, 13:04, L
11223344F, Ramon Pladevall Homs, 13:30, E
52458425H, Maria Sans Rocafort, 13:55, L
11122233V, Anna Pi Fort, 14:00, L
44337799P, Santiago Forns Cheng, 14:01, E
77722212M, Corneli Perez Simon, 15:45, E
11122233V, Anna Pi Fort, 15:55, E
44337799P, Santiago Forns Cheng, 16:50, L
11122233V, Anna Pi Fort, 17:30, L
11223344F, Ramon Pladevall Homs, 18:00, L
77722212M, Corneli Perez Simon, 18:30, L

You can download the example file entryexit.txt

Save these functions into a file named control.py.

  1. Write the function control() that given the name of a file as described above and an integer indicating a time (hour), h, returns the number of workers that have entered to work between h (included) and h+1 (excluded). Examples:

    >>> control ('entryexit.txt', 8)
    3
    >>> control ('entryexit.txt', 9)
    1
    >>> control ('entryexit.txt', 11)
    1
    >>> control ('entryexit.txt', 15)
    2
    

    Note

    More tests are provided in file test-control.txt

  2. Write the function add_time() that given 4 integers corresponding to the hours and minutes of a first time and the hours and minutes of a second time, returns two integers corresponding to the hours and minutes of the sum of the two given times. Hours and minutes must be positive and minutes must be less than 60. Examples:

    >>> add_time(2, 45, 3, 35)
    (6, 20)
    >>> add_time(1, 20, 4, 15)
    (5, 35)
    >>> add_time(3, 50, 1, 55)
    (5, 45)
    

    Note

    More tests are provided in file add_time.txt

  3. Write the function subtract_time() that given 4 integers corresponding to the hours and minutes of a first time and the hours and minutes of a second time, returns two integers corresponding to the hours and minutes of the difference between the first time and the second time. Hours and minutes must be positive and minutes must be less than 60. Examples:

    >>> subtract_time(3, 45, 2, 35)
    (1, 10)
    >>> subtract_time(2, 20, 2, 15)
    (0, 5)
    >>> subtract_time(3, 5, 1, 55)
    (1, 10)
    >>> subtract_time(5, 15, 2, 35)
    (2, 40)
    

    Note

    More tests are provided in file subtract_time.txt

  4. Write the function control_1() that given the name of a file as described above and the NIF of a worker of the company, calculates how long this worker has been working. The function must return two integers corresponding to the hours and minutes of the working time. This function must use the previous two. A worker may come in and out more than once. Examples:

    >>> control_1 ('entryexit.txt', '11223344F')
    (8, 15)
    >>> control_1 ('entryexit.txt', '44337799P')
    (5, 19)
    >>> control_1 ('entryexit.txt', '77722212M')
    (7, 16)
    >>> control_1 ('entryexit.txt', '11122233V')
    (6, 5)
    

    Note

    More tests are provided in file test-control_1.txt

  5. Write the function control_2() that given the name of a file as described above and the NIF of the factory CEO, returns True if, and only if, the CEO has been the last one to come into work. If the CEO did not come into work, the function must also return True. With the given data, if the CEO is Maria Sans, the function has to return True; however, if the CEO is any of the other four employees, the function must return False. Tip: build an auxiliary dictionary in which the key is the NIF and the value the first time that the worker has come into work. Examples:

    >>> control_2 ('entryexit.txt', '11223344F')
    False
    >>> control_2 ('entryexit.txt', '44337799P')
    False
    >>> control_2 ('entryexit.txt', '77722212M')
    False
    >>> control_2 ('entryexit.txt', '11122233V')
    False
    >>> control_2 ('entryexit.txt', '52458425H')
    True
    >>> control_2 ('entryexit.txt', '12345678X')
    True
    

    Note

    More tests are provided in file test-control_2.txt

Solutions

A solution of these functions is provided in file control.py