One day, you were just relaxing in your office in the CIA, when suddenly you see an alert on your computer. Your programs have just intercepted hundreds of coded messages! A quick examination reveals the rule for encoding, but you need a program in order to decode fast.


You will be given a list of strings, separated by commas. Each string will contain either:

  • Part of the coded message
    • It is part of the coded message if it is not in the form a=b. Note that it is part of the message if it is ab=c. Add this string to the coded message.
  • Part of the encoding scheme
    • This will be in the form of a=b. That means that all a's in the message must be replaced by b's. Note that it could be a==, meaning that all a`s must be replaced with ='s.

Your program must then output the message, decoded using the scheme found.

Other info: Your input will only contain commas for separating the strings. It could contain other characters, like !1#, etc. It will not contain uppercase letters. Bits of decoding info do not decode each other. Only the message is affected by the decoding information. Only one replacement will be given for each character, e.g. no "io,"i=u","i=g"


Input:"ta","y=s","y","a=e","b=t","b"," ","j","j=1"

Output:test 1





This is , so shortest answer in bytes wins!

  • \$\begingroup\$ oh yeah, sorry! editting \$\endgroup\$
    – Luke B
    Apr 5, 2017 at 21:13
  • \$\begingroup\$ What about transitivity and circularity, e.g. "massega","e=a","a=e" and the like? \$\endgroup\$ Apr 5, 2017 at 21:28
  • 1
    \$\begingroup\$ bits of decoding info do not decode each other. Only the message is affected by the decoding information. \$\endgroup\$
    – Luke B
    Apr 5, 2017 at 21:30
  • 1
    \$\begingroup\$ also, only one replacement will be given for each character, e.g. no "io,"i=u","i=g" \$\endgroup\$
    – Luke B
    Apr 5, 2017 at 21:32
  • 1
    \$\begingroup\$ Thanks, I suggest adding this information to the specification. \$\endgroup\$ Apr 5, 2017 at 21:33

9 Answers 9


Python 3, 98

lambda l:''.join(x*('='!=x[1:-1])for x in l).translate({'='!=x[1:-1]or ord(x[0]):x[2:]for x in l})

This lambda function receives a list of strings (input) and returns a string (the decoded message).


>>> f(['ta', 'y=s', 'y', 'a=e', 'b=t', 'b', ' ', 'j', 'j=1'])
'test 1'
>>> f(['z=p', 'zota', 'g=e', 'yugkb', 'y=t', 'u=o', 'k=s', 'li', 'fg', 'b=='])
>>> f(['p', '=', '==n', 'ot', 'p=a', 'hiz', 'i=e', 'z=r'])
  • \$\begingroup\$ Do you need :-1 or will :2 work? \$\endgroup\$
    – DSM
    Apr 6, 2017 at 0:35
  • 1
    \$\begingroup\$ @DSM I think it's needed because '=' == x[1:2] will be true for x = 'a=bc', which is not a part of the encoding scheme \$\endgroup\$
    – vaultah
    Apr 6, 2017 at 0:43
  • 1
    \$\begingroup\$ Ahh, good point! \$\endgroup\$
    – DSM
    Apr 6, 2017 at 0:44

Haskell, 85 bytes

f x=[(a,b)|[a,'=',b]<-x]
h x=map(\v->maybe v id$lookup v$f x)$concat[c|c<-x,[]==f[c]]


>h ["p","=","==n","ot","p=a","hiz","i=e","z=r"]


f creates a lookup table.

concat[c|c<-x,[]==f[c]] extracts the message.

map(\v->maybe v id$lookup v$f x) perfoms the lookup.


JavaScript (ES6), 87 bytes


<input id=a oninput="try{b.innerText=((l,s='',d={})=>l.map(v=>/.=./.test(v)?d[v[0]]=v[2]:s+=v)&&[...s].map(c=>d[c]||c).join``)(eval(`[${a.value}]`))}catch(e){}"/>
<p id=b />


Jelly, 19 bytes


Try it online!


ḊṖ⁼“=” - Link 1, isMappngElement?: string
Ḋ      - dequeue
 Ṗ     - pop
   “=” - char-list,['=']
  ⁼    - equal?

ÇÐfKm2yÇÐḟF$ - Main link: list of strings
 Ðf          - filter keep:
Ç            -     last link (1) as a monad
   K         - join with spaces
    m2       - modulo 2 slice (every other character)
           $ - last two links as a monad:
        Ðḟ   -     filter discard:
       Ç     -         last link (1) as a monad
          F  -     flatten
      y      - translate right according to the mapping of left
  • \$\begingroup\$ Very interesting, what does m2 "modulo 2 slice" mean? \$\endgroup\$ Apr 6, 2017 at 17:06
  • \$\begingroup\$ m is a dyadic atom that takes every right-th element of left*. Here, for example, ['x','=','y','<space>','a','=','b']m2 would yield ['x','y','a','b']. (* unless right is zero when it appends a reflection instead.) \$\endgroup\$ Apr 6, 2017 at 17:15

Retina, 84 82 77 74 bytes

Takes a comma-separated list as input. Note the trailing newline


Try it Online!


First, we move all expressions of the form .=. to the end of the string and separate these from the message with a double comma (,,). This is so that in the next step, we can find all encodings by checking if each character ahead of the ,, has a matching =. afterwards. This is achieved by M!&`(.).*,,.*\1=.|.+, which finds all such matches and places them into a linefeed separated list of strings. We then modify each string to only contain either one unencoded character or the encoded version of the character. Finally we replace all linefeeds and commas with the empty string so that our output is formatted nicely.


Batch, 188 bytes

@echo off
set c=
for %%s in (%m%)do call:c %%s "%%c%%%%~s" not
for %%s in (%m%)do call:c %%s "%%c:%%~s%%"
echo %c%
set s=%~1
if %3 "%s:~1,1%"=="=" call set c=%~2

Explanation: Loops through the list of strings twice (conveniently for likes a string in CSV format). The first time, looks for strings that do not contain an = as the second character, and concatenates them to the result. The second time, looks for strings that do contain an = as the second character, and performs the substitution. (even more conveniently, the substitution is already in Batch format.)


PHP, 116 Bytes

<?foreach($_GET as$i)$i[1]=="="&&strlen($i)==3?$r[]=$i:$s.=$i;foreach($r as$l)$s=str_replace($l[0],$l[2],$s);echo$s;

Online Version


PHP, 89 87 bytes

two versions:

while(a&$s=$argv[++$i])3==strlen($s)&"="==$s[1]?$t[$s[0]]=$s[2]:$m.=$s;echo strtr($m,$t);
while(a&$s=$argv[++$i])preg_match("#^.=.$#",$s)?$t[$s[0]]=$s[2]:$m.=$s;echo strtr($m,$t);

takes input from command line arguments; run with -nr.

  • loop through arguments creating parameters for strtr
    (translation if argument contains =, message else).
  • perform strtr.

05AB1E, 31 bytes


Try it online!

vy              }
  '=åyg3Q&                      # See if it's length 3 with an equals in the mid.
          iyˆyS                 # If so, add to global array, split into chars.
                 )øJÁ¨`         # Transpose, join, and remove the middle =.
                       ¹¯KJ     # Push original input without decryption key.
                           .Ás‡ # Shift stack to the right, swap and transliterate.

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.