Skip to main content
removed debug `print`
Source Link
kyrill
  • 1.6k
  • 11
  • 24
def almost_increasing_sequence?(sequence)
  return false if sequence.length  < 2
  return true  if sequence.length == 2

  max1 = sequence[0]  # maximum up to the previous item
  max2 = sequence[1]  # maximum up to the current item
  count = 0

  sequence[1..-1].each_with_index do |item, index|
    prev = sequence[index]
    nxt  = sequence[index+2]

    print(max1,' ',max2,' ',prev,' ',item,' ',nxt,"\n")

    if prev >= item
      count += 1
      return false if count > 1
      if index > 0 and item <= max1 and nxt and nxt <= max2
        return false
      end
    end

    max1 = prev if prev > max1
    max2 = item if item > max2
  end

  return count == 1
end
def almost_increasing_sequence?(sequence)
  return false if sequence.length  < 2
  return true  if sequence.length == 2

  max1 = sequence[0]  # maximum up to the previous item
  max2 = sequence[1]  # maximum up to the current item
  count = 0

  sequence[1..-1].each_with_index do |item, index|
    prev = sequence[index]
    nxt  = sequence[index+2]

    print(max1,' ',max2,' ',prev,' ',item,' ',nxt,"\n")

    if prev >= item
      count += 1
      return false if count > 1
      if index > 0 and item <= max1 and nxt and nxt <= max2
        return false
      end
    end

    max1 = prev if prev > max1
    max2 = item if item > max2
  end

  return count == 1
end
def almost_increasing_sequence?(sequence)
  return false if sequence.length  < 2
  return true  if sequence.length == 2

  max1 = sequence[0]  # maximum up to the previous item
  max2 = sequence[1]  # maximum up to the current item
  count = 0

  sequence[1..-1].each_with_index do |item, index|
    prev = sequence[index]
    nxt  = sequence[index+2]

    if prev >= item
      count += 1
      return false if count > 1
      if index > 0 and item <= max1 and nxt and nxt <= max2
        return false
      end
    end

    max1 = prev if prev > max1
    max2 = item if item > max2
  end

  return count == 1
end
added 1165 characters in body
Source Link
kyrill
  • 1.6k
  • 11
  • 24
def almost_increasing_sequence?(sequence)
  return false if sequence.length  < 2
  return true  if sequence.length == 2

  max1 = sequence[0]  # maximum up to the previous item
  max2 = sequence[1]  # maximum up to the current item
  count = 0

  sequence[1..-1].each_with_index do |item, index|
    prev = sequence[index]
    nxt  = sequence[index+2]

    print(max1,' ',max2,' ',prev,' ',item,' ',nxt,"\n")

    if prev >= item
      count += 1
      return false if count > 1
      if index > 0 and item <= max1 and nxt and nxt <= max2
        return false
      end
    end

    max1 = prev if prev > max1
    max2 = item if item > max2
  end

  return count == 1
end

Some tests courtesy of @AJFaraday (corrected & extended):

[
  [[], false],
  [[1], false],
  [[1, 2], true],
  [[1, 2, 3, 4, 99, 5, 6], true],
  [[1, 3, 2], true],
  [[10, 1, 2, 3, 4, 5], true],
  [[0, -2, 5, 6], true],
  [[1, 2, 3, 4, 3, 6], true],
  [[1, 1], true],
  [[100, 200, 300, 400, 99, 500, 600], true],
  [[1, 2, 1, 2], false],
  [[1, 2, 3, 4, 5, 3, 5, 6], false],
  [[40, 50, 60, 10, 20, 30], false],
  [[1, 3, 2, 1], false],
  [[1, 4, 10, 4, 2] , false],
  [[1, 1, 1, 2, 3], false],
  [[10, 1, 2, 3, 4, 5, 6], true],
  [[5, 7, 8, 90, 91, 92, 93], false],
  [[2,3,1,2], false],
  [[1,2,1,2], false],
  [[1, 2, 3, 4, 5, 3, 5, 6], false],
  [[40, 50, 60, 10, 20, 30], false]
].each do |array, expected|
  if almost_increasing_sequence?(array) != expected
    puts "FAIL Should get #{expected} for #{array}"
  else
    puts "PASS"
  end
end
def almost_increasing_sequence?(sequence)
  max1 = sequence[0]  # maximum up to the previous item
  max2 = sequence[1]  # maximum up to the current item
  count = 0

  sequence[1..-1].each_with_index do |item, index|
    prev = sequence[index]
    nxt  = sequence[index+2]

    if prev >= item
      count += 1
      if index > 0 and item <= max1 and nxt and nxt <= max2
        return false
      end
    end

    max1 = prev if prev > max1
    max2 = item if item > max2
  end

  return count == 1
end
def almost_increasing_sequence?(sequence)
  return false if sequence.length  < 2
  return true  if sequence.length == 2

  max1 = sequence[0]  # maximum up to the previous item
  max2 = sequence[1]  # maximum up to the current item
  count = 0

  sequence[1..-1].each_with_index do |item, index|
    prev = sequence[index]
    nxt  = sequence[index+2]

    print(max1,' ',max2,' ',prev,' ',item,' ',nxt,"\n")

    if prev >= item
      count += 1
      return false if count > 1
      if index > 0 and item <= max1 and nxt and nxt <= max2
        return false
      end
    end

    max1 = prev if prev > max1
    max2 = item if item > max2
  end

  return count == 1
end

Some tests courtesy of @AJFaraday (corrected & extended):

[
  [[], false],
  [[1], false],
  [[1, 2], true],
  [[1, 2, 3, 4, 99, 5, 6], true],
  [[1, 3, 2], true],
  [[10, 1, 2, 3, 4, 5], true],
  [[0, -2, 5, 6], true],
  [[1, 2, 3, 4, 3, 6], true],
  [[1, 1], true],
  [[100, 200, 300, 400, 99, 500, 600], true],
  [[1, 2, 1, 2], false],
  [[1, 2, 3, 4, 5, 3, 5, 6], false],
  [[40, 50, 60, 10, 20, 30], false],
  [[1, 3, 2, 1], false],
  [[1, 4, 10, 4, 2] , false],
  [[1, 1, 1, 2, 3], false],
  [[10, 1, 2, 3, 4, 5, 6], true],
  [[5, 7, 8, 90, 91, 92, 93], false],
  [[2,3,1,2], false],
  [[1,2,1,2], false],
  [[1, 2, 3, 4, 5, 3, 5, 6], false],
  [[40, 50, 60, 10, 20, 30], false]
].each do |array, expected|
  if almost_increasing_sequence?(array) != expected
    puts "FAIL Should get #{expected} for #{array}"
  else
    puts "PASS"
  end
end
added 203 characters in body
Source Link
kyrill
  • 1.6k
  • 11
  • 24

All of the provided implementations fail for [2,3,1,2].

Basically you want something like:Here is a (Python-like pseudodode)

THIS IS NOT COMPLETELY CORRECTcorrect and efficient solution with \$O(n)\$ time complexity:

countdef almost_increasing_sequence?(sequence)
  max1 = 0sequence[0]  # maximum up to the previous item
for i max2 = 1sequence[1]  # maximum up to length(arr)-1:the current item
  ifcount arr[i= 0

  sequence[1..-1].each_with_index >=do arr[i]:|item, index|
    countprev +== 1sequence[index]
    #nxt Preceding number= issequence[index+2]

 greater than this oneif --prev make>= sureitem
    # all numberscount before+= the1
 preceding are less than this one.if index > 0 and item <= max1 and nxt and nxt <= max2
    #  !! FAILS forreturn [1,2,1,3]false
    for j =end
 0 to i-2: end

    max1 = prev if arr[j]prev >=> arr[i]:max1
    max2 = item if returnitem false> max2
  end

  return count == 1
end

All of the provided implementations fail for [2,3,1,2].

Basically you want something like: (Python-like pseudodode)

THIS IS NOT COMPLETELY CORRECT

count = 0
for i = 1 to length(arr)-1:
  if arr[i-1] >= arr[i]:
    count += 1
    # Preceding number is greater than this one -- make sure
    # all numbers before the preceding are less than this one.
    #  !! FAILS for [1,2,1,3]
    for j = 0 to i-2:
      if arr[j] >= arr[i]:
        return false
return count == 1

All of the provided implementations fail for [2,3,1,2].

Here is a correct and efficient solution with \$O(n)\$ time complexity:

def almost_increasing_sequence?(sequence)
  max1 = sequence[0]  # maximum up to the previous item
  max2 = sequence[1]  # maximum up to the current item
  count = 0

  sequence[1..-1].each_with_index do |item, index|
    prev = sequence[index]
    nxt  = sequence[index+2]

    if prev >= item
      count += 1
      if index > 0 and item <= max1 and nxt and nxt <= max2
        return false
      end
    end

    max1 = prev if prev > max1
    max2 = item if item > max2
  end

  return count == 1
end
added 75 characters in body
Source Link
kyrill
  • 1.6k
  • 11
  • 24
Loading
Added pseudocode example
Source Link
kyrill
  • 1.6k
  • 11
  • 24
Loading
Post Undeleted by kyrill
Post Deleted by kyrill
Source Link
kyrill
  • 1.6k
  • 11
  • 24
Loading