5
\$\begingroup\$

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

\$\endgroup\$
0

3 Answers 3

3
\$\begingroup\$

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.

\$\endgroup\$
2
\$\begingroup\$

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.

\$\endgroup\$
2
  • \$\begingroup\$ I don't think @"HelloThere" needs to be counted towards the size. \$\endgroup\$
    – kennytm
    Jan 27, 2015 at 13:58
  • \$\begingroup\$ @KennyTM oh, thank you, totally forgot to remove that \$\endgroup\$ Jan 27, 2015 at 14:48
0
\$\begingroup\$

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

\$\endgroup\$
1
  • \$\begingroup\$ for aacc, the program returned acca, which unforetunately fails the third restriction. \$\endgroup\$
    – Sam Weston
    Jan 27, 2015 at 21:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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