Problem 243099 · easy · Phase 02 Linear Data Structures

Daily Changes

arrays · iteration · neighbours

closes holds the closing price of a stock on consecutive days. Return a list of the day-to-day changes: element i of the answer is closes[i + 1] - closes[i]. The result has one fewer element than the input (and is empty when there are fewer than two days).

Examples

Input:  closes = [10, 12, 9, 9]
Output: [2, -3, 0]

Input:  closes = [5]
Output: []

Constraints

  • 0 <= len(closes) <= 10**5
  • Target: O(n) time.

Goals

  • Produce a list one element shorter than the input
  • Index neighbouring pairs safely
Starting Python…