介绍

本篇是"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. SQL CASE WHEN语句性能优化

    背景:性能应该是功能的一个重要参考,特别是在大数据的背景之下!写SQL语句时如果仅考虑业务逻辑,而不去考虑语句效率问题,有可能导致严重的效率问题,导致功能不可用或者资源消耗过大.其中的一种情况是,处理 ...

  2. ScSPM & LLC

    为啥会有SPM→ScSPM呢?原因之一是为了寻找better coding + better pooling的方式提高性能,原因之二就是提高速度.如何提高速度?这里的速度,不是Coding+Pooli ...

  3. ZBrush中如何做不同图案的遮罩

    ZBrush®软件中不仅可以创建矩形遮罩还可以创建有图案的遮罩,且是非常简单有效的,那么究竟怎样做出神奇的效果,本文将为您详细讲解. 有关反转遮罩.清除遮罩的详细内容,请点击:ZBrush中如何反选遮 ...

  4. Python3.7中的常用关键字

    本文是在学习Python中遇到的一些关键字,作为日常总结的笔记. Python中有保留字/关键字 保留字就是在Python中预先保留的标识符,这些标识符在Python程序中具有特定用途,不能被程序员作 ...

  5. Django路由URL

    URL配置(URLconf)就像Django所支撑网站的目录.URL与要为该URL调用的视图函数之间的映射表. URLconf配置 样式: from django.conf.urls import u ...

  6. npx命令

    npx命令 查了一下, 英文资料: https://www.npmjs.com/package/npx 中文资料: 什么是npx 第一次看到npx命令是在 babel 的文档里 Note: If yo ...

  7. laravel 常用单词翻译

    1.ORM:(Object Relational Mapping,简称ORM,或O/RM,或O/R mapping), 翻译为:对象关系映射. 是一种程序技术,用于实现面向对象编程语言里不同类型系统的 ...

  8. Lumen5.5,使用laravel excel 下载 、导入excel文件

    1.安装 首先是安装laravel excel,使用composer安装 composer require maatwebsite/excel ~2.1.0 2.配置 在bootstrap/app.p ...

  9. git--客户端管理工具初步使用

    说点废话哈 小白一枚, 今年3月份进入自己的第一家公司, 开始成为前端中的一份子,好在公司里有位和我一同进来的一位老哥带着我,从老哥身上学到的知识不多,(因为和老哥只相处工作了三个月,因为家里的事情, ...

  10. Vue系列(二):发送Ajax、JSONP请求、Vue生命周期及实例属性和方法、自定义指令与过渡

    上一篇:Vue系列(一):简介.起步.常用指令.事件和属性.模板.过滤器 一. 发送AJAX请求 1. 简介 vue本身不支持发送AJAX请求,需要使用vue-resource.axios等插件实现 ...