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

I'm on PS8 problem 2 (Greedy Advisor) on the 2008 course. I keep getting an "index out of range" error. Has anyone done this PSET? Can anyone help me figure out what the problem is? Here is my code http://dpaste.com/797490/

OpenStudy (anonymous):

i had to convert tabs to spaces for indentation problems but after that, it ran without an indexing error. http://dpaste.com/797615/

OpenStudy (anonymous):

however, you are not getting a correct result. it would help if you put comments in your code it looks like you are going through and comparing adjacent subjects. what i did was: go through the whole list and find the best subject then save it and adjust max work go through the whole list and find the best subject that hasn't already been saved then save it and adjust max work keep doing it till it can't find another subject < max work

OpenStudy (anonymous):

yes I'm trying to do something similar. I'm finding the highest value and saving it, then repeating the process. I have managed to fix the previous problem. Basically the loop was running too many times. The reason it stopped giving you an index error was because you changed the "load subjects" function so that there was only one subject in the dictionary. Thanks for your help

OpenStudy (anonymous):

here is what i got for a maxwork of 15 http://dpaste.com/798005/

OpenStudy (anonymous):

Thanks. I will check my answers when it's running properly. As you recommended I have added some detailed comments to my code. I have managed to fix the previous problem. However when I run the code now, it runs forever. I.e. there is a problem with the while loop but I can't figure out why. It is only in the greedyAdvisor function (from line 100 down). I have included as many comments a I can. If anyone can tell me where I'm going wrong that would be great!!! Here it is: http://dpaste.com/798926/

OpenStudy (anonymous):

do you use tabs and spaces for your indentation? i keep having to fix your code that i get off dpaste. i will try to see what is wrong - but i think you may have complicated your code by checking adjacent subjects. it seems simpler to write a loop that iterates over the whole dataset using a variable to keep the 'best' item so while the loop iterates over the dataset, you compare the current item against the 'best' item. the way you wrote it might have advantages, it just isn't intuitive to me. something like this: http://dpaste.com/798950/

OpenStudy (anonymous):

lines 135, 138, 139 can be replaced by: m = subjects.keys() http://docs.python.org/library/stdtypes.html#mapping-types-dict

OpenStudy (anonymous):

Hi pelikan!I looked at your code and it was long and boring, also i think it would be better not to use 'cmp' as a variable name.My advise is firstly to sort dictionary according to Value(first divide to lists then sort) and then start picking from last(or first depending on order u made) taking into account Work constraint.Keep in mind best values chosen first if maxWork is enough.Be careful with end tests(otherwise u might run into infinite loop).Good luck!

OpenStudy (anonymous):

.. ye, you shouldn't use the name of a built in function (cmp()) as a variable lines 155 and 156 (of your original post) should be replaced by a single line: if comparator(subjects[subjects.keys()[a]], subjects[subjects.keys()[b]]): the condition for the while statement at line 141 is not working. i have added a print statement at line 142 so you can see what is happening I have also made the edits, i mentioned above http://dpaste.com/799241/

OpenStudy (anonymous):

OK I didn't know that cmp was a built in function. Thanks bwCA - I have started again using the method that you recommended in your 4th post. Once I have found the first subject with the highest value, how do I then search the list again excluding the value I have previously found?

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!