# Code Golf: Gibberish Echo

This is my first code golf challenge, as such I am not entirely certain if it is suitable as a Code Golf challenge, but it's all a learning experience.

The challenge is that in as few characters as possible, using whichever language you prefer, develop a code to complete the following:

1. Take a value from stdin
2. Return a value to stdout that contains the same characters as the value at stdin in a different order. Not necessarily random, but following the restrictions set out below.

Here are a few restrictions:

• All characters that are contained in the stdin value must appear in stdout value
• Characters must appear in stdout the same amount of times they appear in stdin
• The order of characters in the output, cannot be identical to the order of characters in input, for more than two consecutive characters. Regardless of the source of the character in the input. That is, the input and output must not have a common substring of length 3 or greater.
• Any input where no solution can be found under the previous rule, is considered an invalid input

Examples: aaaa, bbbb

• you do not need to test for invalid inputs, assume all inputs are valid.

• The length of stdout must be the same as the length of stdin
• You may use any method to scramble the initial value, there is nothing off limits here.

Example:

stdin: Hello There

stout: loTeHle reh

Note: a character in this context can be any valid ASCII character

# CJam, 31 27 bytes

l:I{mr[_I]{_,((,\f>3f<}/&}g


Test it here.

Same approach as my Mathematica answer. It keeps generating random permutations of the input, until the substring restriction is satisfied. I think this can be golfed a bit further.

# Mathematica, 89 bytes

(For[c=Characters@#;d=c,#⋂#2&@@(Partition[#,3,1]&/@{c,d})!={},d=RandomSample@c];d<>"")&


Generates random permutations until there is the permutation and the original have no common substring of length 3.

• I don't think @"HelloThere" needs to be counted towards the size. – kennytm Jan 27 '15 at 13:58
• @KennyTM oh, thank you, totally forgot to remove that – Martin Ender Jan 27 '15 at 14:48

# CJam, 48 bytes

This is super huge. I am not sure if there are any good algorithms to do the task. This is just a brute force.

q:Q;{Q(+2/zs:R,3-_0>{,{RQI>3<#W>}fI}{;}?]1b0>}gR


Try it online here

• for aacc, the program returned acca, which unforetunately fails the third restriction. – Sam Weston Jan 27 '15 at 21:52