PS9_SC2011 part 2 greedyAdvisor. I really do not understand the process the ps9_test went through to get its answers. with smallCatalog = {'6.00': (16, 8), '1.00': (7, 7), '15.01': (9, 6), '6.01': (5, 3)} Trying greedyAdvisor(smallCatalog, 15, cmpWork)... ERROR in greedyAdvisor: For greedyAdvisor(smallCatalog, 15, ps9.cmpWork), expected {'15.01': (9, 6), '6.01': (5, 3)} but was actually {'15.01': (9, 6), '1.00': (7, 7)} http://pastebin.com/rGgHvwk9
Looking at your error code it could be a typo. Did you verify that you have '6.01' and not '1.00' in your list?
I went about it the wrong way, using keyFcn's. ended up looking at someone else's code. they made a sorting algorithm based on the comparator function. don't really understand how that would work
Greedy just picks the most valuable 'object' then the next most valuable .... until the constraint (work) is met. You could turn the dictionary of {subject:(value, work)} items into a list of of tuples [(subject, value, work)] then sort the list on the value item of the tuples then just start taking items till work is reached or will be exceeded
looked at your code and see that you tried to do that (sort the list). Couple of things (assuming Python v2.7ish - not 3.x): - lines 35, 37, 39: the sorted() function returns a sorted list but you do not assign that list to anything so those lines don't 'do' anything. If you want to sort the list in place use the list sort() method subject_list.sort(......) this may be your main problem - i didn't completely walk thru the while loop because i got confused without any comments; especially line 56, why are you sending things thru the comparator function after you already used it to sort the list? assuming the list is (reverse) sorted you should be able to simplify your while loop to something like this http://dpaste.com/1274569/ (i didn't test that) http://docs.python.org/2.7/howto/sorting.html
Join our real-time social learning platform and learn together with your friends!