I tried this contest: https://www.hackerrank.com/challenges/secret-language Well, the score in this contest said I made 30 out of 30 points. As the score is calculated with the formula 30*(1000-#chars)/1000 this is impossible. Here is my code:

import sys,string as s
for l in sys.stdin:

The contest is over, so asking is no cheating but I would like to get some realistic score. I have 187 chars with newline and 195 with cr-lf. For pythoncode 24.39 (assuming newline) isn't so bad, but I think it can be done better.

For completeness here is the challenge:

Bob and Fred pass notes to each other in class all the time. However their teacher started intercepting their messages and reading the messages out loud to the class. Undeterred, Bob and Fred decided to encode their messages before they pass them so that when the message is intercepted the teacher and class will not understand it.

They got together and decided on using a simple substitution cipher. The substition cipher they agreed upon is listed below:


Using this key (Case matters!), A is changed to A, a is changed to a, B is changed to N, b is changed to n, C is changed to B, c is changed to b, … . All other characters remain the same.

However after Bob and Fred started using this method of note transfer they quickly realized that they are very slow at encoding and decoding messages. They have hired you to help them transfer messages faster by writing a program that will encode or decode messages using their substitution cipher. They want the code to be very portable so the smaller the source file is the better.


Input will consist of several lines. Each line will begin with either the string “ENCODE”, “DECODE”, or “DONE”. If the string is “ENCODE” or “DECODE” then a message will follow. This is the message that you will have to either encode (if the first string is “ENCODE”) or decode (if the first string is “DECODE”). Messages will contain spaces and punctuation but will always be on the same line. If the string is “DONE” then this signifies that Bob and Fred are done exchanging messages.


For each line that begins with “ENCODE” print the encoding of the message that follows.

For each line that begins with “DECODE” print the decoding of the message that follows.

Be sure to keep all capitolization and punctuation that is used in the message.

Sample Input

ENCODE This is a message. DECODE Zuej ej a gcjjadc. DONE

Sample Output

Zuej ej a gcjjadc. This is a message.


Your score will be 30 * (1000 - #characters) / 1000. The minimum score a correct solution will receive is 0.03.

  • \$\begingroup\$ A real test case would include a DONE followed by another message, which shouldn't be translated. \$\endgroup\$ – Peter Taylor Mar 28 '13 at 9:48

bash and friends (168 167 chars)

while read x&&[ "$x" != "DONE" ]
[ "$x" \> "E" ]
x=$(tr a-zA-Z `tr A-Z a-z<<<$T`$T<<<$x)
echo ${x:6}

The magic number isn't so magic: it's obtained by solving a set of simultaneous modular identities obtained from the cycle structure of the permutation.

| improve this answer | |
  • \$\begingroup\$ :O Impressive math... albeit simple, awesome too. \$\endgroup\$ – kaoD Mar 29 '13 at 5:32

GolfScript, 93 characters



>ENCODE This is a message.
>DECODE Zuej ej a gcjjadc.
>ENCODE This line is not processed

Zuej ej a gcjjadc.
This is a message.

Note: Due to the nature of i/o the input is read completely before processing and output starts.

| improve this answer | |

K, 95

while[~"DONE"~f:0:0;-1@7_f^$[f like"E*";d@;d?]f]


k)while[~"DONE"~f:0:0;-1@7_f^$[f like"E*";d@;d?]f]
ENCODE This is a message.
Zuej ej a gcjjadc.
DECODE Zuej ej a gcjjadc.
This is a message.
| improve this answer | |

Python, 159

from string import*
while'N'>s[2]:print translate(s[7:],maketrans(*(letters,n+upper(n))[::1-2*('E'>s)]));s=r()
| improve this answer | |
  • \$\begingroup\$ Thank you, I didn't expect raw_input to work as I always thought it must be commited by a CR(newline). Especially I like the trick with reversing the arguments for the maketrans method. \$\endgroup\$ – Johannes Maria Frank Mar 28 '13 at 14:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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