Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 6f18bf6

Browse files
committedAug 2, 2024·
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
1 parent e147166 commit 6f18bf6

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed
 

‎dynamic_programming/bitmask.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,15 @@
1010

1111
from collections import defaultdict
1212

13+
1314
class AssignmentUsingBitmask:
1415
def __init__(self, task_performed, total):
1516
"""
1617
Initialize the AssignmentUsingBitmask class.
1718
1819
:param task_performed: A list of lists where each sublist contains the tasks that a person can perform.
1920
:param total: Total number of tasks.
20-
21+
2122
>>> a = AssignmentUsingBitmask([[1, 3, 4], [1, 2, 5], [3, 4]], 5)
2223
>>> a.total_tasks
2324
5
@@ -28,7 +29,9 @@ def __init__(self, task_performed, total):
2829

2930
# DP table will have a dimension of (2^M) x (N+1)
3031
# initially all values are set to -1
31-
self.dp = [[-1 for _ in range(total + 1)] for _ in range(2 ** len(task_performed))]
32+
self.dp = [
33+
[-1 for _ in range(total + 1)] for _ in range(2 ** len(task_performed))
34+
]
3235

3336
self.task = defaultdict(list) # stores the list of persons for each task
3437

@@ -42,7 +45,7 @@ def count_ways_until(self, mask, task_no):
4245
:param mask: Current mask representing which persons have been assigned tasks.
4346
:param task_no: The current task number being considered.
4447
:return: The number of ways to assign tasks starting from the current state.
45-
48+
4649
>>> a = AssignmentUsingBitmask([[1, 3, 4], [1, 2, 5], [3, 4]], 5)
4750
>>> a.count_ways_until(0, 1)
4851
0
@@ -85,7 +88,7 @@ def count_no_of_ways(self, task_performed):
8588
8689
:param task_performed: A list of lists where each sublist contains the tasks that a person can perform.
8790
:return: The total number of ways to distribute tasks.
88-
91+
8992
>>> a = AssignmentUsingBitmask([[1, 3, 4], [1, 2, 5], [3, 4]], 5)
9093
>>> a.count_no_of_ways([[1, 3, 4], [1, 2, 5], [3, 4]])
9194
10
@@ -98,6 +101,7 @@ def count_no_of_ways(self, task_performed):
98101
# Call the function to fill the DP table, final answer is stored in dp[0][1]
99102
return self.count_ways_until(0, 1)
100103

104+
101105
if __name__ == "__main__":
102106
total_tasks = 5 # Total number of tasks (the value of N)
103107

0 commit comments

Comments
 (0)
Please sign in to comment.