-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(arrays): optimal task assignment
- Loading branch information
1 parent
03e848f
commit fcc5584
Showing
3 changed files
with
40 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# Optimal Task Assignment | ||
|
||
Assign tasks to workers so that the time it takes to complete all the tasks is minimized given a count of workers and an | ||
array where each element indicates the duration of a task. | ||
|
||
We wish to determine the optimal way in which to assign tasks to some workers. Each worker must work on exactly two | ||
tasks. Tasks are independent of each other, and each task takes a certain amount of time. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
from typing import List, Tuple | ||
|
||
|
||
def optimal_task_assignment(tasks: List[int]) -> List[Tuple[int, int]]: | ||
""" | ||
After sorting the array, the for loop iterates for half the length of the array adds the pairs using indexing to a | ||
list. So ~x is the bitwise complement operator which puts a negative sign in front of x and subtracts 1 from it. | ||
Thus, the negative numbers as indexes mean that you count from the right of the array instead of the left. So, | ||
numbers[-1] refers to the last element, numbers[-2] is the second-last, and so on. In this way, we are able to pair | ||
the numbers from the beginning of the array to the end of the array. | ||
""" | ||
sorted_tasks = sorted(tasks) | ||
result: List[Tuple[int, int]] = [] | ||
|
||
for x in range(len(sorted_tasks) // 2): | ||
result.append((sorted_tasks[x], sorted_tasks[~x])) | ||
|
||
return result |
15 changes: 15 additions & 0 deletions
15
algorithms/arrays/optimal_task_assignment/test_optimal_task_assignment.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import unittest | ||
from . import optimal_task_assignment | ||
|
||
|
||
class OptimalTaskAssignmentTestCase(unittest.TestCase): | ||
def test_1(self): | ||
"""Should return [(2,7), (3,6),(5,5)] from [6, 3, 2, 7, 5, 5]""" | ||
tasks = [6, 3, 2, 7, 5, 5] | ||
expected = [(2, 7), (3, 6), (5, 5)] | ||
actual = optimal_task_assignment(tasks) | ||
self.assertEqual(expected, actual) | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |