Trifid Cipher (tanpa kata kunci)


19

Pengantar:

Saya memiliki banyak cipher yang berbeda yang disimpan dalam dokumen yang pernah saya kompilasi sebagai seorang anak, saya memilih beberapa yang saya pikir paling cocok untuk tantangan (tidak terlalu sepele, dan tidak terlalu keras) dan mengubahnya menjadi tantangan. Sebagian besar dari mereka masih berada di kotak pasir, dan saya belum yakin apakah saya akan memposting semuanya, atau hanya beberapa. Ini yang kedua ( Computer Cipher adalah yang pertama kali saya posting).


Untuk Trifid Cipher (tanpa menggunakan kata kunci) alfabet (dan wildcard tambahan) dibagi menjadi tiga tabel 3 oleh 3:

table 1:     table 2:     table 3:
 |1 2 3       |1 2 3       |1 2 3
-+-----      -+-----      -+-----
1|a b c      1|j k l      1|s t u
2|d e f      2|m n o      2|v w x
3|g h i      3|p q r      3|y z  

Teks yang ingin kami enkripsikan adalah karakter pertama dengan karakter yang disandikan ke dalam nomor kolom-baris-tabel. Misalnya, teks this is a trifid ciphermenjadi:

        t h i s   i s   a   t r i f i d   c i p h e r
table:  3 1 1 3 3 1 3 3 1 3 3 2 1 1 1 1 3 1 1 2 1 1 2
row:    1 3 3 1 3 3 1 3 1 3 1 3 3 2 3 2 3 1 3 3 3 2 3
column: 2 2 3 1 3 3 1 3 1 3 2 3 3 3 3 1 3 3 3 1 2 2 3

Kami kemudian meletakkan semuanya satu demi satu baris demi baris dalam tabel di atas dalam kelompok tiga:

311 331 331 332 111 131 121 121 331 331 313 133 232 313 332 322 313 313 132 333 313 331 223

Dan itu diubah kembali ke karakter menggunakan tabel yang sama:

s   y   y   z   a   g   d   d   y   y   u   i   q   u   z   w   u   u   h       u   y   o

Satu hal yang perlu diperhatikan, panjang input harus coprime ke 3. Jadi jika panjangnya adalah kelipatan 3, kami menambahkan satu atau dua spasi tambahan untuk membuat panjang input tidak kelipatan 3 lagi.

Tantangan:

Diberikan string sentence_to_encipher, enkripsinya seperti dijelaskan di atas.

Anda hanya perlu mengenkripsikan yang diberikan sentence_to_encipher, jadi tidak perlu membuat program / fungsi penguraikan juga. Saya mungkin membuat tantangan bagian 2 untuk menguraikan di masa depan (meskipun saya merasa itu sepele / mirip dengan proses penyandian).

Aturan tantangan:

  • Anda dapat mengasumsikan surat sentence_to_encipherwasiat tersebut hanya berisi huruf dan spasi.
  • Anda dapat menggunakan huruf besar atau huruf besar penuh (sebutkan mana yang Anda gunakan dalam jawaban Anda).
  • Anda dapat memilih untuk menambahkan satu atau dua spasi tambahan saat panjang input 3 untuk membuatnya bukan kelipatan 3 lagi.
  • I / O fleksibel. Baik input dan output dapat berupa string, daftar / array / aliran karakter, dll.

Aturan umum:

  • Ini adalah , jadi jawaban tersingkat dalam byte menang.
    Jangan biarkan bahasa kode-golf mencegah Anda memposting jawaban dengan bahasa non-codegolf. Cobalah untuk memberikan jawaban sesingkat mungkin untuk bahasa pemrograman 'apa saja'.
  • Aturan standar berlaku untuk jawaban Anda dengan aturan I / O default , sehingga Anda diizinkan untuk menggunakan STDIN / STDOUT, fungsi / metode dengan parameter yang tepat dan tipe pengembalian, program penuh. Panggilanmu.
  • Celah default tidak diperbolehkan.
  • Jika memungkinkan, silakan tambahkan tautan dengan tes untuk kode Anda (yaitu TIO ).
  • Juga, menambahkan penjelasan untuk jawaban Anda sangat dianjurkan.

