You must write a program that encrypts an input into a string of characters.

For every character i in the input up to the second to last character, take the i and i+1 characters and encode them by writing the letters of the alphabet, in order, that range in the same direction between those chosen characters.

For example: if the original string were bo then it would be encoded as cdefghijklmn, but if the string were boa then bo is encoded as cdefghijklmn and oa is encoded as nmlkjihgfedcb with the final encrypted string being cdefghijklmnnmlkjihgfedcb.

If the original string contain zero letters between two chosen characters, such as the string ab then you should encrypt it as aRb with R standing for what direction in the alphabet to go in determining the original characters. The encrypted string aRb represents ab but the encrypted string aLb represents ba (R = right, L = left).

If two chosen characters in the original string are the same, such as the string tt, then you should encrypt it as tZt with Z standing for zero letters in between.


Input: att Output: bcdefghijklmnopqrstZt

Input: brep Output: cdefghijklmnopqqponmlkjihgffghijklmno

Input: Optimizer Output: oRpqrssrqponmlkjjkllkjjklmnopqrstuvwxyyxwvutsrqponmlkjihgffghijklmnopq


  • You can write a program or a function.
  • Your input may contain uppercase letters, so you need to put them all to lowercase before encrypting.
  • You can ignore non-alphabet characters.
  • One character length strings are undefined.
  • Shortest code in bytes wins!
  • 1
    \$\begingroup\$ I'm not sure I quite get all the rules - what's the expected output for aabbcfcBBAa? \$\endgroup\$
    – Sp3000
    Jul 22, 2015 at 1:22
  • \$\begingroup\$ Is "ab" encrypted as "aZb"? Is "aa" encrypted as "aZa"? \$\endgroup\$ Jul 22, 2015 at 1:25
  • \$\begingroup\$ @Sp3000 aZa aRb bZb bRc de ed cRb bZb bLa aZa without any spaces. \$\endgroup\$
    – jado
    Jul 22, 2015 at 1:27
  • \$\begingroup\$ @MtnViewMark ab = aRb, aa = aZa \$\endgroup\$
    – jado
    Jul 22, 2015 at 1:27
  • \$\begingroup\$ What should the output be for the input a? \$\endgroup\$
    – Doorknob
    Jul 22, 2015 at 2:36

7 Answers 7


Pyth, 38 bytes



                                          Implicit: z = input(), Z = 0
                                          g is a function of a 2 letter string
                                          and an insert letter.
M                                         def g(G, H): return
    rFG                                   Take the string range between the
                                          letters in G.
   t                                      Remove the first letter to get just
                                          the letters in between.
  s                                       Concatenate.
 |                                        Logical or (if nothing in between)
       jHG                                H.join(G)
                                  rzZ     Convert input to lowercase.
                                .:   2    Take 2 letter substrings.
           m                              Map d over the substrings
            ?>Fd                          If the first character of `d`
                                          is greater than the second,
                 _g_\L                    g(d[::-1], 'L')[::-1]
                      gd                  else, g(d,
                        ?qFd              if the letters of d are the same,
                            \Z            'Z'
                              \R          else 'R')
          s                               Concatenate and print.

C - 161 Bytes - GCC

#define y(x) putchar(x)

Will update when I get it shorter


JavaScript ES6, 253 bytes


Just by glancing you can see all the places where this can be golfed. I can probably chop off 100 bytes.


