34
3
It is fairly simple to, given a number n
, create a range from 0
to n-1
. In fact, many languages provide this operation as a builtin.
The following CJam program reads an integer, and then prints out such a range (Try it online!):
ri,
Notice that it prints out numbers without a separator.
The Challenge
Your task is to reverse this process. You should write a program that, given a string representing a range, returns the number used to produce that range.
Specifications
- The numbers are given without any separator.
- You may assume the string forms a valid range.
- You may use 0- or 1-based indexing for your range.
- You may assume that a correct output will never exceed 32,767 (so a valid input will never have a length greater than 152,725).
- You may assume that a correct output will always be positive (so you do not have to handle 0 or negative).
This is code-golf, so the shortest competing answer (measured in bytes) wins.
Test Cases
0-indexed:
0123 -> 4
0 -> 1
0123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100 -> 101
1-indexed:
1234 -> 4
1 -> 1
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100 -> 100
Are there any descending ranges? Does it need to work for negative numbers? – Daniel – 2017-08-21T08:37:51.437
@Daniel No. Forgot to mention that; added. – Esolanging Fruit – 2017-08-21T08:41:47.790
4Do our programs really need to handle the empty string? I think it would be reasonable to allow us to ignore that. Some answers do not benefit from that rule at all. – Mr. Xcoder – 2017-08-21T09:34:14.060
Can the output be a string representation of the number i.e. taken as a substring from the original string? – user2390246 – 2017-08-21T10:04:37.440
@user2390246 Yes, that's fine. – Esolanging Fruit – 2017-08-21T18:11:41.730
@Mr.Xcoder That's fair, considering that I expect many languages' range builtins won't handle it. – Esolanging Fruit – 2017-08-21T18:15:41.247
"You may assume that a correct output will always be positive (so you do not have to handle 0 or negative)." Yet, you put
0
in the test cases! – Zacharý – 2017-08-21T18:18:04.663@Zacharý I didn't delete those when I added that rule. Edited. – Esolanging Fruit – 2017-08-21T18:18:58.623