All problems
Differencing Order to Constant
Problem
Repeatedly difference a series until all remaining values are equal, and return the number of differencing steps used. A single remaining value counts as constant and stops the process.
At most five differences are attempted; return -1 if the series is still not constant after those five steps.
Hint
A degree-d polynomial trend needs d differences to become constant.
Test cases
case 1
in: [[1,2,3,4]]
expected: 1
case 2
in: [[1,4,9,16]]
expected: 2
case 3
in: [[2,2,2]]
expected: 0
case 4
in: [[1,2,4,8,16,32,64]]
expected: -1
case 5
in: [[7]]
expected: 0
Starter codePython
def differencing_order_to_constant(series):
# Your code here
pass5 test cases · run your code against them in the editor