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 425152

Submitted on 2 Apr 2014 by James

Correct

65Size
Leading solution size is 38.
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
 
%
m=[5 ;1 ];
Lexp= 0;
L=Loop(m);
assert(isequal(Lexp,L))
ans =
     0
2
Pass
 
%%
m=[6 1 7 3 ;0 1 0 1 ];
Lexp= 13;
L=Loop(m);
assert(isequal(Lexp,L))
ans =
    13
3
Pass
 
%%
m=[5 4 3 2 5 4 3 ;1 0 0 0 0 0 0 ];
Lexp= 8;
L=Loop(m);
assert(isequal(Lexp,L))
ans =
     8
4
Pass
 
%%
m=[46 39 3 40 12 36 83 24 61 66 19 87 38 62 27 12 66 39 48 25 89 3 13 80 61 4 76 26 99 89 62 79 25 45 19 5 3 97 1 20 97 86 73 89 26 62 22 41 13 55 28 87 95 16 41 30 62 44 11 34 48 47 33 51 22 15 42 32 33 91 21 57 26 78 66 65 78 97 57 97 19 78 27 23 8 14 37 47 59 38 43 90 96 3 92 59 42 99 92 32 30 6 19 20 62 84 25 87 83 42 51 80 10 16 15 23 40 3 76 13 39 78 94 3 21 94 87 21 74 64 55 43 8 4 46 24 93 69 78 53 28 59 95 29 79 54 64 81 26 5 61 51 47 92 31 53 43 28 79 63 68 14 69 95 35 16 30 34 77 67 65 69 82 79 63 65 82 82 62 98 7 58 29 70 93 14 7 82 20 99 88 77 39 59 65 35 62 51 13 51 37 59 91 84 34 50 62 88 67 12 82 13 75 66 31 69 97 71 21 70 6 53 71 47 40 39 56 5 76 80 60 38 63 89 18 79 55 16 53 7 61 97 88 19 68 97 52 94 90 60 60 29 53 88 50 11 57 81 75 50 28 58 92 1 14 74 20 92 45 52 97 37 10 52 8 93 17 34 76 40 59 34 67 73 73 36 4 71 90 33 71 12 92 98 88 10 6 6 90 15 4 66 72 39 60 69 38 7 20 81 11 98 51 79 71 25 36 60 96 70 46 22 35 27 88 3 30 81 28 53 88 16 57 25 23 14 98 38 16 66 10 5 59 72 19 4 63 73 30 22 61 1 10 73 1 18 49 32 91 12 18 83 42 34 50 36 75 37 34 69 30 10 25 36 38 18 13 5 22 89 56 95 32 68 54 86 80 3 37 30 98 49 85 49 2 7 68 9 96 54 43 1 87 56 66 30 43 54 46 71 90 76 1 35 26 16 97 48 52 73 8 3 5 21 89 15 40 60 12 14 90 15 42 37 27 3 75 52 82 16 77 39 12 85 18 76 81 81 19 6 2 19 33 41 58 17 54 41 2 37 39 43 69 76 37 45 77 67 96 38 34 52 67 1 84 77 11 8 36 19 18 79 13 89 67 99 77 15 47 16 9 89 36 84 56 33 97 95 80 93 47 90 75 88 76 61 16 26 8 75 60 61 23 37 98 29 62 17 13 59 8 38 12 4 50 64 44 9 4 55 53 91 79 29 49 23 8 42 76 82 43 55 46 39 59 12 5 40 36 94 29 89 13 39 91 92 71 35 97 6 25 42 95 38 57 88 84 80 17 38 39 77 45 66 27 42 51 72 35 7 88 18 60 20 98 78 10 32 56 14 25 82 58 3 65 19 11 75 66 21 51 28 58 92 95 16 77 37 89 36 44 70 72 40 4 64 50 69 8 31 16 64 69 6 17 71 82 31 18 68 59 80 51 24 82 63 40 36 33 40 42 48 86 48 10 88 61 17 3 53 69 85 39 44 79 39 61 53 96 32 59 52 79 96 97 86 43 34 35 28 79 17 13 ;1 0 0 0 1 1 0 0 0 1 1 1 1 0 0 1 0 1 0 0 1 0 0 0 0 0 0 1 1 0 0 0 1 1 1 1 1 1 0 0 0 0 1 1 0 1 0 0 0 1 0 0 1 1 0 0 0 0 1 0 0 0 0 1 1 0 0 0 0 1 1 1 1 1 0 0 0 0 1 1 0 1 1 1 1 0 1 1 0 0 1 0 0 0 1 0 1 0 0 1 1 0 1 1 0 0 0 1 1 0 1 1 1 0 1 1 0 1 1 0 0 0 0 0 1 0 0 0 1 0 0 0 0 0 1 0 1 1 0 0 1 0 1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 0 0 1 1 1 1 1 1 1 0 0 0 1 1 1 1 0 0 ];
Lexp= 32240;
L=Loop(m);
assert(isequal(Lexp,L))
ans =
       32240
