Highest alternating integer sequence

Published:
June 27, 2023

Python

Using the integers from the problem statement:

seqs = [
  [0, 1, 2, 3],
  [4, 7, 8, 9],
  [10, 12, 14, 15, 17],
]

We write the following function:

def hais(seqs):
  results = [0] * len(seqs)
  for ix, seq in enumerate(seqs):
    result = seq[0]
    add = True
    for i in seq[1:]:
      if add:
        result += i
      else:
        result -= i
      add = not add
    results[ix] = result
  return results.index(max(results))

Then, we can get the index for our example data:

>>> hais(seqs)
1
Go back to the problem.

I would be thrilled to hear from you! Please share your thoughts and ideas with me via email.

Back to Index