Kasus uji:

Input:            "this is a trifid cipher"
Output:           "syyzagddyyuiquzwuuh uyo"

Input:            "test"
Output:           "utbk"

Input:            "output"
Possible outputs: "rrvgivx" (one space) or "rrzcc lr" (two spaces)

Input:            "trifidcipher"
Possible output:  "vabbuxlzz utr" (one space) or "vabbyzv rx ie " (two spaces)

3
Saya membacanya sebagai "tanpa keyboard ". Itu akan menjadi tantangan yang menyenangkan!
Cort Ammon - Reinstate Monica

1
Mengapa panjang input harus koprime ke 3? Saya tidak melihat bagaimana itu penting di sini.
Dana Gugatan Monica

Persyaratan coprime tidak diperlukan untuk membuat cipher bekerja, tetapi itu membuatnya sedikit lebih aman dengan memastikan kelompok tiga digit tidak akan sejalan dengan divisi baris dari daftar digit awal.
Sparr

@NicHartley Anda memang benar bahwa meskipun inputnya dapat dibagi 3, Anda masih dapat mengubah tabel. Awalnya saya tidak memiliki aturan itu di Sandbox, tetapi seseorang berkata kepada saya bahwa Wikipedia untuk Trifid menunjukkan bahwa itu harus menambahkan satu atau dua ruang untuk membuat pengodean sedikit lebih aman. Jadi saya menambahkannya sebagai bagian dari tantangan, dan membuatnya lebih selaras dengan halaman Wikipedia (terlepas dari Kata Kunci yang saya hapus).
Kevin Cruijssen

1
@KevinCruijssen I ... benar-benar tidak melihat bagaimana itu membuatnya lebih aman (jika ada, memaksa input tidak dengan panjang tertentu akan membuatnya kurang aman, karena Anda dapat menebak bahwa karakter terakhir sedang mengkode sebuah space) tetapi saya setuju bahwa menjaga deskripsi sandi di sini sesuai dengan Wikipedia adalah ide yang bagus. Terima kasih telah menjelaskan!
Dana Gugatan Monica

Jawaban:


6

Jelly , 29 26 25 byte

⁶Øa;3ṗ¤,©Ṛy;⁶$L3ḍƊ¡ZFs3®y

Cobalah online!

Bagaimana itu bekerja

⁶Øa;3ṗ¤,©Ṛy;⁶$L3ḍƊ¡ZFs3®y  Main link. Argument: s (string)

