Skip to main content
Commonmark migration
Source Link

#Pyth, 34 33 bytes

Pyth, 34 33 bytes

#Pyth, 34 33 bytes

Pyth, 34 33 bytes

Saved a byte
Source Link
Sok
  • 6.2k
  • 1
  • 17
  • 30

#Pyth, 3434 33 bytes

sm@+G;id3csCmtj+27x+G;d3+Wm@+G;id3csCmtj+27x+G;d3+W!%lz3zd3

Full program. Input is expected as lowercase, output is a character array. Try it online herehere, or verify all test cases at once herehere.

sm@+G;id3csCmtj+27x+G;d3+Wm@+G;id3csCmtj+27x+G;d3+W!%lz3zd3   Implicit: z=input(), d=" ", G=lowercase alphabet
                            lz       Length of z
                           %  3      The above, mod 3
                         W!          If the above != 3...
                        +      zd    ... append a space to z
            m                        Map the elements of the above, as d, using:
                   +G;                 Append a space to the lowercase alphabet
                  x   d                Find the 0-based index of d in the above
               +27                     Add 27 to the above
              j        3               Convert to base 3
             t                         Discard first element (undoes the +27, ensures result is 3 digits long)
           C                         Transpose the result of the map
          s                          Flatten
         c                       3   Split into chunks of length 3
 m                                   Map the elements of the above, as d, using:
      id3                              Convert to decimal from base 3
  @+G;                                 Index the above number into the alphabet + space
s                                    Concatenate, implicitImplicit print

Alternative 34 byte solution: sm@+G;id3csCm.[03jx+G;d3+W!%lz3zd3 - rather than +27 and tail, uses .[03 to pad with 0 to length 3. Can be 33 if the leading s is dropped.

Edit: saved a byte by dropping leading s as character arrays are valid output

#Pyth, 34 bytes

sm@+G;id3csCmtj+27x+G;d3+W!%lz3zd3

Full program. Try it online here, or verify all test cases at once here.

sm@+G;id3csCmtj+27x+G;d3+W!%lz3zd3   Implicit: z=input(), d=" ", G=lowercase alphabet
                            lz       Length of z
                           %  3      The above, mod 3
                         W!          If the above != 3...
                        +      zd    ... append a space to z
            m                        Map the elements of the above, as d, using:
                   +G;                 Append a space to the lowercase alphabet
                  x   d                Find the 0-based index of d in the above
               +27                     Add 27 to the above
              j        3               Convert to base 3
             t                         Discard first element (undoes the +27, ensures result is 3 digits long)
           C                         Transpose the result of the map
          s                          Flatten
         c                       3   Split into chunks of length 3
 m                                   Map the elements of the above, as d, using:
      id3                              Convert to decimal from base 3
  @+G;                                 Index the above number into the alphabet + space
s                                    Concatenate, implicit print

Alternative 34 byte solution: sm@+G;id3csCm.[03jx+G;d3+W!%lz3zd3 - rather than +27 and tail, uses .[03 to pad with 0 to length 3.

#Pyth, 34 33 bytes

m@+G;id3csCmtj+27x+G;d3+W!%lz3zd3

Full program. Input is expected as lowercase, output is a character array. Try it online here, or verify all test cases at once here.

m@+G;id3csCmtj+27x+G;d3+W!%lz3zd3   Implicit: z=input(), d=" ", G=lowercase alphabet
                           lz       Length of z
                          %  3      The above, mod 3
                        W!          If the above != 3...
                       +      zd    ... append a space to z
           m                        Map the elements of the above, as d, using:
                  +G;                 Append a space to the lowercase alphabet
                 x   d                Find the 0-based index of d in the above
              +27                     Add 27 to the above
             j        3               Convert to base 3
            t                         Discard first element (undoes the +27, ensures result is 3 digits long)
          C                         Transpose the result of the map
         s                          Flatten
        c                       3   Split into chunks of length 3
m                                   Map the elements of the above, as d, using:
     id3                              Convert to decimal from base 3
 @+G;                                 Index the above number into the alphabet + space
                                    Implicit print

Alternative 34 byte solution: sm@+G;id3csCm.[03jx+G;d3+W!%lz3zd3 - rather than +27 and tail, uses .[03 to pad with 0 to length 3. Can be 33 if the leading s is dropped.

Edit: saved a byte by dropping leading s as character arrays are valid output

Source Link
Sok
  • 6.2k
  • 1
  • 17
  • 30

#Pyth, 34 bytes

sm@+G;id3csCmtj+27x+G;d3+W!%lz3zd3

Full program. Try it online here, or verify all test cases at once here.

sm@+G;id3csCmtj+27x+G;d3+W!%lz3zd3   Implicit: z=input(), d=" ", G=lowercase alphabet
                            lz       Length of z
                           %  3      The above, mod 3
                         W!          If the above != 3...
                        +      zd    ... append a space to z
            m                        Map the elements of the above, as d, using:
                   +G;                 Append a space to the lowercase alphabet
                  x   d                Find the 0-based index of d in the above
               +27                     Add 27 to the above
              j        3               Convert to base 3
             t                         Discard first element (undoes the +27, ensures result is 3 digits long)
           C                         Transpose the result of the map
          s                          Flatten
         c                       3   Split into chunks of length 3
 m                                   Map the elements of the above, as d, using:
      id3                              Convert to decimal from base 3
  @+G;                                 Index the above number into the alphabet + space
s                                    Concatenate, implicit print

Alternative 34 byte solution: sm@+G;id3csCm.[03jx+G;d3+W!%lz3zd3 - rather than +27 and tail, uses .[03 to pad with 0 to length 3.