Find the runs inside an array
A run is defined as three or more numbers that increment from the previous with a constant step. For example [1,2,3]
would be a run with step 1, [1,3,5,7]
would be a run with step 2, and [1,2,4,5]
is not a run.
We can express these runs by the notation i to j by s
where i
is the first number of the run, j
is the last number of the run, and s
is the step. However, runs of step 1 will be expressed i to j
.
So using the arrays before, we get:
[1,2,3] -> "1to3"
[1,3,5,7] -> "1to7by2"
[1,2,4,5] -> "1 2 4 5"
In this challenge, it is your task to do this for arrays that may have multiple runs.
Example Python code with recursion
Input
Array of sorted positive ints (no duplicates)
Output
String of the runs separated by a space, or a string array of the runs
Does not need to be greedy in a particular direction
Can have trailing whitespace
Test Cases
In: [1000, 1002, 1004, 1006, 1008, 1010]
Out: "1000to1010by2"
In: [1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233]
Out: "1to3 5 8 13 21 34 55 89 144 233"
In: [10, 20, 30, 40, 60]
Out: "10to40by10 60"
In: [5, 6, 8, 11, 15, 16, 17]
Out: "5 6 8 11 15to17"
In: [1, 2, 3, 4, 5, 6, 7, 9, 11, 13, 15, 30, 45, 50, 60, 70, 80, 90, 91, 93]
Out: "1to7 9to15by2 30 45 50to90by10 91 93"
This is code-golf so least number of bytes wins.
[4, 5, 6, 7, 9, 11, 13, 15]
not be4to6 7to15by2
?) \$\endgroup\$