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










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

相关文章推荐

Project Euler Solutions(Problem 1~7)

Project Euler 的解答,使用mathematica实现。

Problem 11

Problem In the 2020 grid below, four numbers along a diagonal line have been marked in red. 08 ...

精选:深入理解 Docker 内部原理及网络配置

网络绝对是任何系统的核心,对于容器而言也是如此。Docker 作为目前最火的轻量级容器技术,有很多令人称道的功能,如 Docker 的镜像管理。然而,Docker的网络一直以来都比较薄弱,所以我们有必要深入了解Docker的网络知识,以满足更高的网络需求。

11-20

授课内容 数组   int score[100];   score[0]=89;   score[1]=99;    下标>=0 小于数组的长度-1    1.定义数组 ...

Problem 11

欧拉项目 问题11:Largest product in a grid In the 2020 grid below, four numbers along a diagonal line...
  • soulx
  • soulx
  • 2013-02-01 11:12
  • 199

Problem 20 of 100!

n! means n  (n  1)  ...  3  2  1 For example, 10! = 10  9  ...&#...

Project Eluer - Problem 11

In the 2020 grid below, four numbers along a diagonal line have been marked in red. 08 02 22 97 3...

算法:Solutions for the Maximum Subsequence Sum Problem

The maximum subarray problem is the task of finding the contiguous subarray within a one-dimension...

11-20代码

作业1:     使用数组实现10名学生的成绩录入,并输出这10名学生中成绩的     最高分     最低分     平均分     按照分数从大到...

2011-11-20

1. There are 3 ingredients in the good life: learning, earning and yearning. 美好人生由三要素组成:学习、收获和向往。(克里...

Problem 20:Factorial digit sum

原题链接:http://projecteuler.net/problem=20 n! means n  (n  1)  ...  3  2  1 For ...
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

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