Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import heapq
- class Job:
- def __init__(self,name,deadline,profit):
- self.name = name
- self.deadline = deadline
- self.profit = profit
- #based on max profit it will be sorted.
- def __lt__(self,other):
- selfPriority = self.profit
- otherPriority = other.profit
- return selfPriority > otherPriority
- class JobSequence:
- def __init__(self):
- self.jobArray = []
- def insert(self,name,deadline,profit):
- self.jobArray.append(Job(name,deadline,profit))
- def algorithm(self):
- q = []
- jobSlot = []
- slot = 0
- for x in self.jobArray:
- heapq.heappush(q,x);
- if(slot>x.deadline):
- slot = slot
- else:
- slot = x.deadline
- jobSlot = [0]*slot
- while len(q)>0:
- node = heapq.heappop(q)
- print(node.name+" "+str(node.deadline)+" "+str(node.profit));
- for i in range(0,slot):
- print(jobSlot[i]);
- #print(node.name+" deadline: "+str(node.deadline)+", profit: "+str(node.profit))
- job = JobSequence()
- job.insert("J1",2,10);
- job.insert("J2",3,25);
- job.insert("J3",4,30);
- job.insert("J4",1,20);
- job.algorithm();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement