- Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathFindMissing.py
More file actions
Latest commit
44 lines (36 loc) · 1.2 KB
/
Copy pathFindMissing.py
File metadata and controls
44 lines (36 loc) · 1.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
def_ConsumeString(str_value, last, has_jumped):
"""Consumes the string value.
Args:
last: The last number that was read.
has_jumped: Whether we have skipped a number.
Returns:
A tuple of a success boolean (for whether we have read the whole string),
and the missing number.
"""
next1=str(last+1)
ifstr_value.startswith(next1):
return_ConsumeString(str_value[len(next1):], last+1, has_jumped)
elifnothas_jumped:
next2=str(last+2)
ifstr_value.startswith(next2):
success, _=_ConsumeString(str_value[len(next2):], last+2, True)
returnsuccess, last+1ifsuccesselseNone
else:
returnFalse, None
else:
returnnotstr_value, None
defFindMissing(str_value):
"""Finds the missing consecutive number in the string value."""
foriinrange(1, len(str_value) /2):
try:
first=int(str_value[0:i])
success, jumped=_ConsumeString(str_value[i:], first, False)
ifsuccess:
returnjumped
exceptValueError:
continue
return-1
printFindMissing("12345689101112")
printFindMissing("9991000100110021004")
# There's no missing number, so it should return -1.
printFindMissing("123456789101112")