Skip to content

Instantly share code, notes, and snippets.

@pete-murphy
Last active December 28, 2018 16:46
Show Gist options
  • Select an option

  • Save pete-murphy/0f41463c22583bed340f2587ec45e311 to your computer and use it in GitHub Desktop.

Select an option

Save pete-murphy/0f41463c22583bed340f2587ec45e311 to your computer and use it in GitHub Desktop.
Almost increasing sequence challenge from CodeSignal
// 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