Cody

# Problem 1506. Maximum Intra-Vector Swaps

Solution 283257

Submitted on 16 Jul 2013
This solution is locked. To view this solution, you need to provide a solution of the same size or smaller.

### Test Suite

Test Status Code Input and Output
1   Pass
tic feval(@assignin,'caller','score',2000);

2   Fail
%% % 2 1 global cseq cseq{1}=1; % Gift answer max_seq=find_max_swap_seq(2); assert(isequal(1:2,unique(max_seq))) cseq{2}=max_seq; seq=max_seq; count=0; while seq(1)>1 count=count+1; seq(1:seq(1))=seq(seq(1):-1:1); end fprintf('count %i\n',count) fprintf('%i ',max_seq) fprintf('\n')

Error: Output argument "seq" (and maybe others) not assigned during call to "/users/msssystem10/find_max_swap_seq.m>find_max_swap_seq".

3   Pass
%% % 3 2 global cseq max_seq=find_max_swap_seq(3); assert(isequal(1:3,unique(max_seq))) cseq{3}=max_seq; seq=max_seq; count=0; while seq(1)>1 count=count+1; seq(1:seq(1))=seq(seq(1):-1:1); end fprintf('count %i\n',count) fprintf('%i ',max_seq) fprintf('\n')

[Warning: Polynomial is not unique; degree >= number of data points.] [> In polyfit at 71 In find_max_swap_seq at 5 In verifyCode>evaluateCode at 227 In verifyCode at 40 In fevalJSON at 14 In workspacefunc at 7] count 2 3 1 2

4   Pass
%% % 4 4 global cseq max_seq=find_max_swap_seq(4); assert(isequal(1:4,unique(max_seq))) cseq{4}=max_seq; seq=max_seq; count=0; while seq(1)>1 count=count+1; seq(1:seq(1))=seq(seq(1):-1:1); end fprintf('count %i\n',count) fprintf('%i ',max_seq) fprintf('\n')

[Warning: Polynomial is not unique; degree >= number of data points.] [> In polyfit at 71 In find_max_swap_seq at 5 In verifyCode>evaluateCode at 227 In verifyCode at 40 In fevalJSON at 14 In workspacefunc at 7] count 4 3 1 4 2

5   Pass
%% % 5 7 global cseq max_seq=find_max_swap_seq(5); assert(isequal(1:5,unique(max_seq))) cseq{5}=max_seq; seq=max_seq; count=0; while seq(1)>1 count=count+1; seq(1:seq(1))=seq(seq(1):-1:1); end fprintf('count %i\n',count) fprintf('%i ',max_seq) fprintf('\n')

[Warning: Polynomial is not unique; degree >= number of data points.] [> In polyfit at 71 In find_max_swap_seq at 5 In verifyCode>evaluateCode at 227 In verifyCode at 40 In fevalJSON at 14 In workspacefunc at 7] count 7 3 1 4 5 2

6   Pass
%% % 6 10 global cseq max_seq=find_max_swap_seq(6); assert(isequal(1:6,unique(max_seq))) cseq{6}=max_seq; seq=max_seq; count=0; while seq(1)>1 count=count+1; seq(1:seq(1))=seq(seq(1):-1:1); end fprintf('count %i\n',count) fprintf('%i ',max_seq) fprintf('\n')

[Warning: Polynomial is not unique; degree >= number of data points.] [> In polyfit at 71 In find_max_swap_seq at 5 In verifyCode>evaluateCode at 227 In verifyCode at 40 In fevalJSON at 14 In workspacefunc at 7] count 10 4 1 5 2 6 3

7   Pass
%% % 7 16 global cseq max_seq=find_max_swap_seq(7); assert(isequal(1:7,unique(max_seq))) cseq{7}=max_seq; seq=max_seq; count=0; while seq(1)>1 count=count+1; seq(1:seq(1))=seq(seq(1):-1:1); end fprintf('count %i\n',count) fprintf('%i ',max_seq) fprintf('\n')

