読者です 読者をやめる 読者になる 読者になる

MORSE CODE(CodeEval)

codeeval ruby

CHALLENGE DESCRIPTION:

You have received a text encoded with Morse code and want to decode it.

INPUT SAMPLE:

Your program should accept as its first argument a path to a filename. Input example is the following:

.- ...- ..--- .-- .... .. . -.-. -..-  ....- .....
-... .... ...--

Each letter is separated by space char, each word is separated by 2 space chars.

OUTPUT SAMPLE:

Print out decoded words. E.g.

AV2WHIECX 45
BH3

You program has to support letters and digits only.

MyCode

#!/usr/bin/env ruby -w

MORSE_CODE = {
  '.-' => 'A',
  '-...' => 'B',
  '-.-.' => 'C',
  '-..' => 'D',
  '.' => 'E',
  '..-.' => 'F',
  '--.' => 'G',
  '....' => 'H',
  '..' => 'I',
  '.---' => 'J',
  '-.-' => 'K',
  '.-..' => 'L',
  '--' => 'M',
  '-.' => 'N',
  '---' => 'O',
  '.--.' => 'P',
  '--.-' => 'Q',
  '.-.' => 'R',
  '...' => 'S',
  '-' => 'T',
  '..-' => 'U',
  '...-' => 'V',
  '.--' => 'W',
  '-..-' => 'X',
  '-.--' => 'Y',
  '--..' => 'Z',
  '-----' => '0',
  '.----' => '1',
  '..---' => '2',
  '...--' => '3',
  '....-' => '4',
  '.....' => '5',
  '-....' => '6',
  '--...' => '7',
  '---..' => '8',
  '----.' => '9'
}

def morse_to_ascii(codes)
  codes.map { |code|
    code.split.map { |sign|
      MORSE_CODE[sign]
    }.join
  }.join(' ')
end

ARGF.each_line do |line|
  codes = line.chomp.split('  ')
  puts morse_to_ascii(codes)
end