The Tower of Babel, no Honoi, maybe


As a kid does any one remember having a wooden toy (has to be older guys, no one can afford to mass manufacture wooden toys anymore these days, everything is Plastic) Where there were rings of different sizes and one to three poles (and these days that would be deemed dangerous as a kid can gauge their eye out with it), the idea being to sort the rings by their size for smaller kids and for others a puzzle to move the rings from pole A to Pole C, where as Pole B can be used as a temporary space.

What does this have anything to do with development? One it is an idea for a puzzle game that some of you can try to make for practicing your developmental skills as it does involve a few things, like touches, movement, etc.

The second thing that this is for is to demonstrate a very important topic that many fail to retain and end up with errors.

Think of this tower as an array



                                         +-+
                                         | |
                                      +-------+
                                      |   8   |
                                     +---------+
                                     |    7    |
                                    +-----------+
                                    |     6     |
                                   +-------------+
                                   |      5      |
                                  +---------------+
                                  |       4       |
                                 +-----------------+
                                 |        3        |
                                +-------------------+
                                |         2         |
                               +---------------------+
                               |          1          |
                               +---------------------+
                                         +-+


Now to remove the items in this array, we can simply run a loop from 1 to the max number of elements and remove each item in the array, right?

Yes, Right and also Wrong. If you are left wondering why right and wrong at the same time, then this is why. The idea is correct, we need to iterate through the array and remove each element, but the way we are going to do it is what will change.

Let's say this is what our code would have been based on our understanding
local maxElements = 8 -- We are setting this manually for now
 local i

 for i=1,maxElements do
  ourArray[i]:removeSelf() -- assumingly that the items in our array were display objects
  ourArray[i] = nil
 end

this will work for a few and then bomb or stop, now you will wonder why, let us DRY run it to see what happens, (Dry run is testing on paper to see what happens rather than to actually run it in the code)
i     Array Items      ourArray[i]:removeSelf
1         8                [1] -> Works fine
2         7                [2] -> Works fine
3         6                [3] -> Works fine
4         5                [4] -> Works fine
5         4                [5] -> BREAKS!!
6         3                [6]
7         2                [7]
8         1                [8]
Wonder why it breaks at the 5th iteration? because we are trying to remove the 5th item from the array and there are only 4 items in the array. So can we never use the iterator method??
This is where the tower thing comes in handy, there were a few four letter (not the 'F' words) words that were created to explain a lot of things in computing, they were GIGO, LIFO, FIFO, if you do not know these, no worries, they stand for
Garbage In Garbage Out which just means if the input is wrong, the output is wrong too.
Last In First Out which means like the tower, the last piece that comes in has to be the first piece that goes out.
First In First Out which means the first element that is added has to be the first element that needs to be out.

FIFO is more applicable to let's say in real life, stuff in the refrigerator, each package has an expiry date, if you had three tubs of youghurt, you had to first eat the one your brought in first as that expiry date is much more closer than the one you got in lets say yesterday. In computing, think of that as the life of a particle from a particle emitter, the first particle will be the first one to be removed as that would have served it's purpose than the one just emitted.

LIFO is used in a queue type scenario, like in a heap, stack, or more like the tower we just spoke, to get ring no 6, you have to first remove the rings 7 and 8, so the last ring has to be the first ring removed.

GIGO, we can all understand, no examples required for that.

So in this case, what we need to use is LIFO, the way we shall use it is, we shall iterate through all the elements, removing the last element first.
local maxElements = 8
 local i

 for i = maxElements, 1, -1 do
   ourArray[i]:removeSelf()
   ourArray[i] = nil
 end
and let us dry run this to see what happens
i     Array Items      ourArray[i]:removeSelf
8         8                [8] -> Works fine
7         7                [7] -> Works fine
6         6                [6] -> Works fine
5         5                [5] -> Works fine
4         4                [4] -> Works fine
3         3                [3] -> Works fine
2         2                [2] -> Works fine
1         1                [1] -> Works fine

Yahoo!! we have managed to get what we were after.

This is one of the most common mistake that practically every one makes, I have been through that too, so there is no shame in admitting that. I hope that you have learned something from this and has helped you to understand the concept of how to remove items from an array using an iterator and also the three GIGO, LIFO, FIFO...

image source dx4-blog

Comments

Popular Posts