Despite the dawn of Assignment 2, I want to spend this post talking about linked lists, and some other stuff.
I've had some experience with linked lists in high school - but it was at a pretty basic level. Making a list out of nodes, traversing it, finding items, adding, inserting removing, etc - basic stuff. However we also got to deal with some other interesting things. For example, what if each list node contained not only a pointer to its next node, but also to its previous? This would be a Doubly Linked List. Something like this could be visualized practically in something like a web-browser - every page gets added to the end of a list and you can go back and forward in the list, one element at a time. Another type of linked list is a Circular list (I like to call them Circly lists, you'll see why in a bit) - and its feature is that the last element points back to the first. Now what if you take a doubly linked list and link the head to the tail and vice-versa? You get a Circly-Doubly-Linked-List! As comedic as the name seems something like this is quite applicable. Lets take a music playlist as an example. Each song is a node and is doubly linked to its neighbors (so you can skip forwards or backwards). Now what happens when the user turns on "Repeat Playlist"? The first and last nodes get linked, and so the list becomes a Circly-Doubly-Linked-List.
On a totally unrelated note...
Now remember that other thing I wanted to talk about? Well here it is. In High school we got a quite interesting recursion assignment and I thought I would post it here for those who want to take a shot at it - the comical nature of the whole thing combined with the decent challenge makes it a good load of fun. I also think its kinda up the same alley as A2 (just much simpler of course).
Monkey Words
Your goal is to make a program (in any language of your choice, I did it in C#, you can use python) that will determine if an input word is a monkey word (a word from a monkey's vocabulary).
A Monkey Word can be one of two things:
-It can be an A-Word
-Or it can be an A-Word followed by "N" followed by another Monkey Word.
An A-Word can be one of two things:
-It can be just the letter "A"
-Or it can the letter "B" followed by a Monkey Word followed by "S"
Here are some examples of valid words:
BANAS
BANANAS
BASNANA
BASNANANBAS
BANANANANBANASS
BBBANANASNANASNANASNA
Here is a link to two text files you can test with (each line is one word) as well as my version of the program (so you can test your own words and profile [time] your algorithm).
All the words in False1.txt are not valid Monkey Words, and all the words in True1.txt are valid words.
My algorithm is a total of two methods and 44 lines (including whitespace and documentation).
Let me know what you think of the problem! Was it easy? Hard? If anyone is interested I can post my solution next week.
Well this was quite the post. I hope you enjoyed reading it, and please comment and let me know what you think. Until next week!
This is a really informative post! I like the example you used to show how Circly-Doubly-Linked lists are useful. Just a question: do you know if a circly linked list would be more time complex than say a linked list or doubly linked list (big O notation)?
ReplyDeleteThanks for the exercise by the way. Mind posting the solution next week?
Depends on what you're trying to accomplish, a circly list could be more efficient since you don't meet to store the head, you just need to store the current node, and move relatively to it.
DeleteInteresting post; I liked your Monkey Word excersize
ReplyDelete