How to Create Your Own Python Split Function

The challenge

Write your own implementation of the built-in split function in Python.

The following rules must be adhered to:

  • the function cannot use, in any way, the original split or rsplit functions,
  • the new function must be a generator,
  • it should behave as the built-in split, so it will be tested that way — think of split() and split('')

The solution in Python

Option 1:

import re def my_very_own_split(string, delimiter = None): if delimiter == '': raise ValueError('empty delimiter') if delimiter == None: delimiter = '\s+' else: delimiter = re.escape(delimiter) pos = 0 for m in re.finditer(delimiter, string): yield string[pos:m.start()] pos = m.end() yield string[pos:]
Code language: Python (python)

Option 2:

import re def my_very_own_split(s, dell = None): f = [] if dell=='': raise SyntaxError if dell: while dell in s: f.append(s[:s.find(dell)]) s = s[s.find(dell)+len(dell):] return f+[s] return re.split(r'[\s]*',s)
Code language: Python (python)

Option 3:

from string import whitespace, maketrans convert_whitespace = lambda s: s.translate(maketrans(whitespace, ' ' * len(whitespace))) def my_very_own_split(strng, sep=None): start, fil = 0, False if sep == '': raise ValueError("empty separator") if sep is None: sep, fil, strng = ' ', True, convert_whitespace(strng) while start <= len(strng): end = strng.find(sep, start) if end < 0: end = len(strng) if not fil or strng[start: end]: yield strng[start: end] start = end + len(sep)
Code language: Python (python)

Test cases to validate our solution

s, d = 'abc,def,ghi', ',' test.assert_equals(list(my_very_own_split(s, d)), ['abc', 'def', 'ghi']) s, d = 'This is a test', ' ' test.assert_equals(list(my_very_own_split(s, d)), ['This', 'is', 'a', 'test']) s, d = 'This is a test', ',' test.assert_equals(list(my_very_own_split(s, d)), ['This is a test'])
Code language: Python (python)
Notify of
Inline Feedbacks
View all comments