Matlab Cody (1)

Size refers to the number of nodes in a parse tree. Generally speaking, you can think of size as code length.

What is Cody? see here

 

Problem 1. Times 2 - START HERE

function y = times2(x)
  y = bitshift(x,1); % size=13
end
function y = times2(x)
  y = 2*x; % size=12 (same as y=x+x)
end
function ans = times2(x)
  2*x; % size=10 (best)
end

 

 

Problem 2. Make the vector [1 2 3 4 5 6 7 8 9 10]

function x = oneToTen
  x = [1 2 3 4 5 6 7 8 9 10]; % size=20
end
function x = oneToTen
  x = 1:10; % size=11
end
function ans = oneToTen
  1:10; % size=9 (best)
end
function ans = oneToTen
  colon(1,10); % size=10
end
function ans = oneToTen
  linspace(1,10,10); % size=11
end
function x = oneToTen
  x = cumsum(ones(1,10)); % size=14
end

 

 

Problem 3. Find the sum of all the numbers of the input vector

function ans = vecsum(x)
   sum(x); % size=10 (best)
end
function y = vecsum(x)
   y = sum(x(:)); % size=14 (useful for matrix)
 end
function y = vecsum(x)
   n = length(x);
   y = 0; 
   for i = 1:n
        y = y + x(i); % size=30
   end
 end

 

 

Problem 4. Make a checkerboard matrix

function a = checkerboard(n)
  a(1:2:n,1:2:n) = 1;
  a(2:2:n,2:2:n) = 1;  % size=36
end
function ans = checkerboard(n)
  invhilb(n)>0; % size=12 (best)
end
function a = checkerboard(n)
  a1 = repmat([1,0;0,1],n,n);
  a = a1(1:n,1:n);  % size=31
end
function a = checkerboard(n)
  for i=1:n
    for j=1:n
      a(i,j)=mod(i+j+1,2); % size=32
    end;
  end;
end

 

Problem 5. Triangle Numbers

function ans = triangle(n)
 sum(1:n); % size=12 (best)
end
function ans = triangle(n)
 n*(n+1)/2; % size=15
end

 

 

Problem 6. Select every other element of a vector

function ans = everyOther(x)
  x(1:2:end); % size=15 (best)
end
function ans = everyOther(x)
  x(1:2:length(x)); % size=16 (same as x(1:2:numel(x)))
end
function x = everyOther(x)
  x(2:2:end) = []; % size=18
end

ps: 对于m-by-n的矩阵x,numel(x)=m*n, length(x)=n.

 

 

Problem 7. Column Removal

function B = column_removal(A,n)
  A(:,n) = [];
  B = A; % size=19
end
function A = column_removal(A,n)
  A(:,n) = []; % size=15
end
function A = column_removal(A,n)
  A(:,n) = ''; % size=14 (best)
end
function ans = column_removal(A,n)
  A(:,[1:n-1,n+1:end]); % size=24
end
function ans = column_removal(A,n)
  A(:,1:end ~= n); % size=17
end

 

 

Problem 8. Add two numbers

function ans = add_two_numbers(a,b)
  a+b; % size=11
end

 

 

Problem 9. Who Has the Most Change?

function a = most_change(a)
  [~,a]=max(a*[.25;.05;.1;.01]); % size=24
end
function b = most_change(a)
  [~,b]=max(sum(bsxfun(@times,a,str2num('[0.25,0.05,0.10,0.01]')),2)); % size=24
end
function b = most_change(a)
  total = a * [25 5 10 1]';
  b = find(total==max(total)); % size=28
end
function b = most_change(a)
  [~,b] = max(a * str2num('[0.25; 0.05; 0.1; 0.01]')); % size=18 (best)
end

 

 

Problem 10. Determine whether a vector is monotonically increasing

function ans = mono_increase(x)
  isequal(unique(x), x); % size=13 (best)
end
function ans = mono_increase(x)
  mean(diff(x)>0)==1 | isempty(diff(x))==1; % size=24
end
function ans = mono_increase(x)
  all(diff(x)>0); % size=14
end

 

 

Problem 26. Determine if input is odd

function ans = is_it_odd(n)
 bitand(n,1); % size=11 (best)
end
function ans = is_it_odd(n)
  mod(n,2); % size=11 (best)
end
function ans = is_it_odd(n)
  rem(n,2); % size=11 (best)
end
function ans = is_it_odd(n)
  1-(round(n/2)==n/2); % size=19
end

你可能感兴趣的:(matlab)