This Challenge, based upon a contest in which Rokicki came in 2nd, is to find sequences containing 1:N that require maximum iterations prior to the value 1 appearing in the first position.
The processing rule is that positions 1 thru Vector(1) are swapped. Processing stops when Vector(1) is 1.
Example Sequences:
[3 1 2], [2 1 3], [1 2 3] Score 2 [3 1 4 5 2][4 1 3 5 2][5 3 1 4 2][2 4 1 3 5][4 2 1 3 5][3 1 2 4 5][2 1 3 4 5][1 2 3 4 5]
Input: n (Integer from 1 to 31) (16 Actual Cases 2:11 13 17 19 23 29 31)
Output: Vector of values 1:n
Example:
Input: 5 Output: [3 1 4 5 2]
Score: 7
A minimum cumulative score of 531 for the 17 cases is required to Pass.
Final Score = 2531 - sum(scores)
Hints:
Usage of perms for 10 or higher may cause Cody Memory/Time issues. Random subsets are suggested for n>9.
Request: If Code is implemented external then please post as a block comment.
Faster Code Block than fliplr:
function count=process_seq(seq) count=0; while seq(1)>1 count=count+1; seq(1:seq(1))=seq(seq(1):-1:1); end end
Solution Stats
Problem Comments
Solution Comments
Show commentsProblem Recent Solvers10
Suggested Problems
-
Make the vector [1 2 3 4 5 6 7 8 9 10]
53504 Solvers
-
314 Solvers
-
Find the "ordinary" or Euclidean distance between A and Z
177 Solvers
-
Sum of first n terms of a harmonic progression
520 Solvers
-
middleAsColumn: Return all but first and last element as a column vector
652 Solvers
More from this Author306
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
Wonderful problem