思路就是先找到分割的位置,那么就需要前面的split函数,接着分割即可
def my_find ( source, target, start= 0 ) :
if not source or not target:
return - 1
if len ( target) > len ( source) :
return - 1
if start < 0 or start >= len ( source) :
return - 1
for index in range ( start, len ( source) - len ( target) + 1 ) :
t_index = 0
while t_index < len ( target) :
if target[ t_index] == source[ t_index+ index] :
t_index += 1
else :
break
if t_index == len ( target) :
return index
return - 1
def my_split ( source, sep, maxsplit= - 1 ) :
if not source or not sep:
return [ ]
lst = [ ]
max_split_count = maxsplit if maxsplit > 0 else len ( source)
split_count = 0
start_index = 0
index = my_find( source, sep, start = start_index)
while split_count < max_split_count and index != - 1 :
sep_str = source[ start_index: index]
lst. append( sep_str)
split_count += 1
start_index = index + len ( sep)
index = my_find( source, sep, start= start_index)
sep_str = source[ start_index: ]
lst. append( sep_str)
return lst
if __name__ == "__main__" :
print ( my_split( "1,3,4" , "," ) )
print ( my_split( "1,,3,,4" , ",," ) )
print ( my_split( "abcadae" , "a" ) )
print ( my_split( "abcadae" , "a" , maxsplit= 2 ) )
print ( my_split( "aaaa" , "a" ) )