⁶                          Set the return value to space.
 Øa;                       Append it to "a...z".
    3ṗ¤                    Yield the third Cartesian power of [1, 2, 3].
       ,©                  Pair the results and store the pair in the register.
                           The register now holds
                           [[[1, 1, 1], ..., [3, 3, 3]], ['a', ... ,'z', ' '].
         Ṛ                 Reverse the outer array.
           ;⁶$             Append a space to s...
              L3ḍƊ¡        if the length is divisible by 3.
          y                Transliterate according to the mapping to the left.
                   ZFs3    Zip/transpose, flatten, split into chunks of length 3.
                       ®y  Transliterate according to the mapping in the register.

Saya tidak pernah tahu aturan tentang output efek samping ... tetapi pengaturan ;L3ḍƊ¡⁶µ⁶Øa;3ṗ¤,ðṚyZFs3⁸ydapat menghapus µjika itu dapat diterima untuk 24.
Jonathan Allan

Kami memiliki konsensus yang lemah ( + 6 / -1 ) bahwa ini diperbolehkan, jadi terima kasih!
Dennis

Outputnya tampaknya salah. Saya tidak berpikir ruang tambahan "menempel".
Dennis

6

Arang , 39 byte

≔E⁺θ× ¬﹪Lθ³⌕βιθ⭆⪪E⁺÷θ⁹⁺÷θ³θ﹪鳦³§⁺β ↨³ι

Cobalah online! Tautan adalah untuk mengucapkan versi kode. Penjelasan:

≔               Assign
   θ            Input string
  ⁺             Concatenated with
                Literal space
    ×           Repeated
         θ      Input string
        L       Length
       ﹪        Modulo
          ³     Literal 3
      ¬         Logical not
 E              Mapped over characters
             ι  Current character
           ⌕    Position found in
            β   Lowercase alphabet
              θ To variable

     θ                      List of positions
    ÷                       Vectorised integer divide by
      ⁹                     Literal 9
   ⁺                        Concatenated with
         θ                  List of positions
        ÷                   Vectorised integer divide by
          ³                 Literal 3
       ⁺                    Concatenated with
           θ                List of positions
  E                         Map over values
             ι              Current value
            ﹪               Modulo
              ³             Literal 3
 ⪪                          Split into
                ³           Groups of 3
⭆                           Map over groups and join
                   β        Lowercase alphabet
                  ⁺         Concatenated with
                            Literal space
                 §          Cyclically indexed by
                       ι    Current group
                     ↨      Converted from
                      ³     Base 3
                            Implicitly print

6

Python 2 , 180 176 174 165 163 byte

lambda s:''.join(chr(32+(33+a*9+3*b+c)%59)for a,b,c in zip(*[iter(sum(zip(*[(c/9,c/3%3,c%3)for c in map(o,s+' '[len(s)%3:])]),()))]*3))
o=lambda c:(ord(c)%32-1)%27

Cobalah online!

Input dapat berupa atas atau bawah. Output huruf besar


6

Pyth, 34 33 byte

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

Program lengkap. Input diharapkan sebagai huruf kecil, output adalah array karakter. Cobalah online di sini , atau verifikasi semua uji sekaligus di sini .

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

Alternatif 34 byte solusi: sm@+G;id3csCm.[03jx+G;d3+W!%lz3zd3- daripada +27 dan ekor, gunakan .[03untuk pad dengan 0 hingga panjang 3. Dapat 33 jika mengarah sdijatuhkan.

Sunting: menyimpan satu byte dengan menjatuhkan dulu skarena array karakter adalah output yang valid


5

Ruby , 153 145 138 131 byte

->a{a<<" "if a.size%3<1;a.map{|c|[(b=(c.ord%32-1)%27)/9,b%9/3,b%3]}.transpose.join.scan(/.{3}/).map{|x|((x.to_i(3)+65)%91+32).chr}}

Cobalah online!

Pendekatan cepat dan naif, bekerja dengan teks huruf kecil. Input dan output array karakter.


4

Java (JDK) , 192 byte

s->{String T="",R=T,C=T,r=T;for(int c:s){c-=c<33?6:97;T+=c/9;R+=c%9/3;C+=c%3;}for(var S:(s.length%3<1?T+2+R+2+C+2:T+R+C).split("(?<=\\G...)"))r+=(char)((Byte.valueOf(S,3)+65)%91+32);return r;}

Cobalah online!

Pendekatan yang sangat naif. Mengambil huruf kecil char[]sebagai input tetapi menghasilkan a String.

Penjelasan

s->{                                       // char[]-accepting lambda
 String T="",                              //  declare variables Table as an empty string,
        R=T,                               //                    Row as an empty string,
        C=T,                               //                    Column as an empty string,
        r=T;                               //                    result as an empty string.
 for(int c:s){                             //  for each character
  c-=c<33?6:97;                            //   map each letter to a number from 0 to 25, space to 26.
  T+=c/9;                                  //   append the table-value to Table
  R+=c%9/3;                                //   append the row-value to Row
  C+=c%3;                                  //   append the column-value to Column
 }                                         //
 for(var S:                                //  For each token of...
     (s.length%3<1?T+2+R+2+C+2:T+R+C)      //    a single string out of table, row and column and take the space into account if the length is not coprime to 3...
      .split("(?<=\\G...)"))               //    split every 3 characters
  r+=(char)((Byte.valueOf(S,3)+65)%91+32); //   Parses each 3-characters token into a number, using base 3,
                                           //  and make it a letter or a space
 return r;                                 //  return the result
}

Kredit


1
Dua golf kecil: Integer.valueOfke Byte.valueOfdan R+=c<26?(char)(c+97):' ';keR+=(char)(c<26?c+97:32);
Kevin Cruijssen


4

R , 145 byte

function(s,K=array(c(97:122,32),rep(3,3)))intToUtf8(K[matrix(arrayInd(match(c(utf8ToInt(s),32[!nchar(s)%%3]),K),dim(K))[,3:1],,3,byrow=T)[,3:1]])

Cobalah online!

I / O sebagai string; menambahkan satu spasi. Pengulangan aneh [,3:1]adalah karena pengindeksan array alami R agak berbeda.


Dang, Anda mengalahkan saya lebih dari 200 byte. Saya selalu terkesan dengan pengkodean Anda, @Giuseppe. Aku kadang
Sumner18

1
@ Sumner18 terima kasih juga! Saya biasanya mencoba untuk membiarkan satu atau dua hari berlalu sebelum menjawab tantangan karena saya tahu ada banyak pegolf R lain di sini sekarang, tetapi saya tidak bisa menolak yang ini sejak saya melihatnya di Sandbox. Anda selalu disambut ide-ide untuk bermain golf di ruang obrolan R golf . :-)
Giuseppe

3
@ Sumner18 juga tidak ada salahnya mencoba dan datang pendek, pengiriman pertama saya di sini mengerikan dan saya hanya menjadi lebih baik! Silakan terus memposting, saya pikir itu selalu baik untuk mendapatkan umpan balik sehingga Anda dapat meningkatkan :-)
Giuseppe

3

APL + WIN, 102 byte

⎕av[n[c⍳(⊂[2]((⍴t),3)⍴,⍉⊃(c←⊂[2]c,(,⍉9 3⍴c←9/⍳3),[1.1]27⍴⍳3)[(⎕av[n←(97+⍳26),33])⍳t←t,(3|⍴t←⎕)↓' '])]]

Penjelasan:

t←t,(3|⍴t←⎕)↓' ' Prompts for input and applies coprime condition

(⎕av[n←(97+⍳26),33]⍳ Indices of characters in APL atomic vector 

c←⊂[2]c,(,⍉9 3⍴c←9/⍳3),[1.1]27⍴⍳3) Create a matrix of table, row column for 27 characters

⊂[2]((⍴t),3)⍴,⍉⊃ Extract columns of c corresponding to input and re-order

c⍳ Identify Column indices of re-ordered columns

⎕av[.....] Use indices back in atomic vector to give enciphered text  

Contoh tangkapan layar dari test case:

⎕av[n[c⍳(⊂[2]((⍴t),3)⍴,⍉⊃(c←⊂[2]c,(,⍉9 3⍴c←9/⍳3),[1.1]27⍴⍳3)[(⎕av[n←(97+⍳26),33])⍳t←t,(3|⍴t←⎕)↓' '])]]
⎕:
'output'
rrvgivx  

Maukah Anda menambahkan tangkapan layar (satu atau beberapa) kasus uji? Saya tahu versi WIN APL tidak tersedia di TIO, tetapi saya masih ingin melihat semacam verifikasi, karena saya hampir tidak tahu bagaimana menafsirkan kode APL dengan hanya membacanya, apalagi memverifikasi tanpa menjalankannya. :)
Kevin Cruijssen