5
Pass
 
%%
m=[60 44 41 80 31 96 12 58 31 13 61 58 55 64 83 6 13 23 18 47 22 44 70 8 31 99 85 80 80 39 33 65 43 67 33 25 82 37 79 84 28 66 74 26 68 54 33 35 2 3 64 8 44 17 70 7 24 78 36 78 46 38 91 26 95 98 58 90 11 32 96 5 63 31 7 93 10 35 12 70 85 2 9 24 83 64 22 74 53 77 98 57 52 88 6 15 90 77 94 94 12 69 80 50 78 10 71 80 95 95 58 79 6 90 15 59 22 96 56 53 65 53 46 23 3 81 34 63 50 95 2 87 40 38 73 1 23 91 42 57 73 71 57 32 25 60 1 79 98 27 71 77 32 93 31 74 53 40 33 39 72 93 43 59 58 90 91 52 29 2 37 8 13 84 29 33 79 23 85 78 75 48 83 80 35 88 61 2 37 72 1 7 78 69 32 76 26 53 93 40 50 95 27 25 86 7 83 14 63 66 52 44 54 83 61 48 31 40 61 98 57 69 32 44 42 76 41 74 64 87 32 19 21 92 6 80 54 85 17 66 75 57 95 81 61 60 62 11 51 36 51 62 10 55 60 12 54 35 50 16 28 94 31 91 6 76 7 26 44 16 58 41 96 99 94 73 53 46 63 6 54 60 96 13 74 76 18 55 34 27 67 67 73 82 35 72 56 27 78 51 49 65 79 73 45 32 40 9 97 43 12 68 25 65 2 86 46 63 70 74 88 18 69 58 37 77 76 31 57 19 91 87 86 45 31 83 55 85 26 56 94 10 48 86 34 85 52 55 20 12 83 39 39 34 17 64 94 72 33 55 87 64 16 78 13 28 53 2 23 92 70 69 45 57 55 3 32 5 5 22 23 45 55 45 79 92 7 68 52 52 4 79 58 96 34 63 74 36 17 73 93 30 94 66 71 82 79 70 7 67 35 41 3 7 79 38 92 84 74 32 92 45 47 66 88 19 14 59 23 42 26 82 4 35 65 54 87 49 87 39 88 59 30 11 56 6 85 66 16 5 56 65 71 36 66 26 29 97 19 51 71 65 71 46 68 47 64 82 29 27 85 22 98 11 28 97 11 14 95 5 52 22 90 2 64 28 54 93 45 90 88 61 3 74 22 57 24 24 26 85 11 76 67 85 48 60 15 23 68 19 68 8 43 16 73 51 62 45 70 59 39 59 39 44 27 72 80 66 20 86 12 27 63 80 41 12 1 92 65 96 80 22 49 29 78 51 69 97 23 4 79 38 30 88 52 53 95 17 81 22 48 33 47 38 88 39 84 84 67 79 38 90 84 79 21 89 58 13 58 36 53 16 51 5 84 97 76 44 54 92 38 62 50 76 99 85 96 95 56 52 57 14 19 83 87 6 56 76 17 83 48 75 76 53 74 58 87 68 5 35 58 30 44 48 44 68 6 76 61 22 60 28 93 65 6 34 2 21 54 21 78 65 30 79 6 53 67 53 76 43 84 9 22 82 24 56 14 39 58 48 57 59 52 31 47 10 71 13 24 45 14 4 80 68 81 56 11 80 94 82 15 65 97 72 31 54 27 15 63 10 32 77 16 5 9 99 50 81 33 90 78 51 55 25 32 35 21 11 3 44 70 90 18 67 67 69 12 59 88 94 88 41 67 46 62 41 38 34 52 97 13 69 86 60 91 11 31 19 15 41 78 46 85 59 36 20 35 25 27 61 16 44 30 34 3 70 72 21 35 5 12 26 20 59 12 83 64 78 30 19 38 70 24 3 94 27 80 15 69 67 33 28 39 80 60 99 44 63 1 8 1 91 58 98 6 15 43 37 4 53 55 51 74 61 58 96 60 75 97 13 41 76 5 71 25 29 16 48 14 61 55 62 3 95 86 75 41 16 22 68 63 33 62 78 67 37 32 93 31 38 97 32 40 90 72 11 10 6 6 45 14 86 12 66 82 89 19 68 72 2 29 71 88 90 55 99 40 98 64 97 86 94 37 94 43 69 57 92 4 64 25 20 65 71 87 26 26 11 97 4 16 53 25 23 47 51 57 68 36 54 50 65 15 83 34 ;1 1 1 1 0 1 1 0 1 1 0 0 0 0 0 1 0 1 1 0 0 0 0 1 0 0 1 0 0 1 1 0 0 0 0 1 1 0 0 1 0 1 1 0 0 0 0 0 0 1 0 0 1 0 0 1 1 0 1 1 0 0 0 1 0 0 1 0 0 0 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0 0 1 0 0 1 0 0 0 0 0 0 0 0 0 0 1 1 1 0 1 1 0 1 1 1 1 1 1 1 1 1 0 0 0 1 1 1 1 0 1 1 0 0 1 0 1 0 0 1 0 0 1 1 0 0 0 0 0 0 1 0 0 1 1 1 0 0 1 0 1 1 0 0 0 0 0 0 1 0 0 0 1 0 0 1 0 1 0 0 1 0 1 0 0 1 1 0 0 1 0 1 0 0 0 0 1 1 1 1 1 1 0 0 1 0 0 1 0 0 1 1 1 0 1 0 1 1 1 0 1 1 1 1 1 0 1 0 1 0 0 1 1 1 0 0 1 1 0 1 1 1 0 1 0 1 1 0 0 1 1 0 1 0 1 0 0 0 1 0 0 1 0 0 1 0 0 1 1 0 1 1 1 1 0 0 1 0 0 1 0 0 1 0 0 0 0 1 1 0 0 1 1 0 1 0 0 0 1 1 1 0 1 0 0 1 1 1 1 1 0 1 1 1 0 0 0 1 1 0 0 1 1 1 1 0 1 1 1 0 1 0 0 0 0 1 0 0 1 0 1 1 1 0 1 0 1 1 0 0 0 0 0 0 0 1 0 0 0 0 0 1 1 1 1 1 1 1 0 1 1 1 1 1 0 1 0 1 1 0 0 0 1 0 0 1 1 0 0 1 1 0 1 1 1 0 0 0 1 0 0 1 0 1 0 0 0 0 0 1 1 1 0 ];
Lexp= 45294;
L=Loop(m);
assert(isequal(Lexp,L))
ans =
       45294