[...`${d}`.toLowerCase()].map((l,i,a, m=a[i-1],
     l==m?         l+'Z'+l:
     c(m)+1==c(l)? m+'R'+l:
     c(l)+1==c(m)? l+'L'+m:
     c(m)>c(l)?    [...d(l,m)].reverse().join``:
     c(l)>c(m)?    d(m,l):  0:

You can clearly see where this can be golfed in the ?: sequence.

ES5 Snippet:

function t(d) {
    var y = 'abcdefghijklmnopqrstuvwxyz';
    return (d+'').toLowerCase().split('').map(function (l,i,a) {
        var m = a[i - 1],
            c = function (s) {
                return y.search(s)
            d = function (s,e) {
                return y.slice( c(s), c(e) ).slice(1);
       return m ? 
              l == m           ? l + 'Z' + l :
              c(m) + 1 == c(l) ? m + 'R' + l :
              c(l) + 1 == c(m) ? l + 'L' + m :
              c(m) > c(l)      ? d(l,m).split('').reverse().join('') :
              c(l) > c(m)      ? d(m,l)                              :
              0 : ''

// Demo
  document.getElementById('output').innerHTML = t(document.getElementById('input').value)
<div style="padding-left:5px;padding-right:5px;"><h2 style="font-family:sans-serif">Encoding Snippet</h2><div><div  style="background-color:#EFEFEF;border-radius:4px;padding:10px;"><input placeholder="Text here..." id="input"><button id='go'>Encode!</button></div><br><div style="background-color:#EFEFEF;border-radius:4px;padding:10px;"><span style="font-family:sans-serif;">Output:</span><br><pre id="output" style="background-color:#DEDEDE;padding:1em;border-radius:2px;overflow-x:auto;"></pre></div></div></div>

  • \$\begingroup\$ By setting y, can you use a different alphabet/set of characters? \$\endgroup\$
    – jado
    Jul 22, 2015 at 2:23
  • \$\begingroup\$ @phase yeah, that contains all the characters the program uses. \$\endgroup\$
    – Downgoat
    Jul 22, 2015 at 2:26

Python 343 334 bytes

import sys,re,string
for l in sys.stdin:
 for i,c in enumerate(s[:-1]):o=s[i+1:i+2]or c;b,d=a.index(c),a.index(o);L.extend(j(e[1])for e in[(c==o,[c,'Z',c]),(c<o,(a[b+1:d],[c,'R',o])[b+1==d]),(c>o,(a[d+1:b][::-1],[c,'L',o])[b-1==d])]if e[0])

Code on ideone.

First golf ever. Any suggestions would be great.

  • \$\begingroup\$ Welcome to the site! A couple of suggestions: Using semicolons to separate your lines instead of newlines is typically shorter, as it avoids indentation. Also, you can alias ''.join to something shorter, like j, which saves bytes because you use it twice. Take a look at this page if you want more tips. \$\endgroup\$
    – isaacg
    Jul 22, 2015 at 11:12
  • \$\begingroup\$ Hi, thanks! How would I go about aliasing a built-in's method invocation to save space? For example this is illegal in all pythons str.j=str.join since 'str' is a built-in type. I could do something like j=lambda L:''.join(L) (21 bytes), but the definition alone is as twice as long as ''.join(L) (10 bytes). \$\endgroup\$
    – Winny
    Jul 22, 2015 at 11:23
  • \$\begingroup\$ It's very simple: j=''.join \$\endgroup\$
    – isaacg
    Jul 22, 2015 at 11:29
  • \$\begingroup\$ Super! I overthought that one! Thanks a bunch! \$\endgroup\$
    – Winny
    Jul 22, 2015 at 11:33

K5, 77 bytes



Explanation coming after more golfing.


Haskell, 168 bytes

import Data.Char
b#a|a==b=[a,'Z',b]|s a==b=[a,'R',b]|a==s b=[a,'L',b]|a>b=reverse$a#b|a<b=[s a..pred b]
e=concat.(tail>>=zipWith(#)).map toLower.filter isLetter


λ: e "att"
λ: e "brep"
λ: e "Optimizer"
λ: e "aabbcfcBBAa"
  • \$\begingroup\$ Nice. 1 byte to save: use both id=<< instead of concat and a list comprehension instead of map and filter: e x=id=<<(tail>>=zipWith(#))[toLower i|i<-x,isLetter i] \$\endgroup\$
    – nimi
    Jul 24, 2015 at 12:55

Haskell, 185 bytes

import Data.Char
f(a:b:c)|b==s a=q 'R'|a==s b=q 'L'|a==b=q 'Z'|a<b=[s a..p b]++n|a>b=[p a,p$p a..s b]++n where q m=a:m:b:n;n=f(b:c)
f _=[]
g x=f[toLower e|e<-x,isLetter e]

Usage: g "brep" -> "cdefghijklmnopqqponmlkjihgffghijklmno".

How it works: g throws away all non-letters and converts the letters to lower case and calls f to do the encryption. Depending on the first two characters f inserts R, L or Z or builds the sequence in-between and and appends the result of a recursive call of itself with the tail of the input.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.