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
This pseudocode implements merging two time series, represented as lists of pairs (time, value). The goal is to obtain a combined series where, for each unique time, the values from both series are summed.
Algorithm 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 lists.
- Update the corresponding value val_a or val_b.
- Append a tuple (time, val_a + val_b) to the result list.
Python example:
from typing import List, Tuple
def sum_series(a: List[Tuple[int, int]], b: List[Tuple[int, int]]) -> List[Tuple[int, int]]:
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
# Example call
print(sum_series([(1, 2), (5, 1)], [(2, 4), (3, 6), (9, 7)]))
# Output: [(1, 2), (2, 6), (3, 8), (5, 7), (9, 8)]
This approach is useful for merging and summing data over time from different sources.