19
1
The challenge is to identify the missing number in a string of undelimited integers.
You are given a string of digits (valid input will match the regular expression ^[1-9][0-9]+$
). The string represents a sequence of integers. For example, 1234567891011
. All numbers in the sequence are in the range from 1
and 2147483647
inclusive.
The sequence is a series of numbers where each number is one greater than its predecessor. However, this sequence may contain one and only one missing number from the sequence. It is possible that a given string also contains no missing numbers from the sequence. The string will always contain at least two numbers from the sequence.
The code must output or return the missing value, or 0
(this is a 0
- not a falsy value) in the event that the no missing values were found.
The following are valid inputs and their output/return:
input output actual sequence (for refrence)
123467 5 1 2 3 4 _ 6 7
911 10 9 __ 11
123125126 124 123 ___ 125 126
8632456863245786324598632460 8632458 8632456 8632457 _______ 8632459 8632460
123 0 1 2 3
8632456863245786324588632459 0 8632456 8632457 8632458 8632459
While all of this is described as a 'string' as input, if the language is capable of handling arbitrarily large numbers (dc
and mathematica
, I'm looking at you two) the input may be an arbitrarily large number instead of a string if that makes the code easier.
For reference, this was inspired by the Programmers.SE question: Find missing number in sequence in string
4Are you sure this is unambiguous? – Martin Ender – 2016-02-16T20:36:16.697
@MartinBüttner I've thought a bit about it and haven't been able to come up with a situation where a sequence increasing by 1 (that might be the problem) has an ambiguous situation. – None – 2016-02-16T20:37:16.460
Is there an entry in OEIS for the list of integers that are a concatenated sequence missing exactly one element? – mbomb007 – 2016-02-16T22:20:13.110
@mbomb007 I don't think so as there are infinitely many different lists. And that this is just one big ole string. Not sure how you'd define it. For that matter, an interesting CS question would be "what is the language that accepts all these strings". Its certainly not regular. I doubt its CF. – None – 2016-02-16T22:22:32.893
They would be sorted, of course. A032607 is similar, but is only a subset of the sequence I'm suggesting.
– mbomb007 – 2016-02-16T22:26:09.313The sequence would be: 13,24,35,46,57,68,79,124,134,235,245,...,679,689,1012,1113,1214,1235,... – mbomb007 – 2016-02-16T22:54:19.437
@mbomb007 ahh. Ok, I understand that now. That's a rather interesting sequence. I was looking at the 689 to 1012 gap and wondering if there was anything there and then realized, no, there isn't. – None – 2016-02-16T23:03:43.437
Actually, you're right. I forgot 810 and 911. Thanks. – mbomb007 – 2016-02-18T20:28:35.617
1
I made the sequence the subject of a challenge: http://codegolf.stackexchange.com/q/73513/34718
– mbomb007 – 2016-02-18T21:27:01.340