Write a formatted Morse code cheatsheet

Python 3.6, 201 197 193 187 bytes

for i in range(16):print('   '.join(i%k and' '*(2+j)or f'{i//k:0{j}b}'.replace(*'0.').replace(*'1-')+' '+'ETIANMSURWDKGOHVF L PJBXCYZQ  '[2**j-2+i//k]for j,k in zip((1,2,3,4),(8,4,2,1))))

Uses some formatting, unpacking and A000918 magic.

Retina, 125 bytes

+m`^((.*?)([-.]+) )(\w)((\w)+?)(((?<-6>)\w)+)$
$2$3 $4   $3. $5¶$.1$*     $3- $7

Try it online! Should be 121 bytes but I was too lazy to deal with the whitespace at the start and end. Explanation:

[blank line]

The letters whose code begins with . and - respectively are preloaded. (It is theoretically possible to avoid preloading the .- but it saves bytes this way.) _s are used instead of spaces as they are considered to be letters which makes them easier to match below.

+m`^((.*?)([-.]+) )(\w)((\w)+?)(((?<-6>)\w)+)$
$2$3 $4   $3. $5¶$.1$*     $3- $7

Here we split each line up into five pieces:

  • The letters for prefixes, if any
  • The current Morse code
  • The current letter
  • The first half of the remaining letters (their next character is .)
  • The second half of the remaining letters (their next character is -)

The pieces are then reassembled onto two lines:

  • The letters for prefixes, the current Morse code, the current letter, the Morse code with a . suffix, the first half of the remaining letters
  • Spaces replacing the first three pieces, the Morse code with a - suffix, the second half of the remaining letters

The new lines follow the same format as the existing line, just with an extra Morse prefix and half as many letters remaining to process. This is then repeated until each line has only one letter.

  [single space]

The _s are then changed back into spaces.

Jelly, 85 bytes

ØQj⁶“_ȦeƤbṅỌ[email protected]⁼Cq’œ?;⁶$⁺ṁ®L€€¤

A full program printing the cheat sheet.

Try it online!


Note: I do think there may be a way to trim this down by forming a list which formats correctly by use of the grid atom, G, but I can't quite work out how.

ØQj⁶“_ȦeƤbṅỌ[email protected]⁼Cq’œ?;⁶$⁺ṁ®L€€¤ - Link 1: get "letters" lists: no arguments
ØQ                             - Qwerty yield = ["QWERTYUIOP","ASDFGHJKL","ZXCVBNM"]
  j⁶                           - join with spaces = "QWERTYUIOP ASDFGHJKL ZXCVBNM"
    “_ȦeƤbṅỌ[email protected]⁼Cq’             - base 250 number = 23070726812742121430711954614
                  œ?           - lexicographical permutation at index = "ETIANMSURWDKGOHVF L PJBXCYZQ"
                       ⁺       - do this twice:
                      $        -   last two links as a monad
                    ;⁶         -     concatenate a space              = "ETIANMSURWDKGOHVF L PJBXCYZQ  "
                             ¤ - nilad followed by link(s) as a nilad:
                         ®     -   recall from registry (4R2ṗ from the Main link)
                          L€€  -   length for €ach for €ach = [[1,1],[2,2,2,2],[3,3,3,3,3,3,3,3],[4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4]]
                        ṁ      - mould like = ["ET","IANM","SURWDKGO","HVF L PJBXCYZQ  "]

4R2ṗ©ị⁾.-;€€⁶ż"¢;€€⁶$⁺W€€j"731Dẋ@⁶¤ZµKFṚ;⁶ẋ³¤ḣ29ṫ3Ṛµ€Y - Main link: no arguments
4R                                                     - range(4) = [1,2,3,4]
  2ṗ                                                   - Cartesian power with 2 = [[[1],[2]],[[1,1],[1,2],[2,1],[2,2]],...,[...,[2,2,2,2]]]
    ©                                                  - copy to register and yield
      ⁾.-                                              - literal ['.','-']
     ị                                                 - index into (makes all the codes, in four lists by length like reading the output top-bottom, left-right)
         ;€€⁶                                          - concatenate a space to each code
               ¢                                       - call last link (1) as a nilad (get the letters reordered as required)
             ż"                                        - zip left and right with zip dyad
                     ⁺                                 - do this twice:
                    $                                  -   last two links as a monad:
                ;€€⁶                                   -     concatenate a space to each code, letter pair
                      W€€                              - wrap each code, letter pair in a list
                                  ¤                    - nilad follwed by link(s) as a nilad:
                           731                         -   literal 731
                              D                        -   to decimal list = [7,3,1]
                               ẋ@⁶                     -   repeat a space = ["       ","   "," "]
                         j"                            - zip with dyad join
                                   Z                   - transpose
                                    µ              µ€  - for each:
                                     K                 -   join with spaces
                                      F                -   flatten
                                       Ṛ               -   reverse
                                            ¤          -   nilad followed by link(s) as a nilad:
                                         ⁶ẋ³           -     space repeated 100 times
                                        ;              -   concatenate
                                             ḣ29       -   head to 29 (make all "lines" the same length)
                                                ṫ3     -   tail from 3 (trim off two spaces from each line)
                                                  Ṛ    -   reverse
                                                     Y - join with newlines
                                                       - implicit print