#!/usr/bin/env python3
import sys
text = sys.argv[1]
maxSequenceStart = -1
maxSequenceLength = 0
currentSequenceStart = -1
currentSequenceLength = 0
insideSequence = False
for pos, ch in enumerate(text):
if ch.isdigit():
if not insideSequence:
currentSequenceStart = pos
currentSequenceLength = 1
insideSequence = True
else:
currentSequenceLength += 1
if insideSequence:
if currentSequenceLength > maxSequenceLength:
maxSequenceStart = currentSequenceStart
maxSequenceLength = currentSequenceLength
if insideSequence and currentSequenceLength > maxSequenceLength:
if maxSequenceStart < 0:
print("No digits in this text!")
print("Longest digits sequence starts from index", maxSequenceStart,
"and has length of", maxSequenceLength, "digits")
$ ./max_digits_sequence.py "123 12 1234"
Longest digits sequence starts from index 7 and has length of 4 digits
#!/usr/bin/env python3
import sys
text = sys.argv[1]
maxSequenceStart = -1
maxSequenceLength = 0
currentSequenceStart = -1
currentSequenceLength = 0
insideSequence = False
for pos, ch in enumerate(text):
if ch.isdigit():
if not insideSequence:
currentSequenceStart = pos
currentSequenceLength = 1
insideSequence = True
else:
currentSequenceLength += 1
else:
if insideSequence:
insideSequence = False
if currentSequenceLength > maxSequenceLength:
maxSequenceStart = currentSequenceStart
maxSequenceLength = currentSequenceLength
if insideSequence and currentSequenceLength > maxSequenceLength:
maxSequenceStart = currentSequenceStart
maxSequenceLength = currentSequenceLength
if maxSequenceStart < 0:
print("No digits in this text!")
else:
print("Longest digits sequence starts from index", maxSequenceStart,
"and has length of", maxSequenceLength, "digits")
$ ./max_digits_sequence.py "123 12 1234"
Longest digits sequence starts from index 7 and has length of 4 digits