Morse code translator

  • Write the shortest program to transform the standard input into Morse code. Characters not in the table should be printed as they are.

    International Morse Code

    Are both capital and lowercase letters encoded?

    Also, could you provide a sample input and output to clarify details like spacing between words?

    Well, in Morse, there's no difference between lower and upper case. There's no real symbol for space (it's just a 3/4 times silence) so I guess it should be translated as a space.

    @Joey: The details are up to you.

    Doing the reverse might be a bit more challenging. Say, with no letter separation?

    How is pauses (The silence equal to one dot) encoded?

  • gnibbler

    gnibbler Correct answer

    10 years ago

    Golfscript - 74 chars

    This answer supports only uppercase and digits. The letters are separated by newlines and words are separated by 2 newlines

    {." ETIANMSURWDKGOHVF L PJBXCYZQ"?)"?/'#! 08<>"@))10%=or 2base(;{!45+}%n}%


        { }%          as usual works like a map over the array
    . push a copy of the char onto the stack
    " ETIAN..." this is a lookup table for the uppercase characters
    ? like a string.find returns the index of the char in the string
    or -1 if it is not found (ie it's a digit)
    ) increment that index so E=>2 T=>3 I=>4 etc. notice that if the
    char is not an uppercase letter or space this is now 0 (False)
    "?/'#!..." this is a lookup table for the digits. it will be used in the
    reverse way to the other lookup table.
    @ pull that copy we made of the char to the top of the stack
    ))%10 convert ascii digit to a number by adding 2 and taking mod 10.
    It's important to do it this way because all the uppercase
    letters hit this code too, and we need to make sure they fall
    in the range 0..9 or the next step will fail.
    = pull the nth char from the string eg "Hello"1= gives "e"
    or remember if the uppercase lookup fails we have a 0 result, so
    the digit lookup will be used
    2base convert to base 2 so E=>[1 0], T=>[1 1], I=>[1 0 0] etc.
    (; pop the front of the list and throw it away so E=>[0], T=>[1]
    {!45+}% negate each bit and add 45, this gives ascii value of . and -
    n newline separates each word. this could be 32 if you wanted to
    separate the words with spaces for a cost of 1 stroke

    Golfscript - 85 chars

    This is shorter than my SO answer due to the relaxed requirements here
    The input must be uppercase/digits and the punctuation characters ".,?"

    {." ETIANMSURWDKGOHVF!L!PJBXCYZQ"?)"UsL?/'#! 08<>"@".,?"58,48>+?=or

    Since the punctuation is not even required here, I may shorten the answer even more

    My answer from SO

    Golfscript - 107 chars

    newline at the end of the input is not supported, so use something like this

    echo -n Hello, Codegolfers| ../golfscript.rb

    Letters are a special case and converted to lowercase and ordered in their binary positions.
    Everything else is done by a translation table

    ' '/{{.32|"!etianmsurwdkgohvf!l!pjbxcyzq"?)"UsL?/'#! 08<>"@".,?0123456789"?=or
    2base(;>{'.-'\=}%' '}%}%'/'*

    The order of your code blocks here is confusing George's UserScript. Could I trouble you to rearrange? And I *would* like to see it explained. I get the implicit tree, but the rest is a mystery to me.

    @dmckee, done. George's script noticed my count was out by one, so I fixed that too

    Requiring upvotes to see the analysis... boo! The analysis is what makes answers worth the upvotes!

    @Nick, ok I think it's as short as I can get it, so I'll get to work on that analysis

License under CC-BY-SA with attribution

Content dated before 7/24/2021 11:53 AM

Tags used