MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / max_tasks

Function max_tasks

scheduling/job_sequence_with_deadline.py:28–55  ·  view source on GitHub ↗

Create a list of Task objects that are sorted so the highest rewards come first. Return a list of those task ids that can be completed before i becomes too high. >>> max_tasks([(4, 20), (1, 10), (1, 40), (1, 30)]) [2, 0] >>> max_tasks([(1, 10), (2, 20), (3, 30), (2, 40)]) [3

(tasks_info: list[tuple[int, int]])

Source from the content-addressed store, hash-verified

26
27
28def max_tasks(tasks_info: list[tuple[int, int]]) -> list[int]:
29 """
30 Create a list of Task objects that are sorted so the highest rewards come first.
31 Return a list of those task ids that can be completed before i becomes too high.
32 >>> max_tasks([(4, 20), (1, 10), (1, 40), (1, 30)])
33 [2, 0]
34 >>> max_tasks([(1, 10), (2, 20), (3, 30), (2, 40)])
35 [3, 2]
36 >>> max_tasks([(9, 10)])
37 [0]
38 >>> max_tasks([(-9, 10)])
39 []
40 >>> max_tasks([])
41 []
42 >>> max_tasks([(0, 10), (0, 20), (0, 30), (0, 40)])
43 []
44 >>> max_tasks([(-1, 10), (-2, 20), (-3, 30), (-4, 40)])
45 []
46 """
47 tasks = sorted(
48 (
49 Task(task_id, deadline, reward)
50 for task_id, (deadline, reward) in enumerate(tasks_info)
51 ),
52 key=attrgetter("reward"),
53 reverse=True,
54 )
55 return [task.task_id for i, task in enumerate(tasks, start=1) if task.deadline >= i]
56
57
58if __name__ == "__main__":

Callers 1

Calls 1

TaskClass · 0.85

Tested by

no test coverage detected