6
Pass
 
%%
m=[94 61 98 11 2 12 99 37 26 10 86 18 88 42 43 38 62 5 36 56 5 86 78 29 64 27 54 72 96 73 36 99 81 55 90 55 73 51 94 11 82 15 45 67 63 79 75 22 4 18 39 1 75 57 92 49 24 16 68 52 70 88 56 38 25 88 86 29 15 89 37 96 85 40 86 76 26 73 42 24 45 22 40 36 35 11 65 22 69 3 9 27 28 78 40 68 62 75 32 65 49 15 35 32 88 73 20 95 67 94 60 73 81 93 76 67 56 37 1 74 59 35 51 38 92 56 37 89 54 67 78 11 62 68 99 61 45 48 74 92 15 74 96 31 33 21 50 98 7 31 51 62 27 26 47 81 66 3 13 98 83 94 ;0 1 1 1 1 0 1 1 0 1 1 0 0 1 1 1 0 1 0 1 1 0 0 0 0 0 0 1 1 1 0 0 0 0 1 0 1 0 0 1 0 1 1 0 0 1 0 0 1 0 1 1 0 0 1 1 1 1 0 0 1 1 0 0 0 1 0 0 1 1 0 1 1 0 1 0 1 1 1 0 0 0 0 1 1 0 0 1 0 0 1 0 1 0 0 0 1 1 0 1 1 0 1 0 0 0 1 1 1 0 1 0 0 0 0 0 0 1 1 0 0 1 1 1 0 0 1 1 1 1 1 0 1 1 0 0 0 0 0 0 0 1 1 0 1 1 0 0 0 1 0 1 1 0 0 0 0 0 0 1 1 0 ];
Lexp= 8346;
L=Loop(m);
assert(isequal(Lexp,L))
ans =
        8346