Tidak begitu yakin bagaimana melakukan itu, tetapi inilah yang akan terlihat. Saya akan menambahkan sesuatu ke entri di atas
Graham

Biasanya saya dapat menggunakan Dyalog Classic di TIO tetapi dalam hal ini vektor atomnya berada dalam urutan yang berbeda sehingga pengindeksan tidak akan berfungsi.
Graham

3

SAS, 305 byte

Sebuah 'oof' yang kuat untuk monster SAS ini. Ada banyak format string acak yang saya pikir saya bisa hindari membahas ini; Saya yakin ada cara yang lebih baik untuk melakukan ini.

data;input n:&$99.;n=tranwrd(trim(n)," ","{");if mod(length(n),3)=0then n=cats(n,'{');f=n;l=length(n);array a(999);do i=1to l;v=rank(substr(n,i,1))-97;a{i}=int(v/9);a{i+l}=mod(int(v/3),3);a{i+l*2}=mod(v,3);end;f='';do i=1to l*3by 3;f=cats(f,byte(a{i}*9+a{i+1}*3+a{i+2}+97));end;f=tranwrd(f,"{"," ");cards;

Input dimasukkan pada baris baru setelah pernyataan kartu, seperti:

data;input n:&$99.;n=tranwrd(trim(n)," ","{");if mod(length(n),3)=0then n=cats(n,'{');f=n;l=length(n);array a(999);do i=1to l;v=rank(substr(n,i,1))-97;a{i}=int(v/9);a{i+l}=mod(int(v/3),3);a{i+l*2}=mod(v,3);end;f='';do i=1to l*3by 3;f=cats(f,byte(a{i}*9+a{i+1}*3+a{i+2}+97));end;f=tranwrd(f,"{"," ");cards;
this is a trifid cipher
test
output
trifidcipher

