A stem and leaf plot displays a bunch of numerical values in groups, which are determined by all but the last digit. For example, suppose we have this set of data:
0, 2, 12, 13, 13, 15, 16, 20, 29, 43, 49, 101
We could produce this stem and leaf plot:
0|02
1|23356
2|09
3|
4|39
5|
6|
7|
8|
9|
10|1
The first row's stem is 0, so its "leaves" - the digits after the |
- represent the values between 0 inclusive and 10 exclusive. The leaves on each stem are sorted. Stems with no leaves (like 3) still appear in the plot. The value of 101 is between 100 inclusive and 110 exclusive, so its stem is 10 (100 divided by 10).
Your challenge is to check whether a piece of text is a valid stem and leaf plot. A valid plot satisfies these rules:
- Has exactly one row for every stem (i.e. 10-wide group) in the range of the data (including stems in the middle of the range with no leaves)
- Has no stems outside the range
- All leaves are sorted ascending to the right
- All stems are sorted ascending down
- Has only numeric characters (besides the separator
|
)
You do not have to deal with numbers that have fractional parts. You may approve or reject extra leading zeros in the stems, but a blank stem is not allowed. There will be at least one value. You may only assume extra spaces after the leaves on each row. You may assume a leading and/or trailing newline. All characters will be printable ASCII.
Your function or program should return or output (to screen or the standard output) a truthy value for a valid plot, or a falsy value for an invalid plot. You may take input from the standard input, from a file, as one big string, as an array of strings - whatever is most convenient.
Here are some test cases that are valid plots (separated by blank lines):
2|00003457
3|35
4|799
5|3
99|3
100|0556
101|
102|
103|8
0|0
Here are some test cases that are invalid plots, with commentary to the right:
|0 Blank stem
5|347 Missing a stem (6) in the range
7|9
4| Has a stem (4) outside the range
5|26
6|7
11|432 Leaves aren't sorted correctly
12|9989
5|357 Stems aren't sorted correctly
4|002
6|1
4|5 Duplicate stem
4|6
4|6
5|1
51114 No stem and leaf separator
609
1|2|03 Multiple separators
2|779|
4|8abcdefg9 Invalid characters
5|1,2,3
75 | 4 6 Invalid characters (spaces)
76 | 2 8 8 9
This is code golf, so the shortest code wins! Standard loopholes are disallowed.
1|2|3
in it. \$\endgroup\$4|;5|26;6|7
which has the first stem outside the range, but instead at the end, i.e.12|3;13|4559;14|
. \$\endgroup\$