Multisplit: Difference between revisions

Content added Content deleted
(J)
(→‎{{header|Python}}: New Python version.)
Line 48: Line 48:


=={{header|Python}}==
=={{header|Python}}==
<lang python>>>> def ms(txt="a!===b=!=c", sep=["==", "!=", "="]):
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']</lang>

'''Alternative version'''
<lang python>def min_pos(List):
<lang python>def min_pos(List):
return List.index(min(List))
return List.index(min(List))