Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def greedyAdvisor(subjects, maxWork, comparator):
- """
- Returns a dictionary mapping subject name to (value, work) which includes
- subjects selected by the algorithm, such that the total work of subjects in
- the dictionary is not greater than maxWork. The subjects are chosen using
- a greedy algorithm. The subjects dictionary should not be mutated.
- subjects: dictionary mapping subject name to (value, work)
- maxWork: int >= 0
- comparator: function taking two tuples and returning a bool
- returns: dictionary mapping subject name to (value, work)
- """
- dic = {}
- while True:
- best = (0,1)
- bestsubject = ''
- for i in subjects:
- if comparator(subjects[i],best) == False :
- best = (1000,1000)
- break;
- for i in subjects:
- if comparator(subjects[i],best) and not i in dic:
- best = subjects[i]
- bestsubject = i
- if bestsubject == '':
- return dic
- maxWork = maxWork - subjects[bestsubject][WORK]
- if maxWork < 0:
- return dic
- dic[bestsubject] = best
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement