Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const assert = require("assert");
- // const hierarchy = ranks => {
- // let result = 0;
- // for (let i = 0; i < ranks.length; i++) {
- // if (ranks.find(v => v === ranks[i] + 1)) {
- // result++;
- // }
- // }
- // return result;
- // };
- const hierarchy = ranks =>
- ranks.reduce(
- (result, rank, iRank, ranks) =>
- ranks.find(rank => rank === ranks[iRank] + 1) ? result + 1 : result,
- 0
- );
- // Count how many soldiers have to report to an officer (+1 rank in hierarchy)
- // Many soldiers may report to one officer ; cannot report if there is only officers that are more than 1 rank above
- assert.strictEqual(hierarchy([2, 2, 3, 0]), 2);
- assert.strictEqual(hierarchy([1, 2, 3]), 2);
- assert.strictEqual(hierarchy([0, 4, 2]), 0);
- assert.strictEqual(hierarchy([3, 4, 3, 0, 2, 2, 3, 0, 0]), 5);
- assert.strictEqual(hierarchy([4, 4, 3, 3, 1, 0]), 3);
Add Comment
Please, Sign In to add comment