Last active
December 28, 2018 16:46
-
-
Save pete-murphy/0f41463c22583bed340f2587ec45e311 to your computer and use it in GitHub Desktop.
Almost increasing sequence challenge from CodeSignal
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
| // Tests if an array is strictly increasing | |
| const isIncreasing = xs => | |
| xs.reduce((acc, x, i) => (i > 0 ? acc && x > xs[i - 1] : true), true) | |
| // Drops the element at a given index from a given array | |
| const dropAtIndex = i => xs => xs.filter((_, j) => i !== j) | |
| // Returns all the elements except for the first | |
| const tail = ([_, ...xs]) => xs | |
| function almostIncreasingSequence(sequence) { | |
| const i = tail(sequence).findIndex((x, j) => x <= sequence[j]) | |
| return ( | |
| isIncreasing(dropAtIndex(i)(sequence)) || | |
| isIncreasing(dropAtIndex(i + 1)(sequence)) | |
| ) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment