Here is my solution for question 2 of problem set 3 . I have done it recursively..please check.. http://dpaste.com/hold/565399/
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
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/
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 :(
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/
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.
i have done the number of instances one too.. that was in question no 1 http://dpaste.com/hold/565457/ here it is
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
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:)
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 :)
If you add the 3rd argument like bwCA stated, you can still call the function the way that the problem set requires
Oh sorry i kind of did not paid attention..thanks :)
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
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.
Ah i see ...Thanks again :)
Join our real-time social learning platform and learn together with your friends!