Highest alternating integer sequence

Published: June 27, 2023, updated: January 1, 2025

Python

Using the integers from the problem statement, define seqs:

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

You can use the following method hais(seqs) to calculate the result:

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))

This is a sample run for the seqs array:

>>> 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