23
3
Consider a one-dimensional sequence of numbers within a fixed range, i.e.
[1, 2, 4, 6, 8, 0, 2, 7, 3] in range [0, 10⟩
The Ever-Increasing Graph* ** is a line that connects all the points in this sequence left to right, and always goes upwards or stays level. If necessary, the line wraps around from top to bottom and continues going up from there to meet the next point.
The goal of this challenge is to split the sequence in different subsequences that are all nondecreasing, so that when plotted together with a limited vertical axis they will form an Ever-Increasing Graph. This is done by adding an a point to the end of one subsequence and to the beginning of the next subsequence, so that the angle of the line that crosses the top boundary aligns with the line that crosses the bottom boundary, and the two crossing points have the same horizontal coordinate. The example above would give the following output:
[1, 2, 4, 6, 8, 10]
[-2, 0, 2, 7, 13]
[-3, 3]
And the corresponding graph will look as follows:
And with the axis extended for a better view:
The required output is a list of subsequences that form the parts of the Ever-Increasing Graph. Making a plot is not required but will earn you bonus points ;). The output must clearly separate the subsequences in some way.
Notes
- The range will be always have zero as the left (inclusive) boundary, and the right boundary will be some integer N.
- The sequence will never contain values that are not within the range.
- The first subsequence does not have an additional point at the beginning.
- The last subsequence does not have an additional point at the end.
- It is not required to provide the starting indices that would be required to plot the subsequences.
Test cases
Input: [0, 2, 4, 6, 1, 3, 5, 0], 7
Output: [0, 2, 4, 6, 8], [-1, 1, 3, 5, 7], [-2, 0]
Input: [1, 1, 2, 3, 5, 8, 3, 1], 10
Output: [1, 1, 2, 3, 5, 8, 13],[-2, 3, 11],[-7, 1]
Input: [5, 4, 3, 2, 1], 10
Output: [5, 14],[-5, 4, 13],[-6, 3, 12],[-7, 2, 11],[-8, 1]
Input: [0, 1, 4, 9, 16, 15, 0], 17
Output: [0, 1, 4, 9, 16, 32], [-1, 15, 17], [-2, 0]
Scoring
This is code-golf, the shortest code in bytes wins.
*Not actual jargon **Actually should be called Ever Non-Decreasing Graph, as @ngm pointed out, but that sounds less impressive.
7Welcome to PPCG! Nice first challenge! – AdmBorkBork – 2018-05-18T12:15:15.673
1Looks like that I misunderstood some part of the challenge. I think this should be what you intended. – user202729 – 2018-05-18T13:22:18.670
1Can you extend the y axis in your sample graph below 0 and above 10 to make the challenge easier to understand? – JayCe – 2018-05-18T15:20:59.237
@JayCe Yes, good suggestion. – RvdV – 2018-05-18T16:11:24.733
2The second test case suggests you intend the sequences to be non-decreasing, as opposed to increasing? In other words, a repeated value in the input doesn't stop that current subsequence, and if the last two values in a subsequence are equal than the "angle" to start the next subsequence is 0 (so it would start with a repeated value as well)? – ngm – 2018-05-18T18:27:00.313
@ngm you are correct, non-decreasing is the correct term! I didn't think about this and will edit the question. – RvdV – 2018-05-19T09:43:03.900
It would be good if you could also make that clear in the title. – None – 2018-05-20T08:50:07.663
Do
[1,1],2
be one or two part? – l4m2 – 2018-05-20T09:33:59.883@l4m2 It is a nondecreasing sequence, so the output would be just one sequence: [1,1] – RvdV – 2018-05-23T07:32:10.510