Quite recently it happened to me to join some recruitment interview, where my first task was to write own implementation of built-in split function. It's quite simple, is it not?

However, there were the following conditions:

* 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('')`


*This Kata will control if the new function is a generator and if it's not using the built-in split method, so you may try to hack it, but let me know if with success, or if something would go wrong!*

Enjoy!

Difficulty: introductory

Starter code:

def my_very_own_split(string, delimiter = None):
	

Solutions:
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:]


def my_very_own_split(string, delimiter=None):

    if delimiter == '':
        raise ValueError

    if delimiter is None:
        string = string.replace('\t', ' ')
        while '  ' in string:
            string = string.replace('  ', ' ')
        delimiter = ' '

    last, l = 0, len(delimiter)
    for i in range(len(string)):
        if string[i:i+l] == delimiter:
            yield string[last:i]
            last = i+l
    yield string[last:]


def my_very_own_split(string, delimiter = None):
    for s in __import__('string').split(string,delimiter): yield s

# I think the anti-cheat failed somewhere
my_very_own_split = str.split

def my_very_own_split(string, delimiter=None):
    for w in eval('string.split(delimiter)'):
        yield w


import re

def my_very_own_split(string, delimiter=None):
    if delimiter == '':
        raise ValueError('empty separator')
    delim_re = re.escape(delimiter) if delimiter is not None else r'\s+'
    for s in getattr(re, 'split')(delim_re, string):
        yield s

import re


def my_very_own_split(string, delimiter=None):
    if delimiter == '':
        raise ValueError('empty separator')
    if delimiter == None:
        delimiter = re.compile(r'\s+')
    else:
        delimiter = re.compile(re.escape(delimiter))
    print(delimiter)

    start = 0
    for m in re.finditer(delimiter, string):
        yield string[start:m.start()]
        start = m.end()
    
    yield string[start:]

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)

from re import escape, split as my_split
def my_very_own_split(string, delimiter = None):
    if delimiter == '':
        raise None
    for s in my_split(r'{0}'.format(escape(delimiter) if delimiter else '[\s\t$]+'), string):
        yield s


def my_very_own_split(string, delimiter = None):
    return string.split(delimiter)