Keluaran dataset yang berisi output dalam variabel f, bersama dengan sekelompok variabel pembantu / nilai array.

masukkan deskripsi gambar di sini

Tidak dikelompokkan / penjelasan:

data;
input n : & $99.; /* Read a line of input, maximum 99 characters */

n=tranwrd(trim(n)," ","{"); /* Replace spaces with '{' (this is the ASCII character following 'z', so it makes it easy to do byte conversions, and lets us not have to deal with spaces, which SAS does not like) */
if mod(length(n),3)=0then n=cats(n,'{'); /* If length of n is not coprime with 3, add an extra "space" to the end */

f=n; /* Set output = input, so that the string will have the same length */
l=length(n);    /* Get the length of the input */
array a(999);   /* Array of values to store intermediate results */

do i = 1 to l; /* For each character in the input... */
    v = rank(substr(n,i,1))-97; /* Get the value of the current character, from 0-26 */

    a{i}=int(v/9);          /* Get the table of the current character and store at appropriate index, from 0-2  */
    a{i+l}=mod(int(v/3),3); /* Get the row of the current character, from 0-2 */
    a{i+l*2}=mod(v,3);      /* Get the column of the current character, from 0-2  */
end;

f='';

do i = 1 to l*3 by 3; /* For each character in the output... */
    f=cats(f,byte(a{i}*9+a{i+1}*3+a{i+2}+97)); /* Convert values back from base 3 to base 10, and convert back into ASCII value */
end;

f = tranwrd(f,"{"," "); /* Replaces our "spaces" with actual spaces for final output */

/* Test cases */
cards;
this is a trifid cipher
test
output
trifidcipher

3

JavaScript (Node.js) ,  146 141 139  136 byte

I / O dalam huruf kecil.

s=>'931'.replace(/./g,d=>Buffer(s.length%3?s:s+0).map(c=>(o=(c>48?c-16:26)/d%3+o*3%27|0,++i)%3?0:(o+97)%123||32),i=o=0).split`\0`.join``

Cobalah online!

Berkomentar

