Consecutive Cargo Load Totals
Context
A distribution hub records the cargo weight of each shipment loaded onto a truck, in loading order. The dispatcher wants a new list showing the combined weight of every shipment with the one loaded immediately after it. These are overlapping pairs: each interior shipment contributes to the pair before it and the pair after it.
Problem
Given an integer array shipments representing shipment weights in loading order, return an array containing the sum of each pair of consecutive weights. Preserve pair order from left to right. The first result uses positions 0 and 1, the next uses positions 1 and 2, and so on. Every valid neighboring pair must contribute exactly one result. If fewer than two shipments are recorded, return an empty array.
Examples
shipments = [12, 7, 15, 4][19, 22, 19]shipments = [0, -5, -5, 10][-5, -10, 5]shipments = [23][]Constraints
0 <= len(shipments) <= 1000-1000000 <= shipments[i] <= 1000000 for every i- Types:
shipmentsis int[]; result is int[]
Function signature
def consecutive_cargo_totals(shipments: list[int]) -> list[int]
shipmentslist[int]- Shipment weights listed in loading order.
- returns list[int]
- The ordered sums for every overlapping pair of consecutive shipment weights, or an empty array when no complete pair exists.
Adapted from MBPP problem task_470 (CC BY 4.0). Rewritten, extended and verified by Iksha.