Guest User

Untitled

a guest
Dec 13th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.84 KB | None | 0 0
  1. const assert = require("assert");
  2.  
  3. // const hierarchy = ranks => {
  4. // let result = 0;
  5. // for (let i = 0; i < ranks.length; i++) {
  6. // if (ranks.find(v => v === ranks[i] + 1)) {
  7. // result++;
  8. // }
  9. // }
  10. // return result;
  11. // };
  12.  
  13. const hierarchy = ranks =>
  14. ranks.reduce(
  15. (result, rank, iRank, ranks) =>
  16. ranks.find(rank => rank === ranks[iRank] + 1) ? result + 1 : result,
  17. 0
  18. );
  19.  
  20. // Count how many soldiers have to report to an officer (+1 rank in hierarchy)
  21. // Many soldiers may report to one officer ; cannot report if there is only officers that are more than 1 rank above
  22. assert.strictEqual(hierarchy([2, 2, 3, 0]), 2);
  23. assert.strictEqual(hierarchy([1, 2, 3]), 2);
  24. assert.strictEqual(hierarchy([0, 4, 2]), 0);
  25. assert.strictEqual(hierarchy([3, 4, 3, 0, 2, 2, 3, 0, 0]), 5);
  26. assert.strictEqual(hierarchy([4, 4, 3, 3, 1, 0]), 3);
Add Comment
Please, Sign In to add comment