[Tutor] A Demolished Function
Gregor Lingl
glingl@aon.at
Sat, 17 Nov 2001 11:22:07 +0100
> After writing this function, though, I still feel tense and apprehensive.
> Does anyone see any improvements one could make to make the function
> easier to read? Any criticism or dissension would be great. Thanks!
No criticism, no dissension but an alternative solution:
def cSplit(regex,stuff):
"""Split 'stuff' along 'regex' seams."""
match = regex.search(stuff)
if not match:
return [stuff]
begin, end = match.span()
if begin == 0:
return [stuff[begin:end]] + cSplit(regex,stuff[end:])
else:
return [stuff[:begin], stuff[begin:end]] + cSplit(regex,stuff[end:])
For those who like recursion. Contains some redundancy -
for sake of readability (??)
Gregor