Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Add] : Job Sequence program under GREEDY methods #10482

Merged
merged 23 commits into from
Oct 15, 2023
Merged
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
2320d90
to add job seq program
kosuri-indu Oct 15, 2023
3b3f3a8
to add job seq program
kosuri-indu Oct 15, 2023
83394f4
Merge branch 'add/job_seq.py' of https://github.com/kosuri-indu/Pytho…
kosuri-indu Oct 15, 2023
8bc4629
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 15, 2023
4a20527
to add definitions in parameters
kosuri-indu Oct 15, 2023
9663d5d
Merge branch 'add/job_seq.py' of https://github.com/kosuri-indu/Pytho…
kosuri-indu Oct 15, 2023
f6c1223
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 15, 2023
3aacb09
to add definitions in parameters
kosuri-indu Oct 15, 2023
4f30ae4
Merge branch 'add/job_seq.py' of https://github.com/kosuri-indu/Pytho…
kosuri-indu Oct 15, 2023
7fe25bf
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 15, 2023
c220965
to add definitions in parameters
kosuri-indu Oct 15, 2023
349c324
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 15, 2023
58856c9
changes as recommended
kosuri-indu Oct 15, 2023
a036fd6
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 15, 2023
688fd27
type hint error resolved
kosuri-indu Oct 15, 2023
0a651e4
Merge branch 'add/job_seq.py' of https://github.com/kosuri-indu/Pytho…
kosuri-indu Oct 15, 2023
37ad111
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 15, 2023
82877b3
removed lambda
kosuri-indu Oct 15, 2023
4697ade
Merge branch 'add/job_seq.py' of https://github.com/kosuri-indu/Pytho…
kosuri-indu Oct 15, 2023
f2ed330
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 15, 2023
148a08d
import stmts order
kosuri-indu Oct 15, 2023
c261d83
Merge branch 'add/job_seq.py' of https://github.com/kosuri-indu/Pytho…
kosuri-indu Oct 15, 2023
20f0dd0
Update and rename job_sequence.py to job_sequence_with_deadline.py
cclauss Oct 15, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions scheduling/job_sequence_with_deadline.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
"""
Given a list of tasks, each with a deadline and reward, calculate which tasks can be
completed to yield the maximum reward. Each task takes one unit of time to complete,
and we can only work on one task at a time. Once a task has passed its deadline, it
can no longer be scheduled.

Example :
tasks_info = [(4, 20), (1, 10), (1, 40), (1, 30)]
max_tasks will return (2, [2, 0]) -
Scheduling these tasks would result in a reward of 40 + 20

This problem can be solved using the concept of "GREEDY ALGORITHM".
Time Complexity - O(n log n)
https://medium.com/@nihardudhat2000/job-sequencing-with-deadline-17ddbb5890b5
"""
from dataclasses import dataclass
from operator import attrgetter


@dataclass
class Task:
task_id: int
deadline: int
reward: int


def max_tasks(tasks_info: list[tuple[int, int]]) -> list[int]:
"""
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, 2]
>>> max_tasks([(9, 10)])
[0]
>>> max_tasks([(-9, 10)])
[]
>>> max_tasks([])
[]
>>> max_tasks([(0, 10), (0, 20), (0, 30), (0, 40)])
[]
>>> max_tasks([(-1, 10), (-2, 20), (-3, 30), (-4, 40)])
[]
"""
tasks = sorted(
(
Task(task_id, deadline, reward)
for task_id, (deadline, reward) in enumerate(tasks_info)
),
key=attrgetter("reward"),
reverse=True,
)
return [task.task_id for i, task in enumerate(tasks, start=1) if task.deadline >= i]


if __name__ == "__main__":
import doctest

doctest.testmod()
print(f"{max_tasks([(4, 20), (1, 10), (1, 40), (1, 30)]) = }")
print(f"{max_tasks([(1, 10), (2, 20), (3, 30), (2, 40)]) = }")