how many people work at a given time for each day of the week, python

Viewed 64

I am trying to create this staffing grit to make my admin work easier at work. 'days' contains a week.

days = [M, T, W, Th, F]

days = [0, 1, 1, 1, 1] means s/he works everyday except for Mondays.

If the value is 2, that means they work a special shift.

S/he works from start_time to end_time - e.g. wakana works 0600-1400 everyday. S/he works from special_start to special_end on days the value is 2, e.g. eleonor works 0700-1900 Monday and Friday, and 0700-1500 on Wednesday.

I got Monday down, but I know there is a better way, perhaps using function, to print all days. I have been playing around forever with it now, but I cannot figure it out. Thank you in advance! I have so much respect for all of you experts!

staffing_data = [
        {'name': 'wakana',
        'start_time': 6,
        'end_time': 14,
        'days': [1, 1, 1, 1, 1],
        'special_start': None,
        'special_end': None},

        {'name': 'kate',
        'start_time': 11,
        'end_time': 21,
        'days': [0, 1, 1, 1, 1],
        'special_start': None,
        'special_end': None},

        {'name': 'eleonor',
        'start_time': 7,
        'end_time': 19,
        'days': [1, 0, 2, 0, 1],
        'special_start': 7,
        'special_end': 15}]

at_7 = 0
at_11 = 0
at_15 = 0
at_19 = 0



for person in staffing_data:

    if person['start_time'] <= 7 and person['end_time'] > 7 and person['days'][0] == 1:
        at_7 += 1
    if person['start_time'] <= 11 and person['end_time'] > 11 and person['days'][0] == 1:
        at_11 += 1
    if person['start_time'] <= 15 and person['end_time'] > 15 and person['days'][0] == 1:
        at_15 += 1
    if person['start_time'] <= 19 and person['end_time'] > 19 and person['days'][0] == 1:
        at_19 += 1


print(f"{at_7} at 7")
print(f"{at_11} at 11")
print(f"{at_15} at 15")
print(f"{at_19} at 19")


#Monday Staffing
#2 at 7
#3 at 11
#1 at 15
#0 at 19
1 Answers

You just need another loop for looping the days, and store the data.

staffing_data = [
        {'name': 'wakana',
        'start_time': 6,
        'end_time': 14,
        'days': [1, 1, 1, 1, 1],
        'special_start': None,
        'special_end': None},

        {'name': 'kate',
        'start_time': 11,
        'end_time': 21,
        'days': [0, 1, 1, 1, 1],
        'special_start': None,
        'special_end': None},

        {'name': 'eleonor',
        'start_time': 7,
        'end_time': 19,
        'days': [1, 0, 2, 0, 1],
        'special_start': 7,
        'special_end': 15}]

days = ['M', 'T', 'W', 'Th', 'F']
#result = [{"at_7":0,"at_11":0,"at_15":0,"at_19":0} for _ in range(len(days))]
result = []
for _ in range(len(days)):
    result.append({"at_7":0,"at_11":0,"at_15":0,"at_19":0})
        
 
for person in staffing_data:
    
    for day in range(len(days)):
        start = 'start_time'
        end = 'end_time'
        
        if person['days'][day] == 0:
            continue
        elif person['days'][day] == 2:
            start = 'special_start'
            end = 'special_end'
            
        if person[start] <= 7 and person[end] > 7:
            result[day]["at_7"] += 1
        if person[start] <= 11 and person[end] > 11:
            result[day]["at_11"] += 1
        if person[start] <= 15 and person[end] > 15:
            result[day]["at_15"] += 1
        if person[start] <= 19 and person[end] > 19:
            result[day]["at_19"] += 1

for i in range(len(days)):
    print(days[i])
    print(f"{result[i]['at_7']} at 7")
    print(f"{result[i]['at_11']} at 11")
    print(f"{result[i]['at_15']} at 15")
    print(f"{result[i]['at_19']} at 19")
    print()
Related