Morse Code

Build a function, ‘morse_encode(str)’ that takes in a string (no numbers or punctuation) and outputs the morse code for it. See http://en.wikipedia.org/wiki/Morse_code. Put two spaces between words and one space between letters.

You’ll have to type in morse code: I’d use a hash to map letters to codes. Don’t worry about numbers.

My solution:

I learned the difference between global variables and Ruby constants while solving this problem.  They both serve similar purposes but global variables’ values can be changed while Ruby constants cannot.  Also, constants are written in all uppercase and global variables start with “$”.

Another thing to remember is how to retrieve key/value from hash.  Here’s a handy cheat sheet of shortcuts for Ruby hashes.

screen-shot-2016-11-29-at-5-48-39-pm

Test Cases: 

screen-shot-2016-11-29-at-5-41-43-pm

Advertisements
Morse Code

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s