Pyth, 106 bytes
DhNR.n.e+]++.[\.sllN::.Bk\0\.\1\-\ b*]*\ +2sllNt/16lNNjmj*3\ d.t[h"ET"h"IANM"h"SURWDKGO"h"HVF L PJBXCYZQ
Test it online!
Explanation
In a few words, what I do here is to generate the table column by column and then transpose the table before printing it. We notice that in a column, the morse codes for the letters can be represented as binary strings (replace .
by 0
and -
by 1
) when counting from zero to the index of the last letter in the column.
The algorithm relies on a function from which I give an example run below (for the second column):
1. Takes "IANM" as input
2. Generates the binary representations of zero up to len("IANM"): ["0", "1", "10", "11"]
3. Replace with dots and hyphens: [".", "-", "-.", "--"]
4. Pad with dots up to floor(log2(len("IANM"))): ["..", ".-", "-.", "--"]
5. Add the corresponding letters: [".. I", ".- A", "-. N", "-- M"]
6. After each element, insert a list of 16 / len("IANM") - 1 (= 3) strings containing only spaces of length floor(log2(len("IANM"))) + 2 (= 4):
[".. I", [" ", " ", " "], ".- A", [" ", " ", " "], "-. N", [" ", " ", " "], "-- M", [" ", " ", " "]]
7. Flatten that list:
[".. I", " ", " ", " ", ".- A", " ", " ", " ", "-. N", " ", " ", " ", "-- M", " ", " ", " "]
8. That's it, we have our second column!
Code explanation
I cut the code in two. The first part is the function described above, the second part is how I use the function:
DhNR.n.e+]++.[\.sllN::.Bk\0\.\1\-\ b*]*\ +2sllNt/16lNN
DhNR # Define a function h taking N returning the rest of the code. N will be a string
.e N # For each character b in N, let k be its index
.Bk # Convert k to binary
: \0\. # Replace zeros with dots (0 -> .)
: \1\- # Replace ones with hyphens (1 -> -)
.[\.sllN # Pad to the left with dots up to floor(log2(len(N))) which is the num of bits required to represent len(N) in binary
++ \ b # Append a space and b
] # Make a list containing only this string. At this point we have something like [". E"] or [".. I"] or ...
+ *]*\ +2sllNt/16lN # (1) Append as many strings of spaces as there are newlines separating each element vertically in the table
.n # At this point the for each is ended. Flatten the resulting list and return it
(1): In the morse table, in the first column, there is seven lines after each line containing a letter ("E" and "T"). In the second column, it is three lines. Then one (third column), then zero (last column). That is 16 / n - 1
where n
is the number of letters in the column (which is N
in the code above). That what does the code at line
(1):
*]*\ +2sllNt/16lN
sllN # Computes the num of bits required to represent len(N) in binary
+2 # To that, add two. We now have the length of a element of the current column
*\ # Make a string of spaces of that length (note the trailing space)
t/16lN # Computes 16 / len(N) - 1
*] # Make a list of that length with the string of spaces (something like [" ", " ", ...])
Alright, now we have a nice helpful function h
which basically generates a table's column out of a sequence of characters. Let's use it (note the two trailing spaces in the code below):
jmj*3\ d.t[h"ET"h"IANM"h"SURWDKGO"h"HVF L PJBXCYZQ
h"ET" # Generate the first column
h"IANM" # Generate the second column
h"SURWDKGO" # Generate the third column
h"HVF L PJBXCYZQ # Generate the last column (note the two trailing spaces)
[ # Make a list out of those columns
.t # Transpose, because we can print line by line, but not column by column
mj*3\ d # For each line, join the elements in that line on " " (that is, concatenate the elements of the lines but insert " " between each one)
j # Join all lines on newline
The code can still be shortened; maybe I'll come back on it later.
1can each line 3 prepending spaces? – dzaima – 2017-05-23T18:58:27.313
1Standard loopholes disallow solution hard coding. How much of the table are we allowed to hard code? – Brunner – 2017-05-23T18:58:27.943
@Brunner considering the table is around 450 bytes, I doubt hard coding is the optimal solution – Cyoce – 2017-05-23T19:48:39.693
@Cyoce Definitely not the shortest version here, but Joerg Huelsermann got it down to 208 bytes in his brilliant php answers. – Brunner – 2017-05-23T20:57:09.390
@Brunner I don't think hard coding is an issue with Kolgomorov-Complexity challenges, since it is pretty much in the definition of the challenge. It doesn't take input, so how would you get to the solution, other than by hard coding it (and compressing or building it up in some shape or form)? – JAD – 2017-05-25T16:29:34.190
@JarkoDubbeldam I'm not opposed to hard-coding, I was just wondering if there was a limit. To answer your question, it doesn't have to be all or nothing. – Brunner – 2017-05-25T17:01:27.640
@Brunner Generally, there are better (shorter) ways to create the output than a simple print statement with the entire output in there. So while there isn't really a limit to the hardcoding, you generally won't win doing it like that. – JAD – 2017-05-25T18:30:50.277
@JarkoDubbeldam I never said that was the case and I wasn't implying that hard-coding might be an advantage. I do F# answers, I don't expect to win with any of those :) It's like I said earlier, I was just curious if there was an additional constraint or not. – Brunner – 2017-05-25T20:05:14.237
1Are we allowed to have trailing spaces? – Adám – 2017-05-28T21:17:28.440
If there´s a pattern, generating the content is almost always the shortest approach. – Titus – 2017-05-29T09:18:55.177
@Adám The default for [tag:ascii-art] is that it's just the visual effect of the output that matters, not the actual characters that make it up; thus, differences in invisible characters (like trailing whitespace) don't matter. The OP can override that if they want to, but I don't see why they would here. – None – 2017-06-18T22:20:16.087
Why no CR newlines? – CalculatorFeline – 2017-06-18T23:47:38.360