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:
- Take a value from stdin
- 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
3 Answers 3
CJam, (削除) 31 (削除ここまで) 27 bytes
l:I{mr[_I]{_,((,\f>3f<}/&}g
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.
-
\$\begingroup\$ I don't think
@"HelloThere"needs to be counted towards the size. \$\endgroup\$kennytm– kennytm2015年01月27日 13:58:50 +00:00Commented Jan 27, 2015 at 13:58 -
\$\begingroup\$ @KennyTM oh, thank you, totally forgot to remove that \$\endgroup\$Martin Ender– Martin Ender2015年01月27日 14:48:45 +00:00Commented Jan 27, 2015 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
-
\$\begingroup\$ for aacc, the program returned acca, which unforetunately fails the third restriction. \$\endgroup\$Sam Weston– Sam Weston2015年01月27日 21:52:57 +00:00Commented Jan 27, 2015 at 21:52