Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Solution 322134

Submitted on 22 Sep 2013 by Alfonso Nieto-Castanon

Correct

1412Size
Leading solution size is 769.
This solution is locked. To view this solution, you need to provide a solution of the same size or smaller.

Test Suite

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

                    
2
Pass
 
%%
% 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')
1
count 1
2 1
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')
2
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')
4
count 4
2 4 1 3
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')
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')
10
count 10
4 5 6 2 1 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')
16
count 16
3 1 4 6 7 5 2
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')
22
count 22
6 1 5 7 8 3 2 4
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')
29
count 29
6 1 5 9 2 7 8 3 4
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')
34
count 34
3 4 2 5 9 8 1 10 6 7
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')
42
count 42
2 6 4 10 1 3 7 11 8 5 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')
60
count 60
4 7 5 13 12 3 11 10 1 2 9 6 8
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')
115
count 115
10 13 12 2 9 8 4 15 16 6 7 11 3 17 5 1 14
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')
114
count 114
7 5 8 18 6 2 4 3 14 15 10 1 12 19 13 17 11 16 9
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')
166
count 166
7 23 8 18 11 16 12 6 4 22 9 19 17 21 15 2 3 10 13 1 5 20 14
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')
223
count 223
17 14 10 21 24 13 12 25 1 22 29 27 19 18 26 28 3 5 11 2 16 20 4 15 7 6 8 23 9
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
274
count 274
13 8 20 28 3 27 29 31 1 14 5 12 7 21 11 10 22 9 15 24 26 6 16 17 2 18 23 25 4 30 19
Elapsed time is 22.566910 seconds.
18
Pass
 
%%
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));
Elapsed time is 22.583065 seconds.