You work for the military as a coder and are responsible for writing software to control missiles. Your task is to write a program or function that will output a code sequence (any length ASCII string) to launch a missile. The program should be based on a loop taking the code sequence from memory and output it in a particular order. Then the code is sent to the missile controller, which launches the missile.

However, you work as one of our spies. We believe that writing the sequence in reverse order will cause the missile to explode in the silo as a form of sabotage! We are able to change by some way (the method is top secret and it is not important how we can do it) one byte of code in the missile controller software. You need to write such a program that changing one byte during the loop will change the loop so that the text will be typed in reverse order.

The task

Write a program or function in any language of your choice that will use a loop to return ASCII sequence. The sequence can be stored in the memory or given from stdin or equivalent, function arguments, being a string or array of any length (>0, <=255 characters).

Changing one character or byte in the loop, in any iteration, should reverse the loop direction, making characters that have already been returned show again, from last to first.

For example, if the input text (the missile code sequence) would be abcdefgh and the loop switch begins in the 5th iteration, the result should be abcdedcba. We believe this would be enough to destroy the missiles.

However, we know that they are aware of our new weapon. We are not able to switch from or to any of the following characters, in your code:

  • + (plus sign, addition)
  • - (minus sign, subtraction, or unary negative operator)
  • > and < (greater than, less than), we also expect this won't work for << and >> operators (bit shift left/right), however you can change commands doing bit shift, if there are any in your language (eg. change shr to shl).

You may not change a whitespace character to a non-whitespace character (eg. you cannot change [space]or to xor).

You may not assign any constants or variables to values or codes so you will bypass the said characters (e.g. you cannot say p = 1; n = -1; and change p to n in such a line i = i + p).

If necessary, after the character switch, you are allowed to leave the loop and output the rest of the string in any way you like.

You are free to choose if one and only one character/byte will be switched or all of the same value. For example, you may switch all occurrences of * to /.

Any GOTO, DJNZ and equivalents also do count as loops.

The program must return no errors, so the enemy will never realize what has happened. The program should return the whole string if no switch would be made.

This is a codegolf, so shortest answer in bytes wins.

To make program simpler, you may insert in your code a conditional statement to switch program flow in particular iteration. This must simulate the line to be changed. If so, you do not have to count the bytes of the conditional in your code. However, you cannot modify anything in this part of code and use it for any other purposes.


The example below was written in PHP. Note, it is invalid, because it changes + sign to - sign, which is not allowed, however I hope it would be clear to understand what I mean.

function f($s){
   $result = ''; // initialize result
   $array = str_split($s); // convert string to array
   $char = 0; // loop start
   $increase = 1; // loop direction
   while($char>=0 && $char < strlen($s)){ // start loop
     $result = $result . $array[$char]; // add next character for the output

     // The conditional statements below simulate character switch
     // The number 5 means it will happen in the 5th iteration
     // The lines in if... and else... statements must have exactly the same length
     if(strlen($result) < 5){ // for iteration 5 and lower...
       $char = $char + $increase;  // count only this line in the block
       $char = $char - $increase;
     // end of the block

   } // end while loop
   return $result; // end of program

The usage of the function is to call it echo f("abcdefghijkl"); which returns abcdedcba. Golfed a bit (removed some whitespaces and the conditional block):

function f($s){$result='';$array=str_split($s);$char=0;$increase=1;while($char>=0 && $char<strlen($s)){
$char=$char+$increase; // this line instead of the block

(175 bytes)

Note, that the golfed version, without the conditional block (or 5 is replaced with 13 or more), returns abcdefghijkl.

This is my very first task here. Let me know if I did something wrong.

  • \$\begingroup\$ The main task must be done in a loop. You must return the result string by copying it one by one character. After the character change, in any possible iteration, you are allowed to return the reversed part using reverse() method or anything like this (then no loop is necessary, but you have to quit it somehow). \$\endgroup\$
    – Voitcus
    Commented Aug 17, 2015 at 21:28
  • \$\begingroup\$ You may not change a whitespace character to a non-whitespace character does this mean i can change non-whitespace to a whitespace? \$\endgroup\$
    – Kamehameha
    Commented Aug 18, 2015 at 5:46
  • \$\begingroup\$ @Kamehameha yes, you can (you can make xor to [space]or or 12 to \n2) \$\endgroup\$
    – Voitcus
    Commented Aug 18, 2015 at 5:48
  • \$\begingroup\$ I thought for a second we could choose our own sequence … R.I.P, echo 'a'; \$\endgroup\$ Commented Aug 18, 2015 at 7:05

1 Answer 1


Python 76 63 Bytes

This anon-function basically prints it's input as is.

lambda x:`[x[(~~e,e)[e<len(x)/2]]for e in range(len(x))]`[2::5]

Outputs - z("123456") -> 123456

But by changing the ~ to whitespace , it prints the second part of the string in reverse.

lambda x:`[x[( ~e,e)[e<len(x)/2]]for e in range(len(x))]`[2::5]

Outputs - z("123456") -> 123321

  • \$\begingroup\$ The output of the first function should have the string only once (z("123456") -> 123456), exactly as it was given. Also, number 6 should be returned once, as in my examples (however it is not clearly stated in the question, so I will accept if you return two 6s) \$\endgroup\$
    – Voitcus
    Commented Aug 18, 2015 at 6:25
  • \$\begingroup\$ @Voitcus check the edit \$\endgroup\$
    – Kamehameha
    Commented Aug 18, 2015 at 6:37

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.