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

I really don't understand why this returns None on my pc. This is for the brute force in ps9 http://dpaste.com/800582/ I print and assert the result value to verify but it shows None when returned. What's the matter?

OpenStudy (anonymous):

the conditional at line 5 is the base case for your recursive function - it will return result to the function that called it, unfortunately the function that calledit cannot return anything to the function that called it .... when the recursive function is executing, every time it gets to line 19 its execution is suspended: it calls itself and waits till the new function stops executing - when the new function stops executing, the original doesn't have any more actions to take so it just vanishes without a trace. you need to pass the result all the way back up to the original function http://dpaste.com/801253/

OpenStudy (anonymous):

recursion takes a bit to get your head around - but when you do, aha! http://en.wikipedia.org/wiki/Recursion_(computer_science)

OpenStudy (anonymous):

Ok i mostly get it. I modified it so the else portion returned something. Thanks a lot.

OpenStudy (anonymous):

http://dpaste.com/801396/

OpenStudy (anonymous):

... nice function, but it looks like you are generating combinations not permutations. http://dpaste.com/801402/

OpenStudy (anonymous):

Thanks. Yes my math isn't strong so the terms are wacky. I looked up a permutations algorithm and modified it as best i could to get unique combos. It was very confusing for me. That's why it doesn't make sense because I borrowed terms from the original algorithm. Your code illustration is very nice. I saved it for reference.

OpenStudy (anonymous):

now that you have written your own combination generator, you might want to google powerset algorithms - you might find it interesting. also, Python (v2.6+) has a module in the standard library with a combination and permutation generators. :) http://dpaste.com/801413/ http://docs.python.org/library/itertools.html

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!