Building things that matter
blake-connally-373084.jpg

Code Challenges

If You Can Read This (Ruby)

Couldn't sleep so decided to code.  

The Challenge

The idea for this Kata came from 9gag today.here
You'll have to translate a string to Pilot's alphabet (NATO phonetic alphabet) wiki.
Like this:
Input: If you can read
Output: India Foxtrot Yankee Oscar Uniform Charlie Alfa November Romeo Echo Alfa Delta
Some notes
Keep the punctuation, and remove the spaces.
Use Xray without dash or space.

My Solution

This one was a bit fun for me because of my background in law enforcement.  While we didn't use the NATO phonetic alphabet, we did use the common speak one, which used mostly names (Adam Boy Charles David Edward Frank George, etc). 

Knowing that there were multiple phonetic alphabets out there, I opted to take the route of making my own hash to use for translations, that could either be updated, added to or have another added to entirely if this program were ever to be expanded to translate from more than one phonetic alphabet. 

NATO Phonetic Alphabet Hash

There is apparently a NATO function in Ruby that will allow you to call something like:

NATO[char.downcase.to_sym]

And it will pull the entire phonetic alphabet for you.  I may consider using that in a refactor, though I do like that having the hash here and visible, it is easier to see if there are issues arising in translations.  For instance some tests I had were not passing because of misspelled words, those are easier to catch in a visible hash like the one I have, though either will work. 

From here I created two other methods, one that will prepare our word string for translation and the other that will translate it.  The prep_for_translation method actually does two things, it downcases all the words and then splits them into an array on the spaces between them, effectively removing the spaces as per the directions.

Prep For Translation Method

While both downcasing and splitting the words could take place in our main method, I did it this way in the event that we may want to do something else to the words during the prep phase, remove numbers, punctuation, etc.  This would allow this method to handle all of the pre-translation prep in one place. 

The second method does the actual translation.  This method takes three parameters, it takes the word being translated, the translation array we defined in the main method and the nato_alphabet_hash that will be used to make the translation. 

Translate Method

The method splits the word into individual letters and then pushes the value of the letter's corresponding key in the nato_alphabet hash object. (a: Alfa, b: Bravo, etc).  I have the splitting of the word as it's own line but I can refactor that down further if I'd like to, or I can leave it so that someone else looking at the code can easily see what is happening step by step.

Once translated, we return the translation_array and join it with spaces to give the response back in the form the challenge has asked for. 

Robert Cornell