s =>                       // s = input string
  '931'.replace(/./g, d => // for each digit d = 9, 3 and 1:
    Buffer(                //   create a buffer from:
      s.length % 3 ?       //     if the length of s is coprime with 3:
        s                  //       the original input string
      :                    //     else:
        s + 0              //       the input string + an extra '0'
    )                      //
    .map(c =>              //   for each ASCII code c from this Buffer:
      ( o =                //     update o:
        ( c > 48 ?         //       if c is neither a space nor the extra '0':
            c - 16         //         yield c - 16 (which gives 81 .. 106)
          :                //       else:
            26             //         this is the 26th character (space)
        ) / d % 3 +        //       divide by d and apply modulo 3
        o * 3 % 27 | 0,    //       add o * 3, apply modulo 27, coerce to integer
        ++i                //       increment i
      ) % 3 ?              //     if i mod 3 is not equal to 0:
        0                  //       yield 0 (NUL character)
      :                    //     else:
        (o + 97) % 123     //       convert o to the ASCII code of the output letter
        || 32              //       or force 32 (space) for the 26th character
    ),                     //   end of map()
    i = o = 0              //   start with i = o = 0
  ).split`\0`.join``       // end of replace(); remove the NUL characters

Saya pikir Anda pernah menjelaskannya sekali sebelumnya, tetapi bagaimana cara (o=...,++i)%3kerjanya di JS lagi? Apakah (o,i)tuple atau sesuatu, dan kedua bilangan bulat dalam dikonversi ke modulo-3 mereka? Sebagai pengembang Java, masih sedikit membingungkan saya untuk melihatnya (a,b)%c. Jawaban yang bagus! Saya suka bagaimana Anda mengonversi setiap digit ketiga, dan kemudian menghapus dua byte pertama nol. +1 dari saya.
Kevin Cruijssen

2
@KevinCruijssen Mengutip MDN : "Operator koma mengevaluasi setiap operandnya (dari kiri ke kanan) dan mengembalikan nilai operan terakhir. " Jadi, modulo hanya berlaku untuk ++i.
Arnauld

3

05AB1E , 25 byte

g3Öð׫SAð«3L3㩇ø˜3ô®Að«‡

Karena belum ada yang mengirim jawaban 05AB1E, saya pikir saya akan memposting solusi saya sendiri. Saya melihat sekarang ini sangat mirip dengan @ Dennis ♦ Jelly jawaban , meskipun saya datang dengan itu secara mandiri sebelum saya memposting tantangan.

Input sebagai string, output sebagai daftar karakter. Menambahkan satu spasi jika panjangnya dapat dibagi 3.

Cobalah secara online atau verifikasi semua kasus uji .

Penjelasan:

g3Ö         # Check if the length of the (implicit) input is divisible by 3
            # (results in 1 for truthy or 0 for falsey)
            #  i.e. "out" → 1
            #  i.e. "test" → 0
   ð×       # Repeat a space that many times
            #  i.e. 1 → " "
            #  i.e. 0 → ""
     «      # And append it to the (implicit) input
            #  i.e. "out" and " " → "out "
            #  i.e. "test" and "" → "test"
      S     # Then make the string a list of characters
            #  i.e. "out " → ["o","u","t"," "]
            #  i.e. "test" → ["t","e","s","t"]
A           # Push the lowercase alphabet
 ð«         # Appended with a space ("abcdefghijklmnopqrstuvwxyz ")
   3L       # Push list [1,2,3]
     3ã     # Cartesian repeated 3 times: [[1,1,1],[1,1,2],...,[3,3,2],[3,3,3]]
       ©    # Save that list of triplets in the registry (without popping)
           # Transliterate, mapping the letters or space to the triplet at the same index
            #  i.e. ["o","u","t"," "] → [[2,2,3],[3,1,3],[3,1,2],[3,3,3]]
            #  i.e. ["t","e","s","t"] → [[3,1,2],[1,2,2],[3,1,1],[3,1,2]]
