FCC编程题之中级算法篇(下)
介绍
本篇是"FCC编程题之中级算法篇"系列的最后一篇
这期完结后,下期开始写高级算法,每篇一题
目录
- 1. Smallest Common Multiple
- 2. Finders Keepers
- 3. Drop it
- 4. Steamroller
- 5. Binary Agents
- 6. Everything Be Tru
- 7. Arguments Optional
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编程题之中级算法篇(下)的更多相关文章
- FCC编程题之中级算法篇(上)
介绍 FCC: 全称为freeCodeCamp,是一个非盈利性的.面向全世界的编程练习网站.这次的算法题来源于FCC的中级算法题. FCC中级算法篇共分为(上).(中).(下)三篇.每篇各介绍7道算法 ...
- FCC编程题之中级算法篇(中)
介绍 接着上次的中级算法题 目录 1. Missing letters 2. Boo who 3. Sorted Union 4. Convert HTML Entities 5. Spinal Ta ...
- java学习之第五章编程题示例(初学篇)
/* Animal.java */ package animal; public abstract class Animal { public abstract void cry(); public ...
- FCC上的javascript算法题之中级篇
FCC中的javascript中级算法题解答 中级算法的题目中用到了很多js的知识点,比如迭代,闭包,以及对json数据的使用等等,现在将自己中级算法的解答思路整理出来供大家参考讨论.欢迎大家提出新的 ...
- fcc的中级算法题
核心提示:这是网上开源编程学习项目FCC的javascript中级编程题(Intermediate Algorithm Scripting(50 hours)),一共20题.建议时间是50个小时,对于 ...
- 算法是什么我记不住,But i do it my way. 解一道滴滴出行秋招编程题。
只因在今日头条刷到一篇文章,我就这样伤害我自己,手贱. 刷头条看到一篇文章写的滴滴出行2017秋招编程题,后来发现原文在这里http://www.cnblogs.com/SHERO-Vae/p/588 ...
- C算法编程题(七)购物
前言 上一篇<C算法编程题(六)串的处理> 有些朋友看过我写的这个算法编程题系列,都说你写的不是什么算法,也不是什么C++,大家也给我提出用一些C++特性去实现问题更方便些,在这里谢谢大家 ...
- C算法编程题(六)串的处理
前言 上一篇<C算法编程题(五)“E”的变换> 连续写了几篇有关图形输出的编程题,今天说下有关字符串的处理. 程序描述 在实际的开发工作中,对字符串的处理是最常见的编程任务.本题目即是要求 ...
- C算法编程题(五)“E”的变换
前言 上一篇<C算法编程题(四)上三角> 插几句话,说说最近自己的状态,人家都说程序员经常失眠什么的,但是这几个月来,我从没有失眠过,当然是过了分手那段时期.每天的工作很忙,一个任务接一个 ...
随机推荐
- ZBrush软件特性之Layers
ZBrush®中的Layers层调控板可以在单个文档工作中添加多个层,实际上是把新建的层作为一个分离的文档,层之间可以相互影响. 使用层工作 Layers调控板为每个层都有预置存放的空间,刚启动ZBr ...
- How to debug systemd step by step
docker run -ti --name systemd --net host --privileged reg.docker.xxxxxxxx:latest /usr/lib/systemd/sy ...
- faker smtp server
import os import asyncio import logging import base64 from email import message_from_bytes from emai ...
- IOS - PDF合并
#pragma mark - Merge PDF - (void)mergePDF { NSArray *paths = NSSearchPathForDirectoriesInDomains(NSD ...
- PHP下的Oauth2.0尝试 - OpenID Connect
OpenID Connect OpenID Connect简介 OpenID Connect是基于OAuth 2.0规范族的可互操作的身份验证协议.它使用简单的REST / JSON消息流来实现,和之 ...
- python_字符串常用操作
name = "monicao"name.capitalize() #首字母大写print(name.capitalize()) print(name.count("o& ...
- LeetCode 11. Container With Most Water 单调队列
题意 Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai ...
- 实现双向数据绑定mvvm
实现双向数据绑定mvvm
- 兴趣爱好-QQ的本地共享
QQ这个本地共享简直了,不就实现了公网FTP的功能么?好方便啊,有啥文件共享给好友就直接放在本地就可以了,真好用
- IOS假设将一个十六进制的color转换成UIColor,非常有用
UI给开发的效果图非常多时候标注着十六进制的Color,而程序中用到的往往是UIColor能够用例如以下方法去转换: (UIColor *)RGBColorFromHexString:(NSStrin ...