1
votes

I'm currently trying to replicate the split function. I have looked around and found one that has helped a lot, the only problem with the code is the empty string is not displayed empty rather they have multiple Apostrophe for multiple different empty strings. the current code is

def mysplit(strng):

    words = []
    current_word = " "
    for char in strng:
        if char == " ":
            words.append(current_word)
            current_word = ""
        else:
            current_word += char

    words.append(current_word)
    return words


    print(mysplit("To be or not to be, that is the question"))
    print(mysplit("To be or not to be,that is the question"))
    print(mysplit("   "))
    print(mysplit(" abc "))
    print(mysplit(""))

The output that comes out

[' To', 'be', 'or', 'not', 'to', 'be,', 'that', 'is', 'the', 'question']
[' To', 'be', 'or', 'not', 'to', 'be,that', 'is', 'the', 'question']
[' ', '', '', '']
[' ', 'abc', '']
[' ']

I'm trying to get the outcome of

['To', 'be', 'or', 'not', 'to', 'be,', 'that', 'is', 'the', 'question']
['To', 'be', 'or', 'not', 'to', 'be,that', 'is', 'the', 'question']
[]
['abc']
[]
5
You need to initialize current_word with an empty string not a space. Then, if you only append to words when current_word is not empty, you should obtain what you are looking for (which is not exactly how split() works btw) - Alain T.

5 Answers

2
votes

When you find your separator (space), you also need to check that you have a proper, non-blank word to add.

    if char == " ":
        words.append(current_word)
        current_word = ""

should be

    if char == " ":
        if current_word:    # If there is anything to append, do so.
            words.append(current_word)
        current_word = ""
0
votes

You could implement this using enumerate and zip:

def mysplit(string):
    spaces = [ i for i,c in enumerate(string) if c==" " ] # indexes of spaces
    subs   = [ string[s+1:e] for s,e in zip([-1]+spaces,spaces+[len(string)]) ]
    return [ s for s in subs if s ] # filter out empty strings
0
votes

You have several issues. Please take a look into my solution

def mysplit(strng):

    words = []
    current_word = ""
    for char in strng:
        if char == " ":
            if current_word:
                words.append(current_word)
            current_word = ""
        else:
            current_word += char

    if current_word:
        words.append(current_word)
    return words


print(mysplit("To be or not to be, that is the question"))
print(mysplit("To be or not to be,that is the question"))
print(mysplit("   "))
print(mysplit(" abc "))
print(mysplit(""))
0
votes

First, you should remove the words.append(current_word) just before return words.

And then you can add a if using the strip function to remove spaces in front and at the end of a string (https://www.journaldev.com/23625/python-trim-string-rstrip-lstrip-strip)

if char is " ":
    need_to_append, stripped_word = should_append(current_word)
    if need_to_append:
        words.append(stripped_word)
    current_word = ""

Final version :

def should_append(word):
    stripped_word = word.strip()
    return stripped_word != "", stripped_word

def mysplit(strng):
    words = []
    current_word = " "
    for char in strng:
        if char is " ":
            need_to_append, stripped_word = should_append(current_word)
            if need_to_append:
                words.append(stripped_word)
            current_word = ""
         else:
            current_word += char
    need_to_append, stripped_word = should_append(current_word)
    if need_to_append:
        words.append(stripped_word)
    return words
0
votes
def mysplit(strng):
    words = []
    if(len(strng)== 0 or strng.isspace()):
        return words
        
    current_word = " "
    for char in strng:
        if char == " ":
                words.append(current_word)
                current_word = ""
        else:
            
            current_word += char

    
    words.append(current_word)
    for w in words:
        if(w.isspace() or len(w)==0):
            words.remove(w) 
    
    return words


print(mysplit("To be or not to be, that is the question"))
print(mysplit("To be or not to be,that is the question"))
print(mysplit("   "))
print(mysplit(" abc "))
print(mysplit(""))