For learning purposes, I am trying to implement Python's built-in function split. I am new to Python, and I know that str.split is written in C. Here's the code:
import math
def split_helper(data: str, sep=','):
if data.find(sep) >= 0:
left=data[0:data.find(sep)]
right=data[data.find(sep)+len(sep):len(data)]
return (left, right)
def split_with_space(data: str, maxsplit=math.inf):
data = data.lstrip()
output = []
temp_str = ""
split_count = 0
for i in range(0,len(data)):
if(data[i] == ' ') and split_count < maxsplit:
if temp_str!="":
temp_str = temp_str.lstrip()
output.append(temp_str)
temp_str = ""
split_count+=1
else:
temp_str+=data[i]
temp_str = temp_str.lstrip()
output.append(temp_str)
temp_str = ""
return output
def split(data: str, sep=' ', maxsplit=math.inf):
data = data.lstrip()
if data == '':
return []
if sep == ' ':
return split_with_space(data, maxsplit)
output = []
split_count = 0
data_tmp = data
while data_tmp.find(sep) >= 0 and split_count < maxsplit:
left, right = split_helper(data_tmp, sep)
output.append(left)
data_tmp = right
split_count+=1
output.append(data_tmp)
return output
if __name__ == '__main__':
# Comparing split functionality to the built-in one
test_string = ''
assert split(test_string) == test_string.split()
test_string = ' '
assert split(test_string) == test_string.split()
test_string=',123,'
assert split(test_string, sep=',') == test_string.split(sep=',')
test_string='test'
assert split(test_string) == test_string.split()
test_string='Python 2 3'
assert split(test_string, maxsplit=1) == test_string.split(maxsplit=1)
test_string=' test 6 7'
assert split(test_string, maxsplit=1) == test_string.split(maxsplit=1)
test_string=' Hi 8 9'
assert split(test_string, maxsplit=0) == test_string.split(maxsplit=0)
test_string=' set 3 4'
assert split(test_string) == test_string.split()
test_string='set;:23'
assert split(test_string, sep=';:', maxsplit=0) == test_string.split(sep=';:', maxsplit=0)
test_string='set;:;:23'
assert split(test_string, sep=';:', maxsplit=2) == test_string.split(sep=';:', maxsplit=2)
Please check the implementation of the split function and its tests. If there is any suggestion, please let me know.