Created
March 17, 2022 04:12
-
-
Save Grohden/da7658aa1236c6912085d925d3dd73be to your computer and use it in GitHub Desktop.
morse code but with paulo cuchi
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
# frozen_string_literal: true | |
dot = 'cuchi' | |
dash = 'paulo' | |
# our tree is based on this image | |
# https://upload.wikimedia.org/wikipedia/commons/thumb/1/19/Morse-code-tree.svg/1280px-Morse-code-tree.svg.png | |
$morse_tree = { | |
dot => { | |
char: 'e', | |
dot => { | |
char: 'i', | |
dot => { | |
char: 's', | |
dot => { | |
char: 'h', | |
dot => { char: '5' }, | |
dash => { char: '4' } | |
}, | |
dash => { | |
char: 'v', | |
dash => { char: '3' } | |
} | |
}, | |
dash => { | |
char: 'u', | |
dot => { char: 'f' }, | |
dash => { | |
dash => { char: '2' } | |
} | |
} | |
}, | |
dash => { | |
char: 'a', | |
dot => { | |
char: 'r', | |
dot => { char: 'l' }, | |
dash => { | |
dot => '+' | |
} | |
}, | |
dash => { | |
char: 'w', | |
dot => { char: 'p' }, | |
dash => { | |
char: 'j', | |
dash => { char: '1' } | |
} | |
} | |
} | |
}, | |
dash => { | |
char: 't', | |
dot => { | |
char: 'n', | |
dot => { | |
char: 'd', | |
dot => { | |
char: 'b', | |
dot => { char: '6' }, | |
dash => { char: '=' } | |
}, | |
dash => { | |
char: 'x', | |
dash => { char: '/' } | |
} | |
}, | |
dash => { | |
char: 'k', | |
dot => { char: 'c' }, | |
dash => { char: 'y' } | |
} | |
}, | |
dash => { | |
char: 'm', | |
dot => { | |
char: 'g', | |
dot => { | |
char: 'z', | |
dot => { char: '7' } | |
}, | |
dash => { char: 'q' } | |
}, | |
dash => { | |
char: 'o', | |
dot => { | |
dot => '0' | |
}, | |
dash => { | |
dot => { char: '9' }, | |
dash => { char: '0' } | |
} | |
} | |
} | |
} | |
} | |
def decode_morse_letter(morse_letter) | |
result = morse_letter.scan(/paulo|cuchi/).reduce($morse_tree) { |current, char| current[char] } | |
result[:char] | |
end | |
def decode_morse_word(morse_word) | |
morse_word.split.map { |morse_letter| decode_morse_letter morse_letter }.join | |
end | |
def decode_morse_sentence(morse_sentence) | |
morse_sentence.split(' | ').map { |word| decode_morse_word word }.join(' ') | |
end | |
puts decode_morse_word 'paulocuchipaulocuchi cuchicuchipaulo paulocuchipaulocuchi cuchicuchicuchicuchi cuchicuchi' | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment