介绍

本篇是"FCC编程题之中级算法篇"系列的最后一篇

这期完结后,下期开始写高级算法,每篇一题


目录


1. Smallest Common Multiple

Find the smallest common multiple of the provided parameters that can be evenly divided by both, as well as by all sequential numbers in the range between these parameters.

The range will be an array of two numbers that will not necessarily be in numerical order.

e.g. for 1 and 3 - find the smallest common multiple of both 1 and 3 that is evenly divisible by all numbers between 1 and 3.

Here are some helpful links:

  • Smallest Common Multiple

寻找给定两数及其中间的所有数的最小公倍数

思路

  • 最小公倍数一定能整除所有的数,此处假设该数组中的最大的数即为最小公倍数。

  • 用假设的最小公倍数除以给定范围中的所有的数,如果都能整除,说明该假设的数即为最小公倍数。

  • 如果有不能整除的数,则将假设的数加上数组中最大的数,在重复第二步。

function smallestCommons(arr) {
let [start, end] = arr[0] < arr[1] ? [arr[0], arr[1]] : [arr[1], arr[0]]; for (var i = start, com = end; i < end; i++) {
if (com % i !== 0) {
i = start - 1;
com += end;
}
} return com;
}

2. Finders Keepers

Create a function that looks through an array (first argument) and returns the first element in the array that passes a truth test (second argument).

Here are some helpful links:

  • Array.prototype.filter()

方法 1

用帮助栏的filter()方法, filter()方法返回一个数组,判断数组的长度,如果不为零,则返回数组第一个元素,否则返回undefined

function findElement(arr, func) {
let arr1 = arr.filter((val) => {
return func(val);
}); return arr1.length === 0 ? undefined : arr1[0];
}

方法 2

find()方法,find()方法接收一个测试函数作为参数,并返回满足测试函数的第一个元素,如果没有则返回undefined

function findElement(arr, func) {
return arr.find(func);
}

3. Drop it

Drop the elements of an array (first argument), starting from the front, until the predicate (second argument) returns true.

The second argument, func, is a function you'll use to test the first elements of the array to decide if you should drop it or not.

Return the rest of the array, otherwise return an empty array.

Here are some helpful links:

  • Arguments object
  • Array.prototype.shift()
  • Array.prototype.slice()

思路

使用while()循环对数组元素进行判断,没有通过测试函数的直接删除即可

function dropElements(arr, func) {
while (!func(arr[0])) {
arr.shift(0);
} return arr;
}

4. Steamroller

Flatten a nested array. You must account for varying levels of nesting.

Here are some helpful links:

  • Array.isArray()

遍历嵌套数组思路即可解题

Array.isArray()方法接收一个参数,判断这个参数是否是数组

思路

  • 创建一个递归函数,判断数组元素是否为数组,不是数组就返回其值,是数组则使用递归函数
function steamrollArray(arr) {
let convert = arr => arr.reduce((arr, val) => {
return arr.concat(Array.isArray(val) ? convert(val) : val);
}, []); return convert(arr);
}

5. Binary Agents

Return an English translated sentence of the passed binary string.

The binary string will be space separated.

Here are some helpful links:

String.prototype.charCodeAt() String.fromCharCode()

将二进制数翻译为英文

思路

  • parseInt()方法接收两个参数,第一个参数为字符串,第二个为基数

  • 利用fromCharCode()方法返回Unicode值序列创建的字符串。

  • 将字符串拼接在一起

function binaryAgent(str) {
return str.split(' ').map((val) => {
return String.fromCharCode(parseInt(val, 2));
}).join('');
}

6. Everything Be Tru

Check if the predicate (second argument) is truthy on all elements of a collection (first argument).

Remember, you can access object properties through either dot notation or [] notation.

根据测试用例可以看出所测属性的值也需为真才行

思路

  • 利用every()方法对每个元素都调用测试函数判断一次

  • 判断对象是否有指定属性,如有,则其值是否为真

function truthCheck(collection, pre) {
return collection.every((val) => {
return val.hasOwnProperty(pre) && !!val[pre];
});
}

7. Arguments Optional

Create a function that sums two arguments together. If only one argument is provided, then return a function that expects one argument and returns the sum.

For example, addTogether(2, 3) should return 5, and addTogether(2) should return a function.

Calling this returned function with a single argument will then return the sum:

var sumTwoAnd = addTogether(2);

sumTwoAnd(3) returns 5.

If either argument isn't a valid number, return undefined.

Here are some helpful links:

  • Closures
  • Arguments object

考察闭包

思路

利用isFinite()方法判断元素是否为数字

function addTogether() {
let arr = Array.prototype.slice.call(arguments); if (arr.length === 2) {
return arr.every(Number.isFinite) ? arr[0] + arr[1] : undefined;
} else {
return Number.isFinite(arr[0]) ? (val) => { return Number.isFinite(val) ? arr[0] + val : undefined; } : undefined;
}
}

欢迎大家去我的简书看看

FCC编程题之中级算法篇(下)的更多相关文章

  1. FCC编程题之中级算法篇(上)

    介绍 FCC: 全称为freeCodeCamp,是一个非盈利性的.面向全世界的编程练习网站.这次的算法题来源于FCC的中级算法题. FCC中级算法篇共分为(上).(中).(下)三篇.每篇各介绍7道算法 ...

  2. FCC编程题之中级算法篇(中)

    介绍 接着上次的中级算法题 目录 1. Missing letters 2. Boo who 3. Sorted Union 4. Convert HTML Entities 5. Spinal Ta ...

  3. java学习之第五章编程题示例(初学篇)

    /* Animal.java */ package animal; public abstract class Animal { public abstract void cry(); public ...

  4. FCC上的javascript算法题之中级篇

    FCC中的javascript中级算法题解答 中级算法的题目中用到了很多js的知识点,比如迭代,闭包,以及对json数据的使用等等,现在将自己中级算法的解答思路整理出来供大家参考讨论.欢迎大家提出新的 ...

  5. fcc的中级算法题

    核心提示:这是网上开源编程学习项目FCC的javascript中级编程题(Intermediate Algorithm Scripting(50 hours)),一共20题.建议时间是50个小时,对于 ...

  6. 算法是什么我记不住,But i do it my way. 解一道滴滴出行秋招编程题。

    只因在今日头条刷到一篇文章,我就这样伤害我自己,手贱. 刷头条看到一篇文章写的滴滴出行2017秋招编程题,后来发现原文在这里http://www.cnblogs.com/SHERO-Vae/p/588 ...

  7. C算法编程题(七)购物

    前言 上一篇<C算法编程题(六)串的处理> 有些朋友看过我写的这个算法编程题系列,都说你写的不是什么算法,也不是什么C++,大家也给我提出用一些C++特性去实现问题更方便些,在这里谢谢大家 ...

  8. C算法编程题(六)串的处理

    前言 上一篇<C算法编程题(五)“E”的变换> 连续写了几篇有关图形输出的编程题,今天说下有关字符串的处理. 程序描述 在实际的开发工作中,对字符串的处理是最常见的编程任务.本题目即是要求 ...

  9. C算法编程题(五)“E”的变换

    前言 上一篇<C算法编程题(四)上三角> 插几句话,说说最近自己的状态,人家都说程序员经常失眠什么的,但是这几个月来,我从没有失眠过,当然是过了分手那段时期.每天的工作很忙,一个任务接一个 ...

随机推荐

  1. 用Latex做介绍自己和团队科研的网页

    最近实验室师妹用网上的一些模板改了改做了几个网页.感觉还可以.但是实际上总觉得好像和韩家炜.周志华他们的页面差点什么. 最近找论文时发现奥地利的hornik老先生页面居然latex做的,然后找到了下面 ...

  2. PHP学习过程中遇到的疑难杂症

    变量当双引号中包含变量时,变量会与双引号中的内容连接在一起:当单引号中包含变量时,变量会被当做字符串输出. Heredoc结构形式首先使用定界符表示字符串(<<<),接着在“< ...

  3. thinkphp5-----模板中函数的使用

    1.在模板中使用php函数 在thinkphp的html中,我们经常会遇到一些变量难以直接从php控制端直接处理,这些变量只有在模板中循环输出的时候处理比较合适,这个时候,我们就要在模板中使用函数 1 ...

  4. JDBC程序实例

    实例 ( Statement ): public class JDBC { public static void main(String[] args) throws Exception { Conn ...

  5. [NOIP补坑计划]NOIP2016 题解&做题心得

    感觉16年好难啊QAQ,两天的T2T3是不是都放反了啊…… 场上预计得分:100+80+100+100+65+100=545(省一分数线280) ps:loj没有部分分,部分分见洛咕 题解: D1T1 ...

  6. docker删除docker_gwbridge网桥

    最后更新时间:2018年12月26日 使用命令:docker network rm docker_gwbridge 提示无法删除. [root@localhost ~]# docker network ...

  7. Android开发进度04

    1,今日:目标:实现登录和注册功能 2,昨天:完成登录和注册的界面以及后台数据库的操作 3,收获:会使用SQlite数据库的操作语句 4,问题:登录时出现问题(登录不上去)

  8. 小学生绞尽脑汁也学不会的python(异常,约束,MD5加密,日志处理)

    小学生绞尽脑汁也学不会的python(异常,约束,MD5加密,日志处理) 异常处理(处理) 1.产生异常.raise 异常类(),抛出异常2. 处理异常: try: xxxxx # 尝试执行的代码. ...

  9. Java并发和多线程3:线程调度和有条件取消调度

    在第1篇中"并发框架基本示例",提到了Executors和ThreadPool.其中,还有个"定时调度"的方法,Executors.newScheduledTh ...

  10. 常用的ES6方法

    常用的ES6方法 ES6之后,新增了定义变量的两个关键字,分别是let和const. let和const都能够声明块级作用域,用法和var是类似的,let的特点是不会变量提升,而是被锁在当前块中. 实 ...