Advertisement
Guest User

PS8#2

a guest
Feb 7th, 2012
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.15 KB | None | 0 0
  1. def greedyAdvisor(subjects, maxWork, comparator):
  2.     """
  3.    Returns a dictionary mapping subject name to (value, work) which includes
  4.    subjects selected by the algorithm, such that the total work of subjects in
  5.    the dictionary is not greater than maxWork.  The subjects are chosen using
  6.    a greedy algorithm.  The subjects dictionary should not be mutated.
  7.  
  8.    subjects: dictionary mapping subject name to (value, work)
  9.    maxWork: int >= 0
  10.    comparator: function taking two tuples and returning a bool
  11.    returns: dictionary mapping subject name to (value, work)
  12.    """
  13.     dic = {}
  14.     while True:
  15.         best = (0,1)
  16.         bestsubject = ''
  17.         for i in subjects:
  18.             if comparator(subjects[i],best) == False :
  19.                 best = (1000,1000)
  20.             break;
  21.         for i in subjects:
  22.             if comparator(subjects[i],best) and not i in dic:
  23.                 best = subjects[i]
  24.                 bestsubject = i
  25.         if bestsubject == '':
  26.             return dic
  27.         maxWork = maxWork - subjects[bestsubject][WORK]
  28.         if maxWork < 0:
  29.             return dic
  30.         dic[bestsubject] = best
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement