Skip to main content
added 560 characters in body
Source Link
Flambino
  • 33k
  • 2
  • 45
  • 90

Edit: Since I seem to be making a hash of this, here's my original method, which seems to pass all tests, however it still makes use of chunk_while, which, according to the comments, is a no-go for some reason:

def almost_increasing_sequence?(array)
  chunks = array.chunk_while(&:<).to_a

  return true if chunks.size < 2
  return false if chunks.size > 2

  head, tail = chunks

  return true if head.size == 1 || tail.size == 1

  head[-2] < tail.first || head.last < tail[1]
end

Edit: Since I seem to be making a hash of this, here's my original method, which seems to pass all tests, however it still makes use of chunk_while, which, according to the comments, is a no-go for some reason:

def almost_increasing_sequence?(array)
  chunks = array.chunk_while(&:<).to_a

  return true if chunks.size < 2
  return false if chunks.size > 2

  head, tail = chunks

  return true if head.size == 1 || tail.size == 1

  head[-2] < tail.first || head.last < tail[1]
end
added 2 characters in body
Source Link
Flambino
  • 33k
  • 2
  • 45
  • 90

My earlier answer included a section on testing, which I'll include here, since it's still relevant, regardless of the exact implementation (which is how tests should work). The only thing missing (as that came about after II'd written it), is the idea of a tolerance argument. Adding tests for that functionality is however left as an exercise to the reader.

My earlier answer included a section on testing, which I'll include here, since it's still relevant, regardless of the exact implementation (which is how tests should work). The only thing missing (as that came about after I written it), is the idea of a tolerance argument. Adding tests for that functionality is however left as an exercise to the reader.

My earlier answer included a section on testing, which I'll include here, since it's still relevant, regardless of the exact implementation (which is how tests should work). The only thing missing (as that came about after I'd written it), is the idea of a tolerance argument. Adding tests for that functionality is however left as an exercise to the reader.

added 41 characters in body
Source Link
Flambino
  • 33k
  • 2
  • 45
  • 90

which basically says, "for every two consecutive elements, count the ones that aren't increasing (i.e. sequence breaks), and if that count endends up greater than 1, the sequence can't be 'fixed'".

An early return version can however be made, for instance by using #reduce to keep track of the sequence breaks without introducing a closure variable:

which basically says, "for every two consecutive elements, count the ones that aren't increasing (i.e. sequence breaks) and if that count end up greater than 1, the sequence can't be 'fixed'".

An early return version can however be made, for instance by using #reduce to keep track of the sequence breaks:

which basically says, "for every two consecutive elements, count the ones that aren't increasing (i.e. sequence breaks), and if that count ends up greater than 1, the sequence can't be 'fixed'".

An early return version can however be made, for instance by using #reduce to keep track of the sequence breaks without introducing a closure variable:

added 1094 characters in body
Source Link
Flambino
  • 33k
  • 2
  • 45
  • 90
Loading
added 89 characters in body
Source Link
Flambino
  • 33k
  • 2
  • 45
  • 90
Loading
Source Link
Flambino
  • 33k
  • 2
  • 45
  • 90
Loading