Cody Problem 11-20 Solutions

原创 2016年08月31日 13:14:37

Problem 11. Back and Forth Rows

function b = back_and_forth(n)
  for i = 1:2:n
      b(i,:) = (i-1)*n+1:i*n;
  end
  for j = 2:2:n
      b(j,:) = j*n : -1 : (j-1)*n+1;
  end
end


Problem 12. Fibonacci sequence

function f = fib(n)
    a(1) = 1
    a(2) = 1
    for i = 3:n
        a(i) = a(i-1)+a(i-2);
    end       
  f = a(n);
end

Problem 13. Remove all the consonants

function s2 = refcn(s1)
  s2=regexprep(s1,'(?=[a-z])[^aeiou]','','ignorecase');
end

%正则表达式


Problem 14. Find the numeric mean of the prime numbers in a matrix.

function out = meanOfPrimes(in)
    num = 0;
    sum = 0;
    for i = 1:length(in)
        if isprime(in(i))
            num = num + 1;
            sum = sum + in(i);
        end
    end
    out = sum / num;
end


Problem 15. Find the longest sequence of 1's in a binary sequence

function y = lengthOnes(x)
  y = max([0, regexp(x, '1+', 'end')-regexp(x, '1+', 'start')+1]);
end


Problem 16. Return the largest number that is adjacent to a zero

function y = nearZero(x)
    y = -inf;
    x = [-inf, x, -inf];
    y = max(y, max([x(find(x==0)-1),x(find(x==0)+1)]));
    
end


Problem 17. Find all elements less than 0 or greater than 10 and replace them with NaN

function y = cleanUp(x)
  x(find(x<0 | x>10)) = NaN;
  y = x;
end

Problem 18. Bullseye Matrix

function a = bullseye(n)
   a = discretize(spiral(n), [0 1:2:n].^2+.5);  
end


Problem 19. Swap the first and last columns

function B = swap_ends(A)
    if length(A) > 1
      B = [A(:, end),A(:, 2:end-1),A(:, 1)];
    else
        B = A;
end


Problem 20. Summing digits

function b = sumDigits(n)
    c = 2^n;
    a = num2str(c);
    b = sum(str2num(a(:)));
end










版权声明:本文为博主原创文章,未经博主允许不得转载。

Cody Problem 1-10 Solutions

Cody Problem 1-10 ,答案的size都不是很小,需要继续改进!
  • hetianbiao
  • hetianbiao
  • 2016年08月29日 10:06
  • 230

[matlab]mathworks上的cody challenge题解及一些常用函数的总结(5)

Part 5. Problem 41 - Problem 50. Problem 41. Cell joiner % You are given a cell array of strings an...
  • lkjslkjdlk
  • lkjslkjdlk
  • 2013年08月24日 22:54
  • 1570

UVA 11020 - Efficient Solutions(set)

UVA 11020 - Efficient Solutions 题目链接 题意:每个人有两个属性值(x, y),对于每一个人(x,y)而言,当有另一个人(x', y'),如果他们的属性值满...
  • u011217342
  • u011217342
  • 2014年08月24日 00:27
  • 1078

phonics-1

Beginning Consonants: b,c, t, n, p B: ball The baby wants to bounce the ball. Directions: Say eac...
  • ccwwff
  • ccwwff
  • 2012年06月05日 15:13
  • 463

[matlab]mathworks上的cody challenge题解及一些常用函数的总结(4)

Part 4. Problem 31 - Problem 40. Problem 31. Remove all the words that end with "ain" % Given the ...
  • lkjslkjdlk
  • lkjslkjdlk
  • 2013年08月23日 22:32
  • 1617

Matlab Cody (1)

Size refers to the number of nodes in a parse tree. Generally speaking, you can think of size as cod...
  • youth0826
  • youth0826
  • 2013年10月24日 17:53
  • 696

[matlab]mathworks上的cody challenge题解及一些常用函数的总结(8)

Part 8. Problem 71 - Problem 80. 前几天把暑假剩的两道题过了,把cody challenge搞完了。最近快放国庆了,又有大段空闲时间了,白天学学算法,晚上把这个坑填了....
  • lkjslkjdlk
  • lkjslkjdlk
  • 2013年09月29日 00:33
  • 1740

CS229 ProblemSet记录

PS0 Gradients and Hessians Positive definite matrices Eigenvectors eigenvalues and the spectral theo...
  • u013508213
  • u013508213
  • 2016年12月16日 17:02
  • 1269

some svn problem solutions

1、when svn cleanup, show this type of message:  svn: E720003: Can't set file 'F:\code\main.cpp' read...
  • cgsgood
  • cgsgood
  • 2016年12月01日 12:52
  • 104

hdu 4497 GCD and LCM(数论,排列组合)

GCD and LCM Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) Tota...
  • acm_cxq
  • acm_cxq
  • 2016年06月09日 23:03
  • 430
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Cody Problem 11-20 Solutions
举报原因:
原因补充:

(最多只允许输入30个字)