def greedyAdvisor(subjects, maxWork, comparator):
'''subjects is a dictionary with keys classes and values of tuples of class value and class work. maxWork is the maximum work a student wants to put in. comparator is a function that takes two tuples of the aforementioned class value/class work variety and returns which one has a higher value. The function returns a dictionary with the most valuable classes to take within the given parameter of the willingness to work. I am supposed to use a greedy algorithem'''
greedy_dict = {}
highest_subjects = []
total_work = 0
while total_work < maxWork:
highest_value = 0, 0
highest_class = 0.0
for i in subjects:
if comparator (subjects[i], highest_value) and i not in highest_subjects and total_work + int(subjects[i][WORK]) <= maxWork:
highest_value = subjects[i]
highest_class = i
print highest_class, highest_value
highest_subjects.append(highest_class)
total_work += int(highest_value[WORK])
greedy_dict[highest_class] = highest_value
print greedy_dict
return greedy_dict
the data, subjects is a dictionary which maps courses like 6.00, 7.01, etc to tuples with values 1-10 for the value of the class and workload 1-20 for how many hours a problem is supposed to take. Well it started in a text file and I turned it into a dictionary just dandy. The problem is from mit ocw intro to programming problem 8, the text is in a file called subjects.txt. I hope this addressed your concerns about the data.
The problem I’m having is the subjects dictionary has class values up to 10, but the greedy_dictionary keeps thinking the maximum value is 9. The comparator in the parameters is a function which returns True if the first tuple[VALUE] is greater than the second tuple[VALUE].
It is… if you’re comparing strings.
Try converting them to numbers first.