Ask your own question, for FREE!
MIT 6.00 Intro Computer Science (OCW) 7 Online
OpenStudy (anonymous):

Does anyone know how to do the greedyAdvisor part for PS 8?

OpenStudy (anonymous):

Here is my code so far 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) """ greedySchedule = {} currentWork = 0 nameList = [] workValueList = [] for name, workValue in subjects.items(): nameList.append(name) workValueList.append(workValue) while currentWork <= maxWork: for i in range(len(workValueList) - 2): j = i + 1 for j in range(len(workValueList) - 1): if comparator(workValueList[i], workValueList[j]): bestKey = nameList[i] bestTuple = workValueList[i] currentWork += workValueList[i][WORK] jWasPicked = False else: bestKey = nameList[j] bestTuple = workValueList[j] currentWork += workValueList[j][WORK] jWasPicked = True if currentWork > maxWork: break if jWasPicked: break if currentWork > maxWork: break greedySchedule[bestKey] = bestTuple return greedySchedule

OpenStudy (hari5719):

i guess thats right

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!