CLooker

hackerRank cut the sticks

Feb 9th, 2018
366
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // https://www.hackerrank.com/challenges/cut-the-sticks/problem
  2.  
  3. // declarative, immutable, recursive, ES6 arrow IIFE, implicit return
  4. const cutTheSticks = arr =>
  5.   (handleSticks = ({ sticks, sticksLeftPerTurn, findShortestStick }) => (
  6.     (shortestStick = findShortestStick(sticks)),
  7.     sticks.length > 0
  8.       ? handleSticks({
  9.           sticks: sticks
  10.             .map(stick => stick - shortestStick)
  11.             .filter(stick => stick > 0),
  12.           sticksLeftPerTurn: [...sticksLeftPerTurn, sticks.length],
  13.           findShortestStick
  14.         })
  15.       : sticksLeftPerTurn
  16.   ))({
  17.     sticks: [...arr],
  18.     sticksLeftPerTurn: [],
  19.     findShortestStick: stickArr => stickArr.slice(0).sort((a, b) => a - b)[0]
  20.   });
  21.  
  22. // declarative, immutable, recursive, ES6 arrow IIFE
  23. const cutTheSticks = arr =>
  24.   (handleSticks = ({ sticks, sticksLeftPerTurn, findShortestStick }) => {
  25.     const shortestStick = findShortestStick(sticks);
  26.     return sticks.length > 0
  27.       ? handleSticks({
  28.           sticks: sticks
  29.             .map(stick => stick - shortestStick)
  30.             .filter(stick => stick > 0),
  31.           sticksLeftPerTurn: [...sticksLeftPerTurn, sticks.length],
  32.           findShortestStick
  33.         })
  34.       : sticksLeftPerTurn;
  35.   })({
  36.     sticks: [...arr],
  37.     sticksLeftPerTurn: [],
  38.     findShortestStick: stickArr => stickArr.slice(0).sort((a, b) => a - b)[0]
  39.   });
  40.  
  41. // declarative, immutable, recursive, IIFE
  42. const cutTheSticks = arr =>
  43.   (function handleSticks({ sticks, sticksLeftPerTurn, findShortestStick }) {
  44.     const shortestStick = findShortestStick(sticks);
  45.     return sticks.length > 0
  46.       ? handleSticks({
  47.           sticks: sticks
  48.             .map(stick => stick - shortestStick)
  49.             .filter(stick => stick > 0),
  50.           sticksLeftPerTurn: [...sticksLeftPerTurn, sticks.length],
  51.           findShortestStick
  52.         })
  53.       : sticksLeftPerTurn;
  54.   })({
  55.     sticks: [...arr],
  56.     sticksLeftPerTurn: [],
  57.     findShortestStick: stickArr => stickArr.slice(0).sort((a, b) => a - b)[0]
  58.   });
  59.  
  60. // // declarative, immutable, recursive, function extraction
  61. const handleSticks = ({ sticks, sticksLeftPerTurn, findShortestStick }) => {
  62.   const shortestStick = findShortestStick(sticks);
  63.   return sticks.length > 0
  64.     ? handleSticks({
  65.         sticks: sticks
  66.           .map(stick => stick - shortestStick)
  67.           .filter(stick => stick > 0),
  68.         sticksLeftPerTurn: [...sticksLeftPerTurn, sticks.length],
  69.         findShortestStick
  70.       })
  71.     : sticksLeftPerTurn;
  72. };
  73. const cutTheSticks = arr =>
  74.   handleSticks({
  75.     sticks: [...arr],
  76.     sticksLeftPerTurn: [],
  77.     findShortestStick: stickArr => stickArr.slice(0).sort((a, b) => a - b)[0]
  78.   });
  79.  
  80. // imperative, immutable
  81. const cutTheSticks = arr => {
  82.   let sticks = [...arr];
  83.   let sticksLeftPerTurn = [];
  84.   while (sticks.length > 0) {
  85.     sticksLeftPerTurn = [...sticksLeftPerTurn, sticks.length];
  86.     const shortestStick = sticks.sort((a, b) => a - b)[0];
  87.     sticks = sticks
  88.       .map(stick => stick - shortestStick)
  89.       .filter(stick => stick > 0);
  90.   }
  91.   return sticksLeftPerTurn;
  92. };
  93.  
  94. // imperative, mutable
  95. function cutTheSticks(arr) {
  96.   let sticksLeftPerTurn = [];
  97.   while (arr.length > 0) {
  98.     sticksLeftPerTurn.push(arr.length);
  99.     const shortestStick = arr.sort((a, b) => a - b)[0];
  100.     let sticks = [];
  101.     for (let i = 0; i < arr.length; i++) {
  102.       sticks.push(arr[i] - shortestStick);
  103.     }
  104.     let sticksGreaterThanZero = [];
  105.     for (let i = 0; i < sticks.length; i++) {
  106.       if (sticks[i] > 0) {
  107.         sticksGreaterThanZero.push(sticks[i]);
  108.       }
  109.     }
  110.     arr = sticksGreaterThanZero;
  111.   }
  112.   return sticksLeftPerTurn;
  113. }
Add Comment
Please, Sign In to add comment