Sunday, November 17, 2013

Hashes

I have tackled Java and C programming in the past (amateur level) and I have come across arrays before. It took a while to get around understanding array. All I know is that it is similar to a matrix in mathematics.

But what do you do in situations where you want to store data that are "pairs". For example a name of a person and their corresponding birthdays or a name of person and their corresponding addresses? Sometimes it is better to use the class Hash than Array in this case.

Let's say we have the following list:

John Arturo, 16 May 1979
Jacob Joaqim, 18 Oct 1992
Tyson Mayer, 22 Dec 1986

In Ruby, you can store these data as pairs:
  birth_date = {}
  birth_date['John Arturo'] = '16 May 1979'
  birth_date['Jacob Joaqim'] = '18 October 1992'
  birth_date['Tyson Mayer'] = '22 December 1986'

So if you want to know a person's birthday on that list you can do the following:
puts 'Whose birthday do you want to know?'
name = gets.chomp
date = birthday[name] # easy retrieval of the date according to                         # the name

Displaying the date:
if date == nil
  puts 'Ooohh... I don't know that one...'
else
  puts date[0..5] # this excludes the year in the output
end

Saturday, November 9, 2013

Uluru and Tealeaf Academy

It has been a long time since I posted in this blog (more than 5 years!!!).  What have I been doing since then? Travel, and the fact that I was stuck in a job for 4 years - the longest I have ever stayed in a job. And during this time, I was beginning to lose my identity: I was becoming boring, more averse to risk-taking and just becoming unhappier each day.  There were good sides of course mainly I can afford more than I used to be able to. But I have come to the point where my time is now more valuable than money and it's time to leave this cocoon of comfort.  So about 3 weeks ago, I quit my job and enrolled in an online course for programming.  I did enrol in some bootcamp in Australia, however the school had some issues with starting up.  Bootcamp would have been more preferable as it will force me to learn and gives the right environment.

So why programming? I chose programming because of the money (hopefully) - starting salaries are pretty high.  Also personally, it will give me the discipline and mental skills to solve problems or tackle challenges.  I'm always amazed by people when they say "I'm a programmer/ software engineer", etc mainly because I know how hard programming is.  Then it only occured to me much later: "If they can do it, why can't I?". I think this is a good way to find out. And hopefully, with this skill, I can travel the world.

I heard about Tealeaf Academy from Quora forum (after researching a lot of bootcamp schools).  There are a lot of online schools and the bootcamp style are Tealeaf and Bloc.  Tealeaf is more cost effective and the description that Kevin Wang (Tealeaf co-founder) put on Quora convinced me. So I enrolled in it.

The course started Saturday 26 Oct 2013.  And as usual I got my priorities right again: I decided to travel first before doing the course. Destination: Uluru - something I've always wanted to do. After finding some fellow travellers in Gumtree, we left on 26 Oct 2013 and over a few days drove about 2,300 kilometres to Uluru.


It was beautiful! By being there, I can understand why people all over the world would travel thousands of kilometres just to see a big red rock.  And Uluru's cousin, The Olgas, were even more stunning.

I came back the week after and checked in my email to see numerous emails from the Tealeaf forum. I looked at the material and found out I hugely underestimated this.  You see, I have done programming before mainly C and Java, and I thought this should be easy.  But it's not.  It was overwhelming that the first three days after the trip, I procrastinated thinking I can quit this, it was too hard. And then again, the angel side of my mind  kept nagging: you will always be stuck on this same situation if you quit this.  And with much reluctance, I finally decided to just do it. Swoosh!

Now we are in Week 3 of the course material, and I'm still more than halfway through the Week 1 assignment. The major assignment was making an interactive blackjack game (all text based, no graphics yet).  It took me 2 days to finish my own version of the blackjack program. It works but looking at my coding style, it was a bit convoluted.  Then I watched the blackjack solution video by Chris Lee and he freaking did it in about an hour.  That's skill for you! By watching it, I learnt how to make my program more efficient eg the data structure for storing the cards (numbers and suits) and creating calculate_total method.  And this took me a shorter time: less than a day.

Code for blackjack in ruby:
Original version:
https://github.com/eduardodelcastillo/Tealeaf_Week1/blob/master/Blackjack.rb

Version 2 (a better code):
https://github.com/eduardodelcastillo/Tealeaf_Week1/blob/master/blackjack_v2.rb