ø           # Zip, swapping all rows/columns
            #  i.e. [[2,2,3],[3,1,3],[3,1,2],[3,3,3]] → [[2,3,3,3],[2,1,1,3],[3,3,2,3]]
            #  i.e. [[3,1,2],[1,2,2],[3,1,1],[3,1,2]] → [[3,1,3,3],[1,2,1,1],[2,2,1,2]]
 ˜          # Flatten the list
            #  i.e. [[2,3,3,3],[2,1,1,3],[3,3,2,3]] → [2,3,3,3,2,1,1,3,3,3,2,3]
            #  i.e. [[3,1,3,3],[1,2,1,1],[2,2,1,2]] → [3,1,3,3,1,2,1,1,2,2,1,2]
  3ô        # Split it into parts of size 3
            #  i.e. [2,3,3,3,2,1,1,3,3,3,2,3] → [[2,3,3],[3,2,1],[1,3,3],[3,2,3]]
            #  i.e. [3,1,3,3,1,2,1,1,2,2,1,2] → [[3,1,3],[3,1,2],[1,1,2],[2,1,2]]
®           # Push the triplets from the registry again
 Að«        # Push the lowercase alphabet appended with a space again
           # Transliterate again, mapping the triplets back to letters (or a space)
            # (and output the result implicitly)
            #  i.e. [[2,3,3],[3,2,1],[1,3,3],[3,2,3]] → ["r","v","i","x"]
            #  i.e. [[3,1,3],[3,1,2],[1,1,2],[2,1,2]] → ["u","t","b","k"]

3

Japt , 42 byte

;Êv3 ?UpS:U
m!bS=iC)®+27 ì3 ÅÃÕc ò3 £SgXì3

Cobalah online!

Inti dari jawaban ini berasal dari jawaban yang dihapus oleh Shaggy, tetapi ia tidak pernah kembali untuk menangani input dengan panjang 3 yang dapat habis dibagi jadi ini adalah versi tetap .

Penjelasan:

;                                 #Set C to the string "abcdefghijklmnopqrstuvwxyz"

 Ê                                #Get the length of the input
  v3 ?                            #If it is divisible by 3:
      UpS                         # Add a space
         :U                       #Otherwise don't add a space
                                  #Store the result in U

   S=iC)                          #Set S to C plus a space
m                                 #For each character in U:
 !bS                              # Get the position of that character in S
        ®        Ã                #For each resulting index:
             ì3                   # Convert to base 3
         +27    Å                 # Including leading 0s up to 3 places
                  Õ               #Transpose rows and columns
                   c              #Flatten
                     ò3           #Cut into segments of length 3
                        £         #For each segment:
                           Xì3    # Read it as a base 3 number
                         Sg       # Get the letter from S with that index

3

C # (Visual C # Interactive Compiler) , 178 byte

s=>{int[]a={9,3,1},b=(s.Length%3>0?s:s+0).Select(c=>c<97?26:c-97).ToArray();s="";for(int i=0,k,l=b.Length;i<l*3;s+=(char)(k>25?32:97+k))k=a.Sum(p=>b[i%l]/a[i++/l]%3*p);return s;}

Cobalah online!

Kurang golf ... Masih membingungkan :)

// s is an input string
s=>{
  // powers of 3
  int[]a={9,3,1},
  // ensure the length of s is coprime to 3
  // and convert to numbers from 0-26
  b=(s.Length%3>0?s:s+0).Select(c=>c<97?26:c-97).ToArray();
  // reset s to collect result
  s="";
  // main loop
  for(
    // i is the main index variable
    // k is the value of the encoded character
    // l is the length
    int i=0,k,l=b.Length;
    // i continues until it is 3x the length of the string
    i<l*3;
    // convert k to a character and append
    s+=(char)(k>25?32:97+k)
  )
    // compute the trifid
    // (this is the confusing part :)
    k=a.Sum(p=>b[i%l]/a[i++/l]%3*p);
  // return the result
  return s;
}
Dengan menggunakan situs kami, Anda mengakui telah membaca dan memahami Kebijakan Cookie dan Kebijakan Privasi kami.
Licensed under cc by-sa 3.0 with attribution required.