Created
July 1, 2019 10:39
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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