PS8#2: Greedy advisor. I've implemented a greedy advisor program. However, it's more brute force than elegance. I think the intuitive approach would be to do pre-processing and create sorted lists of tuples (where the tuple is (subject string, value/work/ratio). Then the program would simply read down the list. However, it seems that's not the approach the professor's wanted us to use as they specified that the function should take the comparator as an argument. Here's my implementation http://codepad.org/AjXZL6jD Did anyone come up with a simpler approach?
to understand how greedyAdvisor works, getDictMax runs through the entire dictionary each time it's called and returns the maximum value using the comparator. It's pretty simply, it cycles through the dictionary, puts the dictionary entry into the comparator, and saves it if value the comparator says it great. http://codepad.org/g9HD6B3J
i just went thru the dictionary and picked the best subject, amended maxWork then did it again and again ... til it was done here is mine with a small subject dictionary and a test function http://pastebin.com/HdZ9ssQM
bwCA , my solution is similar to yours but I feel there is a better solution that just seeing if the given comparator is cmpWork , but I still can't find it :( http://pastebin.com/NbmEMHLn
To do this preprocessing, you would have to sort the whole subjects dictionary, right ? Complexity is O(n log n) in the best case, I think. With the solution proposed in the assignment, you just have to go through the whole list k times, k being the number of selected subjects, which is very small. Complexity is roughly O(kn). So the second solution is better when k is << n, which is the case.
Join our real-time social learning platform and learn together with your friends!