Sobes.tech
Intern

Псевдокод sum_series(a={{1, 2}, {5, 1}}, b={{2, 4}, {3, 6}, {9, 7}}) -> {{1, 2}, {2, 6}, {3, 8}, {5, 7}, {9, 8}} def sum_series(a, b): result = [] i , j = 0, 0 val_a, val_b = 0, 0 while i < len(a) or j < len(b): a_next = a[i][0] if i < len(a) else float('inf') b_next = b[j][0] if j < len(b) else float('inf') if a_next < b_next: val_a = a[i][1] t = a[i][0] i += 1 else: val_b = b[j][1] t = b[j][0] j += 1 result.append((t, val_a + val_b)) return result

sobes.tech AI

Answer from AI

The sum_series function merges two time series, each represented as a list of (time, value) pairs. It iterates through both series, comparing timestamps, and sums the values for matching or closest times, forming a new series with combined values.

Main idea:

  • Use two pointers i and j to traverse lists a and b.
  • At each step, select the minimum time from the current elements of both series.
  • Sum the values from both series for this time (if a series lacks a value for this time, use the last known value or 0).
  • Append the result to the final list.

Example:

# Input data
series_a = [(1, 2), (5, 1)]
series_b = [(2, 4), (3, 6), (9, 7)]

# Call the function
result = sum_series(series_a, series_b)
print(result)  # [(1, 2), (2, 6), (3, 8), (5, 7), (9, 8)]

Here, at time 1, the value from a is 2, and from b there is none — sum is 2. At time 2, the value from b is 4, and from a the last known value is 2 — sum is 6, and so on.