Hi, I'm working on the greedy advisor (Pb Set 8) and the pg is running. However, given a set of courses with values [1, 3, 7, 10, 4, 7, 8, 3, 1] and a maximum amount of work of 10, it selects three courses with values 7, 8 and 7 for a total work amount of 9. Why is the course with value 10 not selected ? here is the code: http://dpaste.com/hold/755287/ Thanks
put some print statements in to see what is going on
I agree with bwCA. Put print statements when debugging. Some hints: -Line 33: Should you really break if work> maxWork? Perhaps the next items can still fill the extra work. -Take note that the subjects dictionary should not be mutated
(Sorry for the earlier deleting couldn't edit my post) What I meant to say was add print type(val1) and print type(val2)....
I looked at your code. Problem was that the values were treated as string rather than ints. I've attached your modified code and it seems to be working as expected. You might want to hack at it before looking at the solution. Hope this helps.
Thanks for the replies. Kcpaas, dbwonders: I use print statements consistently for debugging . I take them out when posting though. Line 33: if I take out the break statement when going over the work limit, the pg will return solutions whose work value can go 1 course over the work limit (since the work value is incremented after the while condition is checked) Chribonn: thanks. Yes indeed that was the problem. I had partially fixed it in the greedy advisor code by adding in() constraints but clearly not sufficiently. It is strange though that the comparators given by the MIT course do not integrate the integer constraint ie returns a check on strings rather than on integers. I also do not understand why only the value 10 was blocked by this bug while the pg worked fine for all other values.
nataraja, what I meant was is the condition enough? Anyway, as for why only 10 is affected, it is because when comparing strings, you look at the first character first. ie. '7' > '10' because '7' > '1'
Thanks kcpaas. That makes perfect sense.
Join our real-time social learning platform and learn together with your friends!