Simplify code by dropping support for legacy Python (#1143)

* Simplify code by dropping support for legacy Python

* sort() --> sorted()
This commit is contained in:
Christian Clauss
2019-08-19 15:37:49 +02:00
committed by GitHub
parent 32aa7ff081
commit 47a9ea2b0b
145 changed files with 367 additions and 976 deletions

View File

@ -9,27 +9,26 @@ Find the total no of ways in which the tasks can be distributed.
"""
from __future__ import print_function
from collections import defaultdict
class AssignmentUsingBitmask:
def __init__(self,task_performed,total):
self.total_tasks = total #total no of tasks (N)
# DP table will have a dimension of (2^M)*N
# initially all values are set to -1
self.dp = [[-1 for i in range(total+1)] for j in range(2**len(task_performed))]
self.task = defaultdict(list) #stores the list of persons for each task
#finalmask is used to check if all persons are included by setting all bits to 1
self.finalmask = (1<<len(task_performed)) - 1
def CountWaysUtil(self,mask,taskno):
# if mask == self.finalmask all persons are distributed tasks, return 1
if mask == self.finalmask:
return 1
@ -48,18 +47,18 @@ class AssignmentUsingBitmask:
# now assign the tasks one by one to all possible persons and recursively assign for the remaining tasks.
if taskno in self.task:
for p in self.task[taskno]:
# if p is already given a task
if mask & (1<<p):
continue
# assign this task to p and change the mask value. And recursively assign tasks with the new mask value.
total_ways_util+=self.CountWaysUtil(mask|(1<<p),taskno+1)
# save the value.
self.dp[mask][taskno] = total_ways_util
return self.dp[mask][taskno]
return self.dp[mask][taskno]
def countNoOfWays(self,task_performed):
@ -67,17 +66,17 @@ class AssignmentUsingBitmask:
for i in range(len(task_performed)):
for j in task_performed[i]:
self.task[j].append(i)
# call the function to fill the DP table, final answer is stored in dp[0][1]
return self.CountWaysUtil(0,1)
if __name__ == '__main__':
total_tasks = 5 #total no of tasks (the value of N)
#the list of tasks that can be done by M persons.
task_performed = [
task_performed = [
[ 1 , 3 , 4 ],
[ 1 , 2 , 5 ],
[ 3 , 4 ]