Advertisement
Guest User

Untitled

a guest
Aug 17th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.40 KB | None | 0 0
  1. import heapq
  2.  
  3. class Job:
  4. def __init__(self,name,deadline,profit):
  5. self.name = name
  6. self.deadline = deadline
  7. self.profit = profit
  8. #based on max profit it will be sorted.
  9. def __lt__(self,other):
  10. selfPriority = self.profit
  11. otherPriority = other.profit
  12. return selfPriority > otherPriority
  13.  
  14. class JobSequence:
  15.  
  16. def __init__(self):
  17. self.jobArray = []
  18.  
  19.  
  20. def insert(self,name,deadline,profit):
  21. self.jobArray.append(Job(name,deadline,profit))
  22.  
  23. def algorithm(self):
  24. q = []
  25. jobSlot = []
  26. slot = 0
  27.  
  28. for x in self.jobArray:
  29. heapq.heappush(q,x);
  30. if(slot>x.deadline):
  31. slot = slot
  32. else:
  33. slot = x.deadline
  34. jobSlot = [0]*slot
  35.  
  36. while len(q)>0:
  37. node = heapq.heappop(q)
  38.  
  39.  
  40. print(node.name+" "+str(node.deadline)+" "+str(node.profit));
  41.  
  42.  
  43.  
  44. for i in range(0,slot):
  45. print(jobSlot[i]);
  46.  
  47.  
  48.  
  49.  
  50. #print(node.name+" deadline: "+str(node.deadline)+", profit: "+str(node.profit))
  51.  
  52.  
  53. job = JobSequence()
  54. job.insert("J1",2,10);
  55. job.insert("J2",3,25);
  56. job.insert("J3",4,30);
  57. job.insert("J4",1,20);
  58.  
  59. job.algorithm();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement