Intermediate Algorithm
1. Sum All Numbers in a Range
We’ll pass you an array of two numbers. Return the sum of those two numbers and all numbers between them.
The lowest number will not always come first.
求和给定数组约束范围内的所有数字
1 | function getMaxMin(arr,op){ |
2. Diff Two Arrays
Compare two arrays and return a new array with any items only found in one of the two given arrays, but not both. In other words, return the symmetric difference of the two arrays.
求两个给定数组的diff1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35function diff(arr1, arr2) {
var newArr = [];
/*
var tmpall = arr1.concat(arr2);
for(var i=0,len=tmpall.length;i<len;i++){
if(arr1.indexOf(tmpall[i]) != -1){
//存在1里不存在2里
if(arr2.indexOf(tmpall[i]) == -1){
newArr.push(tmpall[i]);
}
}else {
if(arr2.indexOf(tmpall[i]) != -1){
newArr.push(tmpall[i]);
}
}
}
*/
arr1.filter(function(val){
if(arr2.indexOf(val) == -1){
newArr.push(val);
}
});
arr2.filter(function(val){
if(arr1.indexOf(val) == -1 && newArr.indexOf(val) == -1){
newArr.push(val);
}
});
return newArr;
}
diff(["andesite", "grass", "dirt", "pink wool", "dead shrub"], ["diorite", "andesite", "grass", "dirt", "dead shrub"]);
//diff([3,4,5],[3,4,5,6]);
//diff([1, 2, 3, 5], [1, 2, 3, 4, 5]);
3. Roman Numeral Converter
Convert the given number into a roman numeral.
将给定数字转化为罗马数字
1 | function convert(num){ |
4. Where art thou
Make a function that looks through an array of objects (first argument) and returns an array of all objects that have matching property and value pairs (second argument). Each property and value pair of the source object has to be present in the object from the collection if it is to be included in the returned array.
参数1给定一个若干对象构成的数组,参数2给定一个对象,算法需要从参数1中查找匹配参数2对象的所有对象并以数组形式输出。1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23function where(collection, source) {
var arr = [];
// What's in a name?
var orikey = "",
orivalue = "";
for(var key in source){
orikey = key;
orivalue = source[key];
}
for(var i=0,len=collection.length;i<len;i++){
for(var key in collection[i]){
if(key === orikey && orivalue === collection[i][key]){
arr.push(collection[i]);
}
}
}
return arr;
}
//where([{ first: "Romeo", last: "Montague" }, { first: "Mercutio", last: null }, { first: "Tybalt", last: "Capulet" }], { last: "Capulet" });
where([{ "a": 1 }, { "a": 1 }, { "a": 1, "b": 2 }], { "a": 1 });
5. Search and Replace
Perform a search and replace on the sentence using the arguments provided and return the new sentence.
First argument is the sentence to perform the search and replace on.
Second argument is the word that you will be replacing (before).
Third argument is what you will be replacing the second argument with (after).
执行查找和替换,将句子(参数1)中出现的词(参数2)用另一个(参数3)替换
1 | function isFirstUpper(str){ |
6. Pig Latin
Translate the provided string to pig latin.
将给定字符串翻译成”pig latin”形式。pig latin就是一个将单词形变的小游戏:将单词第一个字母移动到最后并添加”ay”结尾,如果单词以元音开头,则添加”way”结尾。
1 | function translate(str) { |
7. DNA Pairing
The DNA strand is missing the pairing element. Take each character, get its pair, and return the results as a 2d array.
DNA配对,DNA的配对形如”AT,CG”模式,亦即出现字母”A”则给之以”T”配对,以此类推。
1 | function pair(str) { |
8. Missing letters
Find the missing letter in the passed letter range and return it.
If all letters are present in the range, return undefined.
查找并返回给定字符序列中缺失的字母,如”bcdef”应返回”a”。1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19function fearNotLetter(str) {
var arr = [];
for(var i=0,len=str.length;i<len;i++){
//均先转为小写字母后转为数字,起始为0
var tmp = str[i].toLowerCase().charCodeAt(0) - 97;
arr.push(tmp);
}
for(var j=0,len2=arr.length,k=arr[j];j<len2;j++){
if(k != arr[j]){
//console.log(arr[j]);
return String.fromCharCode(arr[j] + 97 -1 );
}
k++;
}
}
fearNotLetter("bcd");
//fearNotLetter("abcdefghjklmno");
9. Boo who
Check if a value is classified as a boolean primitive. Return true or false.
这个不知道为什么会算到中级里来,简直简单的发指。
1 | function boo(bool) { |
10. Sorted Union
Write a function that takes two or more arrays and returns a new array of unique values in the order of the original provided arrays.
In other words, all values present from all arrays should be included in their original order, but with no duplicates in the final array.
The unique numbers should be sorted by their original order, but the final array should not be sorted in numerical order.
接收若干数组参数,返回所有不重复的数组元素并按接收参数的顺序排列。
1 | function unite() { |
中级算法这个系列一共有21道题,为了避免博文篇幅过长,将中级算法系列分成两篇总结,此篇总结1-10题,下一篇总结11-21题。
欢迎留言探讨,共同学习进步。