2019 has come and probably everyone has noticed the peculiarity of this number: it's in fact composed by two sub-numbers (20 and 19) representing a sequence of consecutive descending numbers.
Challenge
Given a number x
, return the length of the maximum sequence of consecutive, descending numbers that can be formed by taking sub-numbers of x
.
Notes :
- sub-numbers cannot contain leading zeros (e.g.
1009
cannot be split into10
,09
) - consecutive and descending means that a number in the sequence must be equal to the previous number -1, or \$n_{i+1} = n_{i}-1\$ (e.g.
52
cannot be split into5,2
because5
and2
are not consecutive,2 ≠ 5 - 1
) - the sequence must be obtained by using the full number, e.g. in
7321
you can't discard7
and get the sequence3
,2
,1
- only one sequence can be obtained from the number, e.g.
3211098
cannot be split into two sequences3
,2
,1
and10
,9
,8
Input
- An integer number (
>= 0
) : can be a number, or a string, or list of digits
Output
- A single integer given the maximum number of decreasing sub-numbers (note that the lower-bound of this number is
1
, i.e. a number is composed by itself in a descending sequence of length one)
Examples :
2019 --> 20,19 --> output : 2
201200199198 --> 201,200,199,198 --> output : 4
3246 --> 3246 --> output : 1
87654 --> 8,7,6,5,4 --> output : 5
123456 --> 123456 --> output : 1
1009998 --> 100,99,98 --> output : 3
100908 --> 100908 --> output : 1
1110987 --> 11,10,9,8,7 --> output : 5
210 --> 2,1,0 --> output : 3
1 --> 1 --> output : 1
0 --> 0 --> output : 1
312 --> 312 --> output : 1
191 --> 191 --> output : 1
General rules:
- This is code-golf, so shortest answer in bytes wins.
Don't let code-golf languages discourage you from posting answers with non-codegolfing languages. Try to come up with an as short as possible answer for 'any' programming language. - Standard rules apply for your answer with default I/O rules, so you are allowed to use STDIN/STDOUT, functions/method with the proper parameters and return-type, full programs. Your call.
- Default Loopholes are forbidden.
- If possible, please add a link with a test for your code (i.e. TIO).
- Also, adding an explanation for your answer is highly recommended.
210 -> 2,1,0
wrong (same with0 -> 0
)? The tasks says "sub-numbers cannot contain leading zeros", is zero a special case? \$\endgroup\$212019
. Seems I didn't read all the rules. \$\endgroup\$