Skip to content

Instantly share code, notes, and snippets.

@decioferreira
Created October 2, 2012 10:22
Show Gist options
  • Save decioferreira/3818046 to your computer and use it in GitHub Desktop.
Save decioferreira/3818046 to your computer and use it in GitHub Desktop.
Morsels are Delicious
require 'benchmark'
require 'test/unit'
def morse_to_eng(morse)
a = " ET_IA_NM____SU_RW____DK_GO#{'_'*13}HV_F_____L__PJ#{'_'*13}BX_CY____ZQ"
morse.gsub(/[.-]/, '.' => 1, '-' => 2).gsub(/\d+ ?| /) { |l| a[l.to_i(3)] }
end
class TestMorseToEng < Test::Unit::TestCase
def test_simple
assert_equal(morse_to_eng('.... . .-.. .--. -- . --- ..- - --- ..-. - .... .. ... .-- . .-.. .-..'), 'HELP ME OUT OF THIS WELL')
end
def test_alphabet
assert_equal(morse_to_eng('.- -... -.-. -.. . ..-. --. .... .. .--- -.- .-.. -- -. --- .--. --.- .-. ... - ..- ...- .-- -..- -.-- --..'), 'ABCDEFGHIJKLMNOPQRSTUVWXYZ')
end
end
puts Benchmark.measure { 3000.times { morse_to_eng('.... . .-.. .--. -- . --- ..- - --- ..-. - .... .. ... .-- . .-.. .-..') } }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment