Tuesday, December 6, 2011

Exercise 34: Accessing Elements Of Arrays: Learn Ruby The Hard Way: Practicum

Let's imagine that you are using a digital audio tape (I know some of you out there remember what a DAT is, if not think of a VCR tape. If neither of those are at all familiar to you, you're too young and this analogy won't make any sense). With a VCR tape, you had to fast forward and look for your content, and then pause it and see if you hit it right. With a DAT tape, the sections were digitally encoded, and if you hit fast forward to a specific spot, you would be able to find that exact spot and press play at the place you wanted to go.

When we have arrays but we aren't quite sure how to get to the elements other than scrolling through them, that's a lot like looking for content on a VCR tape with the remote control. Knowing where items are and accessing them directly is a lot more like the DAT tape, which will fast forward to the exact spot you have told it to go to.

This exercise gives you a feeling as to what it takes to get to specific array elements.

- the first element in any array of items[] is items[0], meaning the first item is the "zeroth" item. This is just the way Ruby and many other languages do math and calculate things like arrays, so get used to it.

animals = ['bear', 'tiger', 'penguin', 'zebra']
bear = animals[0]

We are used to things being in order, and to do that, we think of "ordinal" numbers. 1 is first, 2 is second, 3 is third, etc. However, for a programmer, we need to be able to grab any item at any time, and so, to do this, it's easier to think of these items as an index, and indexes in computerese start at zero. This approach is called using "cardinal" numbers.


Remember: ordinal == ordered, 1st; cardinal == cards at random, 0.

Let’s practice this. Take this list of animals, and follow the exercises where I tell you to write down what animal you get for that ordinal or cardinal number. Remember if I say “first”, “second”, etc. then I’m using ordinal, so subtract 1. If I give you cardinal (0, 1, 2) then use it directly.

animals = ['bear', 'python', 'peacock', 'kangaroo', 'whale', 'platypus']

For each of these, write out a full sentence of the form: “The 1st animal is at 0 and is a bear.” Then say it backwards, “The animal at 0 is the 1st animal and is a bear.”

[
The animal at 1. The animal at 1 is is 2nd animal and is a python. The 2nd animal is at 1 and is a python. 
The 3rd animal. The 3rd animal is at 2 and is a peacock. The animal at 2 is the 3rd animal and is a peacock.
The 1st animal. The 1st animal is at 0 and is a bear. The animal at 0 is the 1st animal and is a bear.
The animal at 3. The animal at 3 is the 4th animal and is a kangaroo. The 4th animal is at 3 and is a kangaroo.  
The 5th animal. The 5th animal is a 4 and is a whale. The animal at 4 is the 5th animal and is a whale. 
The animal at 2. The animal at 2 is the 3rd animal and is a peacock. The 3rd animal is at 2 and is a peacock.
The 6th animal. The 6th animal is at 5 and is a platypus. the animal at 5 is the 6th animal and is a platypus.
The animal at 4. The animal at 4 is the 5th animal and is a whale. The 5th animal is at 4 and is a whale.   
]

Use your Ruby to check your answers. Hint: Ruby has also a few convenience methods for accessing particular elements in an array: animals.first and animals.last




Extra Credit


Read about ordinal and cardinal numbers online.

[ 
http://en.wikipedia.org/wiki/Ordinal_number 
and 
http://en.wikipedia.org/wiki/Cardinal_number
Lots of information, way more than would be practical to put here. 
]

With what you know of the difference between these types of numbers, can you explain why this really is 2010? (Hint, you can’t pick years at random.)

[
Well, it's 2011, but yeah, we have made a denomination of years based on being in the Christian Era (CE) and before the Christian Era (BCE). In this case, there is no year 0. We have taken a period of time and roughly placed it and its resulting timeline at year 1 CE and counted forward from there. Thus 2011 is the actual 2011th year since this event by our reckoning. 
]

Write some more arrays and work out similar indexes until you can translate them. Use Ruby to check your answers to this as well.




TESTHEAD's TAKEAWAYS:

This is an important area to consider when it comes to dealing with array values. Having that "zeroth" position and the idea of the ordinal value (the ordering of things the way we are used to) and associating them with a cardinal value (ordinal - 1, including cardinal value 0 for the start) will help us make sure we are getting the value we think we are getting.

No comments: