Saturday, 25 April 2015

container vs recursion

I'm finding that in some of the past exam questions, they specifically request NOT to use container objects. In their hint... it says use recursion...
So the following is a rev(q) that reverses the items in queue q using std queue functions and NOT usnig other container objects. FYI, I made enqueue and dequeue return the queue object after modifying. This just allows stuff like q.enqueue().enqueue().dequeue()... stuff like that. Anyhow, using this example I want to talk about what I think about this kind of method.
I'm not sure what the point of this question is because surely I don't use container objects but in the process of using recursion I create new name spaces and in essence I create variables that store things. The name spaces are like container objects. the next question is more interesting. can rev(s) be defined for stack s with similar restrictions? I'm not sure. If you can use loops and variables you can definitely hack this to work. It is possible to do something like exec("a" + str(i) + "=...") to make an "array of variables". I remember when I first learned programming and got to for loops I was thinking it would be awesome if I can autogenerate series of variables which conceptually was somewhat like arrays. Anyways... using recursion like above it's not possible. This is interesting because it kind of hints at how queues and stacks are complementary. With queues, you need a stack structure to reverse and with stacks you need a queue structure. It queues work with recursion because the namespace/namespace recursive calling forms a stack.

No comments:

Post a Comment