7
Pass
 
%%
% A Method to Read, Process, and Output a GJam Entry
% Read a file into a Cell Array variable for each case
% Open Output file
% Loop thru Cases using cell array loaded
% Write each case's solution
% Generalized function to Process a Concise Data set
% function GJam_SA_2011A
% %Segment Loop
% %fn='A-small-practice.in';
% fn='A-large-practice.in';
% [data] = read_file(fn);
%
% %fidG = fopen('A-small-output.out', 'w');
% fidG = fopen('A-large-output.out', 'w');
% tic
% for i=1:size(data,2)
%  L = Loop(data{i}) ;% data 2xN, row 1 Length row 2 Type
%  toc
%   fprintf(fidG,'Case #%i: %i\n', i,L);
%   fprintf('Case #%i: %i\n', i,L);
%
% end
% toc
%
% fclose(fidG);
%
% end
%
% function [d] = read_file(fn)
% %Read a numeric of lines to count
% fid=fopen(fn);
% fgetl(fid); % Total Count ignore
% set=0;
% while ~feof(fid)
%  fgetl(fid); % Qty in a row, ignore
%  strv=fgetl(fid); % read line of data as a string since mixed numeric/char
%  m=str2num(strrep(strrep(strv,'R',''),'B','')); % Remove R and B to get Lengths
%  % Extract R and B and convert to a binary vector
%  m(2,:)=str2num(strrep(strrep(strv(regexp(strv,'[RB]','start')),'R','0 '),'B','1 '));
%
%  set=set+1;
%  d{set}=m;
% end
%
% fclose(fid);
%
% end
%
% function L=Loop(m)
% % Create two descending vectors
% % L=sum of paired sets - qty of paired sets
%  L=0;
%  m0=m(1,m(2,:)==0);
%  m1=m(1,m(2,:)==1);
%  if isempty(m0),return;end
%  if isempty(m1),return;end
%  m0=sort(m0,'descend');
%  m1=sort(m1,'descend');
%  q=min(length(m0),length(m1));
%  L=sum([m0(1:q) m1(1:q)])-2*q;
%
% end
%
% Google File Input
%5
%1
%5B
%4
%6R 1B 7R 3B
%7
%5B 4R 3R 2R 5R 4R 3R
%673
% Data line of 673 values not included