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

Here is my solution for question 2 of problem set 3 . I have done it recursively..please check.. http://dpaste.com/hold/565399/

OpenStudy (anonymous):

Question 3 done too..but i am not sure about correctness..cant really figure out how to check.. http://dpaste.com/hold/565446/ please check

OpenStudy (anonymous):

it's recursive and it works!! that is the first time i have seen a function defined in another function. I imagine that is why you had to add line 10 - to call the inner function. did you do that so that you could 'persist' posn? you can do the same thing by adding posn to subSME's argument list and giving it a default value - http://dpaste.com/565447/

OpenStudy (anonymous):

actually...the format of input for the subSME function is already given in the question..and it has only 2 inputs..so i defined another function inside it..it is only my second recursive code..so i was experimenting a bit..and it actually worked :).. and yes i had to add posn for the second function..but i have no idea what u mean by 'persisting' posn..sorry i am kind of weak in english :(

OpenStudy (anonymous):

your solution was very nice and much better than mine when I did the problem... but I think it asked for a count of instances of key in target rather than locations. and, it is kind of cheating to add a third argument "pos" (I had to do that too when I did it). One way to address the count issue in a recursive function is with global variable (I don't think they have come up yet) http://dpaste.com/565454/

OpenStudy (anonymous):

also, rather than moving position you could cut down target before sending it back through loop...that's what I did in example, sorry, should have commented it.

OpenStudy (anonymous):

i have done the number of instances one too.. that was in question no 1 http://dpaste.com/hold/565457/ here it is

OpenStudy (anonymous):

and about adding the 3rd argument..i did not add it to the main function..i added it to a function inside it.. so my function is still in the format they want it to be.. so it is ok ..i think coz..as long as u don't get caught..u committed no crime :D

OpenStudy (anonymous):

ah yes, you are right. so you could set the global variable to an empty tuple and add from there... and your function inside a function was pretty clever:)

OpenStudy (anonymous):

Thanks for the global variable thing..i was actually in a damn need of a thing like that..that is why i had to create a function inside another :)

OpenStudy (anonymous):

If you add the 3rd argument like bwCA stated, you can still call the function the way that the problem set requires

OpenStudy (anonymous):

Oh sorry i kind of did not paid attention..thanks :)

OpenStudy (anonymous):

now i see ..why study groups are so useful..i now have like 3 or 4 types of methods to do the thing which i once thought as impossible :P

OpenStudy (anonymous):

by persist, i meant saving/passing the value of posn from one recursion to the next - you don't want to lose that information, you need to persist it.

OpenStudy (anonymous):

Ah i see ...Thanks again :)

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!