Drat, I was hoping to get here before the GolfScripters arrived :-(
Anyhoo...
C: 228 characters:
char n,t,m[9],*c=" etianmsurwdkgohvf l pjbxcyzq 54 3 2& + 16=/ ( 7 8 90 $ ?_ \" . @ ' - ;! ) , :";
main(){while(scanf("%s",m)>0){for(t=m[6]=n=0;m[n];n++)t+=t+1+(m[n]&1);putchar(c[t]);}}
I thought I'd add an explanation of how this works.
The input data is parsed according to the tree data in *c
, which can be expanded as follows (using ·
to represent a vacant node):
dot <-- (start) --> dash
e t
i a n m
s u r w d k g o
h v f · l · p j b x c y z q · ·
5 4 · 3 · · · 2 & · + · · · · 1 6 = / · · · ( · 7 · · · 8 · 9 0
····$·······?_····"··.····@···'··-········;!·)·····,····:·······
Starting at the top of the tree, work your way down while moving to the left for a dot and to the right for a dash. Then output whatever character you happen to be at when the input string ends (i.e., when a whitespace character is encountered). So for example, three dots and a dash will take you to v
via e
, i
and s
. Instead of explicitly checking for dots (ASCII \x2e
) and dashes (ASCII \x2d
), we only need to check the last bit (m[n]&1
), which is 0 for .
and 1 for -
.
Six rows are enough to encode everything except $
, which has 7 dot/dashes: ...-..-
, but since the input data is guaranteed to be valid, this can easily be fixed by truncating the input at 6 characters (m[6]=0
) and interpreting ...-..
as $
instead. We can also cut away the last 7 bytes from the tree data, since they are all empty and aren't needed if the input is valid.
You say only 'character symbols' is that characters and symbols? – Sinkingpoint – 2014-01-09T04:59:32.393
@Quirliom All the "symbols" in that link are characters. Anything that you can put in a String is a character (well, basically). However, that part of the question is basically saying that every bit of morse will be valid. – Justin – 2014-01-09T05:03:53.793
@Quirliom Yes, every Morse 'character', such as .- for 'a' and . for 'e' is valid. No non-Morse characters need be handled. – None – 2014-01-09T05:58:08.117
What about letter space and word space ? One space for the former and two (or more) for the latter ? – Paul R – 2014-01-09T10:30:48.010
Slighly (un)related: http://stackoverflow.com/questions/1352587/code-golf-morse-code
– javatarz – 2014-01-09T12:07:57.793@PaulR Yes, that sounds good. – None – 2014-01-09T15:59:49.623