Skip to content

Instantly share code, notes, and snippets.

@anushshukla
Created July 1, 2019 10:39
Show Gist options
  • Select an option

  • Save anushshukla/c975c4c2f4c2410e9cb01bf62779000e to your computer and use it in GitHub Desktop.

Select an option

Save anushshukla/c975c4c2f4c2410e9cb01bf62779000e to your computer and use it in GitHub Desktop.
Given a very long array of black and white balls. Assuming you can only swap any two balls at a time, please bring all the white balls to front and black balls to end.
const isWhiteBall = ball => ball === 'W';
const arrangeBlackWhiteBalls = (whiteBalls, blackBalls) => ball => {
if (isWhiteBall(ball)) whiteBalls.push(ball);
else blackBalls.push(ball);
}
const getArrangedBlackWhiteBalls = ballsArr => {
const ballsRearranged = [];
const whiteBalls = [];
const blackBalls = [];
ballsArr.map(arrangeBlackWhiteBalls(whiteBalls, blackBalls));
return whiteBalls.concat(blackBalls);
}
console.log(getArrangedBlackWhiteBalls(['W', 'B', 'W', 'B', 'B', 'B', 'W', 'B']));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment