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.