[7 kyu] Row Weights
Scenario
Several people are standing in a row divided into two teams.
The first person goes into team 1, the second goes into team 2, the third goes into team 1, and so on.
Task
Given an array of positive integers (the weights of the people), return a new array/tuple of two integers, where the first one is the total weight of team 1, and the second one is the total weight of team 2.
Input >> Output Examples
rowWeights([13, 27, 49]) ==> return (62, 27)
翻译:
几个人分成两队站成一排。
第一个人进入第一队,第二个人进入第二队,第三个人进入第一组,依此类推。
任务
给定一个正整数数组(人员的权重),返回一个由两个整数组成的新数组/元组,其中第一个是团队1的总权重,第二个是团队2的总权重。
解:
function rowWeights(array){
let odd = array.filter((x, index) => index % 2 == 0)
let even = array.filter((x, index) => index % 2 != 0)
return [odd.reduce((a, b) => a + b, 0), even.reduce((a, b) => a + b, 0)]
}
[8 kyu] ES6 string addition
It is easy to join two strings together like this string1 + string2.
Another way to get the desired result would be with string1.concat(string2)
ES6 has introduced another way of joining strings. Your task is to find out what this is and write a function that will add two strings together, there must be a space between the two strings.
- , .concat() & .join() will not be allowed in this exercise. I have also removed some other methods that can be used to cheat!
If one of the arguments is a number your code must coerce it into being a string.
翻译:
很容易将两个字符串连接在一起,如string1+string2。
获得所需结果的另一种方法是使用string1.concat(string2)
ES6引入了另一种连接字符串的方法。您的任务是找出这是什么,并编写一个将两个字符串相加的函数,这两个字符串之间必须有空格。
+在本练习中不允许使用、.concat()和.join()。我还删除了一些其他可以用来作弊的方法!
如果其中一个参数是数字,则代码必须将其强制为字符串
解:
// 题目规定不能用 ’+‘ 、 join()、concat() 方法
function joinStrings(string1, string2){
return `${string1} ${string2}`
}
[7 kyu] Tidy Number (Special Numbers Series #9)
Definition
A Tidy number is a number whose digits are in non-decreasing order.
Task
Given a number, Find if it is Tidy or not .
翻译:
Tidy数字是指数字按非递减顺序排列的数字。
任务
给定一个数字,找出它是否是Tidy数字。
解一:
const tidyNumber = n => {
let s = n.toString();
for (let i = 0; i < s.length-1; i++) {
if (s[i] > s[i+1]) return false;
}
return true;
}
解二:
function tidyNumber(n){
return [...n+=""].sort().join``==n
}
[7 kyu] Over The Road
Task
You've just moved into a perfectly straight street with exactly n identical houses on either side of the road. Naturally, you would like to find out the house number of the people on the other side of the street. The street looks something like this:
Street
1| |6
3| |4
5| |2
you
Evens increase on the right; odds decrease on the left. House numbers start at 1 and increase without gaps. When n = 3, 1 is opposite 6, 3 opposite 4, and 5 opposite 2.
Example (address, n --> output)
Given your house number address and length of street n, give the house number on the opposite side of the street.
1, 3 --> 6
3, 3 --> 4
2, 3 --> 5
3, 5 --> 8
Note about errors
If you are timing out, running out of memory, or get any kind of "error", read on. Both n and address could get upto 500 billion with over 200 random tests. If you try to store the addresses of 500 billion houses in a list then you will run out of memory and the tests will crash. This is not a kata problem so please don't post an issue. Similarly if the tests don't complete within 12 seconds then you also fail.
To solve this, you need to think of a way to do the kata without making massive lists or huge for loops. Read the discourse for some inspiration :)
解:
function overTheRoad(address, n){
return (n*2+1)-address;
}
[7 kyu] Functional Addition
Create a function add(n)/Add(n) which returns a function that always adds n to any number
Note for Java: the return type and methods have not been provided to make it a bit more challenging.
var addOne = add(1);
addOne(3); // 4
var addThree = add(3);
addThree(3); // 6
翻译:
您收到一个数字N。返回一个函数,该函数接受一个参数M并返回它们的和(即N+M)。
解:
function add(n) {
return function (m) {
return n+m;
}
}
[8 kyu] Leonardo Dicaprio and Oscars
You have to write a function that describe Leo:
function leo(oscar) {
}
if oscar was (integer) 88, you have to return "Leo finally won the oscar! Leo is happy".
if oscar was 86, you have to return "Not even for Wolf of wallstreet?!"
if it was not 88 or 86 (and below 88) you should return "When will you give Leo an Oscar?"
if it was over 88 you should return "Leo got one already!"
翻译:
你必须写一个描述Leo的函数:
function leo(oscar){
}
如果奥斯卡是(整数)88,你必须返回“利奥终于赢得了奥斯卡!利奥很高兴”。
如果奥斯卡是86岁,你必须回答“即使是华尔街之狼也不行?!”
如果不是88岁或86岁(88岁以下),你应该返回“你什么时候给利奥奥斯卡奖?”
如果超过88,你应该返回“利奥已经有了!”
解:
const leo = (oscar) => {
return oscar === 88 ? 'Leo finally won the oscar! Leo is happy' :
oscar === 86 ? 'Not even for Wolf of wallstreet?!' :
oscar < 88 ? 'When will you give Leo an Oscar?' :
'Leo got one already!';
};