Created
February 26, 2016 10:42
-
-
Save anonymous/5885857ecfeef423be7a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def temp(line,flag,pos,lvl=0): | |
if line[pos].isdigit(): | |
flag[pos]=lvl+1 | |
if pos+1<len(line): | |
return temp(line,flag,pos+1,lvl+1) | |
else: | |
return pos+1,lvl | |
else: | |
flag[pos]=0 | |
lvl=0 | |
return pos+1,lvl | |
def extract_digits(line): | |
""" | |
extract longest digit sub-str from line | |
:param line: | |
:return: | |
""" | |
flag=[0 for x in range(len(line))] | |
print flag | |
pos=0 | |
while pos<len(flag): | |
pos,lvl=temp(line,flag,pos) | |
print flag | |
length=max(flag) | |
if length>0: | |
pe=flag.index(length) | |
return line[pe-length+1:pe+1] | |
else: | |
return '' | |
if __name__=='__main__': | |
print extract_digits('tt2012g2t11111g') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment