This week is the big post about recursion. I know I've posted about recursion before but this post is going to sum[everything about recursion] (hehehe get it?)
Recursion, in computer science, is a method where the solution of a problem consists of the solution of smaller problems which are subsets of the whole problem. For our convenience, it's basically calling a function on an object repeatedly until a base case is reached.
Then we will be able to determine the solution of the main problem from the solutions of the base cases. Most of the time, we would use recursion on things that are nested within itself, especially data structures, and Trees are a prime example of this. (See week 6 post for full Tree explanation).
This week, we encountered recursion in the labs through LinkedLists. A LinkedList is pretty much a tree with a maximum of one child (think the Chinese 1 child rule). The end of the list is reached when a node has no children. The problem of the labs this week was a stack taking the form of LinkedLists. Similar to week 2, I had trouble with this because I couldn't figure out how to keep the pointer on the back of the list with the enqueue function.
CONFESSIONS OF A COMPSCI GIRL: I couldn't figure out how to do enqueue this week and my partner and I tried so many different things and it wouldn't work. WE EVEN TRIED INDEXING THE LINKED LIST WHY DID WE DO THAT? Lesson 2 in Trees: don't index them. OMG don't do it please please please or else you'll face the wrath of the Computer Science gods (aka the Python shell) and will unleash the utmost horrifying consequence on you (EXCEPTION! OH GOD NOT THE EXCEPTION)
So in the end the kind TA (Abdi you're the best) explained everything to us and VOILA (I feel like I cheated though because I couldn't figure it out myself T___T) but basically we set a variable to the last empty "node" of the LinkedList, and just prepend to that. REMEMBER THIS, I feel like this will help with recursion and data structures for like the rest of my life.
TATA FOR NOW FRIENDS. See you all next week :) !
Before Danny posted answers to the labs before term test two, I actually did the insert, delete etc. functions in linked lists all using indexing! What problems were you having when you tried this?
ReplyDeleteHow were you able to index them? Did you recursively iterate it the number of times you "indexed" it? Or did you literally just make it into a flat list and index it then turned it back into a linked list?
DeleteI can't remember exactly what kind of errors we had now since it's been more than a month since we did it. I just remember it raising some kind of error when we tried prepending to the end of the list for the stack (which was what I think we were supposed to do) I don't think decapitating really affected, though.
No I didn't do either. My method is below, while kinda verbose and ugly compared to danny's version is actually somewhat simple. If empty or invalid index, raise error like
ReplyDeletein the actual index method that was implemented. Else if the index the user inputs is zero simply decapitate(since decapitate deletes the head or index 0)
Otherwise this is where it gets messy...
self[ind] is there simply to confirm the existence of that index AND refers to a specific value we want gone, otherwise return an error
then the variable current is set to the 'rest' of the linked list. The while loop will continuously set current to the 'rest' until the current.item is equal to that self[ind] i mentioned earlier. Once the loop breaks we know we have the right section of the linked list. We can thus decapitate from there
if self.empty or ind < 0:
raise IndexError
if ind == 0:
self.decapitate()
else:
self[ind]
current = self.rest
while current.item != self[ind]:
current = current.rest
current.decapitate()
Also Note when I copy and pasted, the indents deleted -_-
DeleteWould your code work if say the item at ind == 2 is 7 and the item at ind == 5 is 7 too?
DeleteYa that's the one problem, it would only work on lists with unique values. I don't think any of the lab examples had any repeating values so I didn't change it. This could be solved by adding an accumulator either in the body or in a helper function, but then things gets kinda messy...
ReplyDeleteYeah LOL. But thanks for your insight! :)
Delete