To solve this problem, we need to create a scheduling program that assigns employees to buildings for the next 5 days. The solution will prioritize buildings based on their order of importance and ensure that the required workforce is available for each building type.
Here’s how we can approach the problem:
Approach
-
Input Parsing:
- Buildings are provided as a list with their types.
- Employees are provided as a list with their types and availability.
-
Scheduling Logic:
- Iterate through the buildings in order of importance.
- For each building, check if the required workforce is available for any day of the week.
- Assign employees to the first available day where all requirements are met.
- Mark those employees as unavailable for that day.
-
Output:
- Generate a schedule for the next 5 days (Monday to Friday), listing which employees are assigned to which buildings on each day.
-
Edge Cases:
- Insufficient workforce for some buildings.
- Employees unavailable on certain days.
- Buildings requiring more workers than are available.
-
Assumptions:
- All installs are completed in one day.
- Buildings are prioritized based on their order in the input list.
- Employees work full days and cannot be split across multiple buildings on the same day.
Code Implementation
Below is the Python implementation of the schedule function:
from collections import defaultdict
def schedule(buildings, employees):
# Constants for employee types
CERTIFIED = "certified"
PENDING = "pending"
LABORER = "laborer"
# Days of the week
DAYS = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday"]
# Initialize schedule and employee availability
schedule_result = {day: [] for day in DAYS}
availability = {day: {emp["id"]: emp for emp in employees} for day in DAYS}
# Helper function to check if a building can be scheduled on a given day
def can_schedule(building, day):
req_certified = 0
req_pending_or_laborer = 0
req_any = 0
if building == "single_story":
req_certified = 1
elif building == "two_story":
req_certified = 1
req_pending_or_laborer = 1
elif building == "commercial":
req_certified = 2
req_pending_or_laborer = 2
req_any = 4
# Count available employees for the day
certified_count = sum(1 for emp in availability[day].values() if emp["type"] == CERTIFIED)
pending_or_laborer_count = sum(1 for emp in availability[day].values() if emp["type"] in [PENDING, LABORER])
any_count = len(availability[day])
return (
certified_count >= req_certified and
pending_or_laborer_count >= req_pending_or_laborer and
any_count >= req_any
)
# Helper function to assign employees to a building on a given day
def assign_employees(building, day):
req_certified = 0
req_pending_or_laborer = 0
req_any = 0
if building == "single_story":
req_certified = 1
elif building == "two_story":
req_certified = 1
req_pending_or_laborer = 1
elif building == "commercial":
req_certified = 2
req_pending_or_laborer = 2
req_any = 4
# Assign certified installers
assigned = []
for emp_id, emp in list(availability[day].items()):
if emp["type"] == CERTIFIED and req_certified > 0:
assigned.append(emp_id)
del availability[day][emp_id]
req_certified -= 1
# Assign pending or laborers
for emp_id, emp in list(availability[day].items()):
if emp["type"] in [PENDING, LABORER] and req_pending_or_laborer > 0:
assigned.append(emp_id)
del availability[day][emp_id]
req_pending_or_laborer -= 1
# Assign any remaining workers
for emp_id, emp in list(availability[day].items()):
if req_any > 0:
assigned.append(emp_id)
del availability[day][emp_id]
req_any -= 1
# Add to schedule
schedule_result[day].append((building, assigned))
# Schedule buildings
for building in buildings:
scheduled = False
for day in DAYS:
if can_schedule(building, day):
assign_employees(building, day)
scheduled = True
break
if not scheduled:
print(f"Could not schedule building: {building}")
return schedule_result
# Example Usage
buildings = ["single_story", "two_story", "commercial"]
employees = [
{"id": 1, "type": "certified"},
{"id": 2, "type": "certified"},
{"id": 3, "type": "pending"},
{"id": 4, "type": "laborer"},
{"id": 5, "type": "laborer"},
{"id": 6, "type": "laborer"},
]
result = schedule(buildings, employees)
for day, assignments in result.items():
print(f"{day}: {assignments}")