A-M baby
This commit is contained in:
parent
764da6cbbb
commit
db842d013d
19005 changed files with 197040 additions and 7 deletions
24
Task/Multisplit/Python/multisplit-2.py
Normal file
24
Task/Multisplit/Python/multisplit-2.py
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
>>> def ms(txt="a!===b=!=c", sep=["==", "!=", "="]):
|
||||
if not txt or not sep:
|
||||
return []
|
||||
size = [len(s) for s in sep]
|
||||
ans, pos0 = [], 0
|
||||
def getfinds():
|
||||
return [(-txt.find(s, pos0), -sepnum, size[sepnum])
|
||||
for sepnum, s in enumerate(sep)
|
||||
if s in txt[pos0:]]
|
||||
|
||||
finds = getfinds()
|
||||
while finds:
|
||||
pos, snum, sz = max(finds)
|
||||
pos, snum = -pos, -snum
|
||||
ans += [ txt[pos0:pos], [snum, pos] ]
|
||||
pos0 = pos+sz
|
||||
finds = getfinds()
|
||||
if txt[pos0:]: ans += [ txt[pos0:] ]
|
||||
return ans
|
||||
|
||||
>>> ms()
|
||||
['a', [1, 1], '', [0, 3], 'b', [2, 6], '', [1, 7], 'c']
|
||||
>>> ms(txt="a!===b=!=c", sep=["=", "!=", "=="])
|
||||
['a', [1, 1], '', [0, 3], '', [0, 4], 'b', [0, 6], '', [1, 7], 'c']
|
||||
Loading…
Add table
Add a link
Reference in a new issue