https://www.codewars.com/kata/5648b12ce68d9daa6b000099
Codewars - Achieve mastery through coding practice and developer mentorship
Coding practice for all programming levels – Join a community of over 3 million developers and improve your coding skills in over 55 programming languages!
www.codewars.com
DESCRIPTION:
There is a bus moving in the city, and it takes and drop some people in each bus stop.
You are provided with a list (or array) of integer pairs. Elements of each pair represent number of people get into bus (The first item) and number of people get off the bus (The second item) in a bus stop.
Your task is to return number of people who are still in the bus after the last bus station (after the last array). Even though it is the last bus stop, the bus is not empty and some people are still in the bus, and they are probably sleeping there :D
Take a look on the test cases.
Please keep in mind that the test cases ensure that the number of people in the bus is always >= 0. So the return integer can't be negative.
The second value in the first integer array is 0, since the bus is empty in the first bus stop.
설명:
시내를 이동하는 버스가 있고 각 버스 정류장에서 사람들이 내립니다.
정수 쌍의 리스트(또는 배열)가 제공됩니다. 각 쌍의 원소는 버스 정류장에서 버스에 타는 사람들의 수(첫 번째 항목)와 버스에서 내리는 사람의 수(두 번째 항목)를 나타냅니다.
마지막 버스 정류장(마지막 배열 후)을 지난 후에도 버스에 남아있는 사람의 수를 반환해야 합니다. 마지막 정류장인데도 버스가 비어있지 않고 사람들이 있다는 것은 그들이 아마 그곳에서 자고 있기 때문이겠죠 :D
테스트 케이스를 보세요.
테스트 케이스는 버스안에 있는 사람의 수가 항상 >=0 임을 보장한다는 것을 기억해두세요. 그러므로 반환되는 정수는 음수일 수 없습니다.
첫 번째 정류장에서 버스가 비어있으므로 첫 정수 배열의 두 번째 값은 0입니다.
풀이
var number = function(busStops){
let result = 0;
for(let i = 0; i < busStops.length; i++){
result += busStops[i][0] - busStops[i][1];
}
return result;
}
busStops의 첫 번째 항목에서 두번째 항목을 뺀 값을 결과 값에 계속 더해서 출력해준다.
다른 사람의 좋았던 풀이
const number = (busStops) => busStops.reduce((rem, [on, off]) => rem + on - off, 0);
'알고리즘 > Codewars' 카테고리의 다른 글
[Codewars] Returning Strings (8 kyu) / JavaScript (0) | 2022.10.14 |
---|---|
[Codewars] Are they the "same"? (6 kyu) / JavaScript (0) | 2022.10.13 |
[Codewars] Return Negative (8 kyu) / JavaScript (0) | 2022.10.12 |
[Codewars] Convert a Boolean to a String (8 kyu) / JavaScript (0) | 2022.10.12 |
[Codewars] Sum Arrays (8 kyu) / JavaScript (0) | 2022.10.12 |
댓글