Job sequencing
Mon Nov 18 2024 13:40:02 GMT+0000 (Coordinated Universal Time)
Saved by
@badram123
class Job:
def __init__(self, id, profit, deadline):
self.id = id
self.profit = profit
self.deadline = deadline
def job_sequencing(jobs, n):
jobs.sort(key=lambda x: x.profit, reverse=True)
result = [None] * n
slot = [False] * n
total_profit = 0
for job in jobs:
for j in range(min(n, job.deadline) - 1, -1, -1):
if not slot[j]:
result[j] = job.id
slot[j] = True
total_profit += job.profit
break
print("Job sequence:", result)
print("Total profit:", total_profit)
if __name__ == "__main__":
n = int(input("Enter number of jobs: "))
jobs = []
for _ in range(n):
id, profit, deadline = map(int, input("Enter id, profit, deadline: ").split())
jobs.append(Job(id, profit, deadline))
job_sequencing(jobs, n)
content_copyCOPY
Comments