Created
October 2, 2012 10:22
-
-
Save decioferreira/3818046 to your computer and use it in GitHub Desktop.
Morsels are Delicious
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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