[Warning: Polynomial is not unique; degree >= number of data points.] [> In polyfit at 71 In find_max_swap_seq at 5 In verifyCode>evaluateCode at 227 In verifyCode at 40 In fevalJSON at 14 In workspacefunc at 7] count 14 6 4 1 2 7 3 5

8   Pass
%% % 8 22 global cseq max_seq=find_max_swap_seq(8); assert(isequal(1:8,unique(max_seq))) cseq{8}=max_seq; seq=max_seq; count=0; while seq(1)>1 count=count+1; seq(1:seq(1))=seq(seq(1):-1:1); end fprintf('count %i\n',count) fprintf('%i ',max_seq) fprintf('\n')

[Warning: Polynomial is not unique; degree >= number of data points.] [> In polyfit at 71 In find_max_swap_seq at 5 In verifyCode>evaluateCode at 227 In verifyCode at 40 In fevalJSON at 14 In workspacefunc at 7] count 20 2 6 7 1 8 5 4 3

9   Pass
%% % 9 30 global cseq max_seq=find_max_swap_seq(9); assert(isequal(1:9,unique(max_seq))) cseq{9}=max_seq; seq=max_seq; count=0; while seq(1)>1 count=count+1; seq(1:seq(1))=seq(seq(1):-1:1); end fprintf('count %i\n',count) fprintf('%i ',max_seq) fprintf('\n')

[Warning: Polynomial is not unique; degree >= number of data points.] [> In polyfit at 71 In find_max_swap_seq at 5 In verifyCode>evaluateCode at 227 In verifyCode at 40 In fevalJSON at 14 In workspacefunc at 7] count 25 7 9 8 5 4 1 3 2 6

10   Pass
%% %10 38 global cseq max_seq=find_max_swap_seq(10); assert(isequal(1:10,unique(max_seq))) cseq{10}=max_seq; seq=max_seq; count=0; while seq(1)>1 count=count+1; seq(1:seq(1))=seq(seq(1):-1:1); end fprintf('count %i\n',count) fprintf('%i ',max_seq) fprintf('\n')

[Warning: Polynomial is not unique; degree >= number of data points.] [> In polyfit at 71 In find_max_swap_seq at 5 In verifyCode>evaluateCode at 227 In verifyCode at 40 In fevalJSON at 14 In workspacefunc at 7] count 31 6 10 5 7 8 3 2 4 1 9

11   Pass
%% %11 51 global cseq max_seq=find_max_swap_seq(11); assert(isequal(1:11,unique(max_seq))) cseq{11}=max_seq; seq=max_seq; count=0; while seq(1)>1 count=count+1; seq(1:seq(1))=seq(seq(1):-1:1); end fprintf('count %i\n',count) fprintf('%i ',max_seq) fprintf('\n')

[Warning: Polynomial is not unique; degree >= number of data points.] [> In polyfit at 71 In find_max_swap_seq at 5 In verifyCode>evaluateCode at 227 In verifyCode at 40 In fevalJSON at 14 In workspacefunc at 7] count 34 5 11 1 10 6 2 3 4 8 7 9

12   Pass
%% %13 80 case 12 global cseq max_seq=find_max_swap_seq(13); assert(isequal(1:13,unique(max_seq))) cseq{12}=max_seq; seq=max_seq; count=0; while seq(1)>1 count=count+1; seq(1:seq(1))=seq(seq(1):-1:1); end fprintf('count %i\n',count) fprintf('%i ',max_seq) fprintf('\n')

[Warning: Polynomial is not unique; degree >= number of data points.] [> In polyfit at 71 In find_max_swap_seq at 5 In verifyCode>evaluateCode at 227 In verifyCode at 40 In fevalJSON at 14 In workspacefunc at 7] count 51 11 4 7 12 5 1 13 3 9 8 2 6 10

13   Pass
%% %17 159 case 13 global cseq max_seq=find_max_swap_seq(17); assert(isequal(1:17,unique(max_seq))) cseq{13}=max_seq; seq=max_seq; count=0; while seq(1)>1 count=count+1; seq(1:seq(1))=seq(seq(1):-1:1); end fprintf('count %i\n',count) fprintf('%i ',max_seq) fprintf('\n')

