Faced an interview question in Python that was as follow?
ex:
input = (\'192.168.15.1\', \'.\', -1) ===> output = (192, 168, 15, 1)
input = (\'192.168.15
def split(text, sep, maxsplit=-1):
parts = []
end = -1
while True:
start = end + 1
end = text.find(sep, start)
if (end == -1) or (maxsplit == 0):
parts.append(text[start:])
break
else:
parts.append(text[start:end])
if maxsplit != 0: maxsplit -= 1
return parts
print(split('192.168.15.1', '.', -1)) # ['192', '168', '15', '1']
print(split('192.168.15.1', '.', 2)) # ['192', '168', '15.1']