[7 kyu] Power of twoForm The Minimum
Given a list of digits, return the smallest number that could be formed from these digits, using the digits only once (ignore duplicates).
Notes:
Only positive integers will be passed to the function (> 0 ), no negatives or zeros.
Input >> Output Examples
minValue ({1, 3, 1}) ==> return (13)
翻译:
给定一个数字列表,返回由这些数字组成的最小数字,只使用一次数字(忽略重复数字)。
笔记:
只有正整数将被传递给函数(>0),没有负数或零。
解:
function minValue(values){
//your code here
return +(Array.from(new Set(values))).sort((a,b)=>a-b).join('');
}
[5 kyu] Sum of Pairs
Given a list of integers and a single sum value, return the first two values (parse from the left please) in order of appearance that add up to form the sum.
If there are two or more pairs with the required sum, the pair whose second element has the smallest index is the solution.
sum_pairs([11, 3, 7, 5], 10)--------------------------- 3 + 7 = 10== [3, 7]
sum_pairs([4, 3, 2, 3, 4], 6)-------------------------- 4 + 2 = 6, indices: 0, 2
--------------------------3 + 3 = 6, indices: 1, 3
--------------------------2 + 4 = 6, indices: 2, 4
the correct answer is the pair whose second value has the smallest index
== [4, 2]
sum_pairs([0, 0, -2, 3], 2)
there are no pairs of values that can be added to produce 2.
== None/nil/undefined (Based on the language)
sum_pairs([10, 5, 2, 3, 7, 5], 10)--------------------------5 + 5 = 10, indices: 1, 5
--------------------------3 + 7 = 10, indices: 3, 4 *
the correct answer is the pair whose second value has the smallest index
== [3, 7]
Negative numbers and duplicate numbers can and will appear.
翻译:
给定一个整数列表和一个单一的和值,按照相加形成和的顺序返回前两个值(请从左边解析)。
如果有两个或多个具有所需总和的对,则第二个元素具有最小索引的对就是解。
注:还将测试长度超过10000000个元素的列表。确保代码不会超时。
解一:
var sum_pairs=function(ints, s){
var seen = {}
for (var i = 0; i < ints.length; ++i) {
if (seen[s - ints[i]]) return [s - ints[i], ints[i]];
seen[ints[i]] = true
}
}
解二:
function sum_pairs(ints, s) {
let seen = new Set();
for (let i of ints) {
if (seen.has(s - i)) return [s - i, i];
seen.add(i);
}
}
[7 kyu] Switcheroo
Given a string made up of letters a, b, and/or c, switch the position of letters a and b (change a to b and vice versa). Leave any incidence of c untouched.
Example:
'acb' --> 'bca'
'aabacbaa' --> 'bbabcabb'
翻译:
描述:
给定一个由字母a、b和/或c组成的字符串,切换字母a和b的位置(将a改为b,反之亦然)。保持c的发生率不变。
解:
function switcheroo(x){
return x.replace(/a/g,'d').replace(/b/g,'a').replace(/d/g,'b');
}
[6 kyu] Take a Ten Minutes Walk
You live in the city of Cartesia where all roads are laid out in a perfect grid. You arrived ten minutes too early to an appointment, so you decided to take the opportunity to go for a short walk. The city provides its citizens with a Walk Generating App on their phones -- everytime you press the button it sends you an array of one-letter strings representing directions to walk (eg. ['n', 's', 'w', 'e']). You always walk only a single block for each letter (direction) and you know it takes you one minute to traverse one city block, so create a function that will return true if the walk the app gives you will take you exactly ten minutes (you don't want to be early or late!) and will, of course, return you to your starting point. Return false otherwise.
Note: you will always receive a valid array containing a random assortment of direction letters ('n', 's', 'e', or 'w' only). It will never give you an empty array (that's not a walk, that's standing still!).
翻译:
你住在卡泰西亚市,那里所有的道路都以完美的网格布局。你提前十分钟到达约会,所以你决定趁机去散散步。这座城市为市民的手机提供了一个步行生成应用程序——每次你按下按钮,它都会向你发送一个表示步行方向的单字母字符串数组(例如['n','s','w','e')。对于每个字母(方向),你总是只走一个街区,你知道你要花一分钟才能穿过一个城市街区,所以创建一个函数,如果应用程序给你的步行时间正好是十分钟(你不想早或晚!)当然,这会让你回到起点。否则返回false。
注意:您将始终收到一个有效的数组,其中包含随机分类的方向字母(仅'n'、's'、'e'或'w')。它永远不会给你一个空数组(这不是散步,而是站着不动!)。
解一:
function isValidWalk(walk) {
let x=0
let y=0
if(walk.length!=10){
return false
}
for(let i=0;i<walk.length;i++){
if(walk[i]=='n'){y++ }
if(walk[i]=='s'){y--}
if(walk[i]=='e'){x++}
if(walk[i]=='w'){x--}
}
return x==0 && y==0
}
解二:
//用长度来计算
function isValidWalk(walk) {
const north = walk.filter(item => { return item === "n" }).length;
const south = walk.filter(item => { return item === "s" }).length;
const east = walk.filter(item => { return item === "e" }).length;
const west = walk.filter(item => { return item === "w" }).length;
return walk.length === 10 && north === south && east === west;
}
[7 kyu] Sum of Triangular Numbers
Your task is to return the sum of Triangular Numbers up-to-and-including the nth Triangular Number.
Triangular Number: "any of the series of numbers (1, 3, 6, 10, 15, etc.) obtained by continued summation of the natural numbers 1, 2, 3, 4, 5, etc."
[01]
02 [03]
04 05 [06]
07 08 09 [10]
11 12 13 14 [15]
16 17 18 19 20 [21]
e.g. If 4 is given: 1 + 3 + 6 + 10 = 20.
Triangular Numbers cannot be negative so return 0 if a negative number is given.
翻译:
您的任务是返回三角形数之和,包括第n个三角形数。
三角数:“自然数1、2、3、4、5等的连续求和所得到的任何一系列数(1、3、6、10、15等)。”
三角数不能为负数,因此如果给定负数,则返回0。
解一:
function sumTriangularNumbers(n) {
var sum = 0;
for(var i = 1; i <= n; i++)
{
sum += (i*(i+1))/2;
}
return sum;
}
解二:
function sumTriangularNumbers(n) {
return n < 0 ? 0 : n * (n + 1) * (n + 2) / 6;
}