[Warning: Polynomial is not unique; degree >= number of data points.] [> In polyfit at 71 In find_max_swap_seq at 5 In verifyCode>evaluateCode at 227 In verifyCode at 40 In fevalJSON at 14 In workspacefunc at 7] count 80 10 16 1 17 2 3 14 11 4 5 7 9 6 12 8 13 15

14   Pass
%% %19 221 case 14 global cseq max_seq=find_max_swap_seq(19); assert(isequal(1:19,unique(max_seq))) cseq{14}=max_seq; seq=max_seq; count=0; while seq(1)>1 count=count+1; seq(1:seq(1))=seq(seq(1):-1:1); end fprintf('count %i\n',count) fprintf('%i ',max_seq) fprintf('\n')

[Warning: Polynomial is not unique; degree >= number of data points.] [> In polyfit at 71 In find_max_swap_seq at 5 In verifyCode>evaluateCode at 227 In verifyCode at 40 In fevalJSON at 14 In workspacefunc at 7] count 109 17 6 5 8 16 9 15 2 3 12 4 1 11 10 13 19 7 18 14

15   Pass
%% %23 382 case 15 global cseq max_seq=find_max_swap_seq(23); assert(isequal(1:23,unique(max_seq))) cseq{15}=max_seq; seq=max_seq; count=0; while seq(1)>1 count=count+1; seq(1:seq(1))=seq(seq(1):-1:1); end fprintf('count %i\n',count) fprintf('%i ',max_seq) fprintf('\n')

[Warning: Polynomial is not unique; degree >= number of data points.] [> In polyfit at 71 In find_max_swap_seq at 5 In verifyCode>evaluateCode at 227 In verifyCode at 40 In fevalJSON at 14 In workspacefunc at 7] count 159 15 6 2 3 12 13 8 11 16 4 1 23 5 9 14 22 18 7 19 17 10 21 20

16   Pass
%% %29 689 case 16 global cseq max_seq=find_max_swap_seq(29); assert(isequal(1:29,unique(max_seq))) cseq{16}=max_seq; seq=max_seq; count=0; while seq(1)>1 count=count+1; seq(1:seq(1))=seq(seq(1):-1:1); end fprintf('count %i\n',count) fprintf('%i ',max_seq) fprintf('\n')

[Warning: Polynomial is not unique; degree >= number of data points.] [> In polyfit at 71 In find_max_swap_seq at 5 In verifyCode>evaluateCode at 227 In verifyCode at 40 In fevalJSON at 14 In workspacefunc at 7] count 194 11 6 15 7 16 4 22 23 25 21 12 29 28 26 13 19 1 10 18 24 2 14 9 8 27 5 3 17 20

17   Pass
%% %31 819 case 17 global cseq max_seq=find_max_swap_seq(31); assert(isequal(1:31,unique(max_seq))) cseq{17}=max_seq; seq=max_seq; count=0; while seq(1)>1 count=count+1; seq(1:seq(1))=seq(seq(1):-1:1); end fprintf('count %i\n',count) fprintf('%i ',max_seq) fprintf('\n') toc

[Warning: Polynomial is not unique; degree >= number of data points.] [> In polyfit at 71 In find_max_swap_seq at 5 In verifyCode>evaluateCode at 227 In verifyCode at 40 In fevalJSON at 14 In workspacefunc at 7] count 238 6 28 2 4 19 7 27 12 18 29 30 15 8 23 24 1 21 3 26 16 5 11 14 25 10 31 22 9 20 13 17 Elapsed time is 11.799868 seconds.

18   Fail
%% global cseq total=0; for i=2:17 seq=cseq{i}; count=0; while seq(1)>1 count=count+1; seq(1:seq(1))=seq(seq(1):-1:1); end total=total+count; end % 2531 is optimal sum total=sum([0 1 2 4 7 10 16 22 30 38 51 80 159 221 382 689 819])-total; assert(total<2001); % Minimum performance requirement toc feval(@assignin,'caller','score',min(2000,total));

Error: Index exceeds matrix dimensions.