Service company (4 points)¶
A service company offers different types of services as maintenance, cleaning, safety, repair, etc. It manages a daily services file with the following data: worker id (str), service type (str), shift (str), arrival time (int), i. e. time it takes for the worker to get to the place that needs the service, service duration (int) and a bool that indicates whether this service was urgent or not. Times are expressed in minutes. Example:
A28;REPA;MOR;15;241;False
A28;REPA;MOR;31;33;False
A44;MAIN;AFT;21;23;True
A44;MAIN;AFT;12;234;True
A73;CLEA;NIG;24;71;True
A74;SAFE;NIG;15;234;True
Download the following example files, needed for the tests: servicesA.txt, servicesB.txt, servicesC.txt.
This company wants to solve the next problems with three Python functions that must be saved to the same file service.py:
Classify urgent services by service type (1.5 points).
Write function
service1()that takes the name of a file (str) as described, and returns a dictionary (dict) corresponding to urgent services. The key of the dictionary is the service type and the value is alistwith the arrival times of those services of this type that were urgent. These lists must be in the same order as in the given file. Example:>>> ds1 = service1('servicesA.txt') >>> if ds1 != {'MAIN': [30, 22, 17, 14, 12, 19, 8], ... 'CLEA': [12, 13, 22, 3], ... 'SAFE': [41, 42, 31, 8, 13, 10, 12, 15, 32]}: ... print(ds1)
Note
You have more tests in file
test-service1.txtObtain the service type with larger average arrival times (1 point).
Write function
service2()that takes a dictionary (dict) as the one returned byservice1(). This function must compute the average of the arrival times for each service type and must return atuplewith two elements. The first one is the service type with the maximum computed average of arrival times (str) and the second one is this maximum average of arrival times (float). You can consider that this maximum is unique and that the dictionary is not empty. Example:>>> ds1 = {'MAIN': [30, 22, 17, 14, 12, 19, 8], ... 'CLEA': [12, 13, 22, 3], ... 'SAFE': [41, 42, 31, 8, 13, 10, 12, 15, 32]} >>> (tmax1, maxim1) = service2(ds1) >>> round(maxim1, 2) 22.67 >>> tmax1 'SAFE'
Note
You have more tests in file
test-service2.txtObtain a file with data corresponding to a specific service type (1.5 points).
Write function
service3()that takes the name of a file (str) as the one described in the first paragraph, and a service type (str), and creates another file corresponding to the given service type. The name of this file is the name of the service type followed by the extension'.txt'. Each line of this file has three data fields: the worker id, the shift and the total time (arrival plus duration) for this service (int), separated by a space. Example:>>> service3('servicesA.txt', 'MAIN') >>> with open('MAIN.txt', 'r') as fm: ... sm = fm.readlines() >>> sm ['A23 MOR 155\n', 'A23 MOR 136\n', 'A23 MOR 52\n', 'A26 MOR 57\n', 'A44 AFT 246\n', 'A47 AFT 123\n', 'A75 NIG 275\n'] >>> service3('servicesA.txt', 'SAFE') >>> with open('SAFE.txt', 'r') as fs: ... ss = fs.readlines() >>> ss ['A25 MOR 72\n', 'A25 MOR 87\n', 'A27 MOR 80\n', 'A27 MOR 156\n', 'A46 AFT 114\n', 'A48 AFT 147\n', 'A48 AFT 140\n', 'A74 NIG 249\n', 'A74 NIG 97\n']
Note
You have more tests in file
test-service3.txt
Hint
Remember to use functions int and str to decode and encode integer values.