TensorTonicTensorTonic
Problems
Study PlansProjectsNewInterviewPricingFeedback
Problems
Loading...
1 / 1

Percent Change

Time Series
Easy

Percent change measures the relative change between consecutive observations. For each index i beginning at 1, compute

pi=xi−xi−1xi−1p_i = \frac{x_i - x_{i-1}}{x_{i-1}}pi​=xi−1​xi​−xi−1​​

The numerator is the current value minus the previous value, and the denominator is the previous value. If the previous value is zero, use 0.0 for that position. Return the consecutive fractional changes as a list.

Loading visualization...

Examples

Input: series = [100, 110, 105]

Output: [0.1, -0.045455]

Explanation: The first change is 10 / 100, and the second is -5 / 110.

Input: series = [50, 100, 200]

Output: [1.0, 1.0]

Hint 1

Begin the loop at index 1 so both the current and previous values exist.

Hint 2

Check the previous value before performing the division.

Requirements

  • Compute one fractional change for each consecutive pair.
  • Divide by the previous value.
  • Use 0.0 when the previous value is zero.
  • Return a list with len(series) - 1 values.

Constraints

  • series contains at least two numeric values.
  • Time limit: 300 ms.
Try Similar Problems
DifferencingCumulative ReturnsLag FeaturesAutocorrelationSimple Moving Average

Sign in to take notes on this problem

Case 1
Case 2

Accepts: array

You must run your code first.
PrevNext

Percent Change

Time Series
Easy

Percent change measures the relative change between consecutive observations. For each index i beginning at 1, compute

pi=xi−xi−1xi−1p_i = \frac{x_i - x_{i-1}}{x_{i-1}}pi​=xi−1​xi​−xi−1​​

The numerator is the current value minus the previous value, and the denominator is the previous value. If the previous value is zero, use 0.0 for that position. Return the consecutive fractional changes as a list.

Loading visualization...

Examples

Input: series = [100, 110, 105]

Output: [0.1, -0.045455]

Explanation: The first change is 10 / 100, and the second is -5 / 110.

Input: series = [50, 100, 200]

Output: [1.0, 1.0]

Hint 1

Begin the loop at index 1 so both the current and previous values exist.

Hint 2

Check the previous value before performing the division.

Requirements

  • Compute one fractional change for each consecutive pair.
  • Divide by the previous value.
  • Use 0.0 when the previous value is zero.
  • Return a list with len(series) - 1 values.

Constraints

  • series contains at least two numeric values.
  • Time limit: 300 ms.
Try Similar Problems
DifferencingCumulative ReturnsLag FeaturesAutocorrelationSimple Moving Average

Sign in to take notes on this problem

Case 1
Case 2

Accepts: array

You must run your code first.