[Tutor] avoid split function
Kent Johnson
kent_johnson at skillsoft.com
Fri Aug 13 19:09:29 CEST 2004
Better :-) Actually this passes all of my original tests, but there is a
problem with the optional sep parameter - your algorithm depends on sep
containing the empty string. Try your examples with sep=(' ',) (a tuple
containing one string which contains a single space). Also a more natural
API would probably take a string for the sep argument and either allow any
character in the string as a separator (like what you do) or use the entire
string as the separator (which is what string.split() does...)
I feel like the troll under the bridge today, biting everyone who tries to
cross :-) I hope it is educational!
Kent
At 08:49 AM 8/13/2004 -0700, Chad Crabtree wrote:
>Not the complete functionality of ''.split() but close.
>And there is some unit tests right? I tried to be a bit more general
>to
>add in optional separators. I'm not sure why this got my goat. It
>was
>fun however.
>
>def split(astr,sep=('','\n','\t',' ')):
> m=[]
> temp=""
> if astr in sep:
> return []
> for l in astr:
> if l in sep:
> m.append(temp)
> temp=""
> else:
> temp=temp + l
> m.append(temp)
> m=[x for x in m if x not in sep] #remove blank elements
> return m
>
>print split('this is a test')
>print split('abc,de',',')
>print split('\n')
>print split(' abc')
>print split('\tabc')
>print split('\nabc')
>print split('abc ')
>print split('abc de')
>print split('')
>print split("this might be a test I don't know")
>
>
>__________________________________________________
>Do You Yahoo!?
>Tired of spam? Yahoo! Mail has the best spam protection around
>http://mail.yahoo.com
More information about the Tutor
mailing list