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

# Solution 243212

Submitted on 13 May 2013 by Richard Zapor

Correct

1422Size
`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
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')
```
```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')
```
```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')
```
```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')
```
```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')
```
```count 10
5 6 4 1 3 2
```
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')
```
```count 16
4 7 6 2 1 5 3
```
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')
```
```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')
```
```count 30
6 1 5 9 7 2 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')
```
```count 34
4 9 8 6 10 3 1 7 2 5
```
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')
```
```count 47
3 5 9 1 6 10 11 8 7 2 4
```
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')
```
```count 62
12 2 9 5 1 10 8 11 3 13 7 4 6
```
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')
```
```count 99
2 13 10 7 14 1 9 11 5 12 16 8 3 6 17 4 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')
```
```count 109
4 18 6 13 7 3 16 2 15 17 12 10 5 1 14 11 8 19 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')
```
```count 156
13 17 4 16 22 20 8 7 9 10 6 19 14 11 1 21 18 5 12 2 15 3 23
```
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')
```
```count 232
27 16 4 17 12 7 1 2 28 11 14 26 18 15 19 9 22 13 25 5 20 10 24 21 8 3 6 23 29
```
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
```
```count 278
16 25 2 23 18 4 30 29 24 26 13 12 8 17 27 22 14 20 11 21 7 10 3 6 9 31 15 5 19 1 28
Elapsed time is 50.313490 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 50.331187 seconds.
```