mardi 22 mars 2016

Trouble splitting text without using split()

I am trying to do this exercise:

splitText(text) where text is a string and return the list of the words by splitting the string text. See example below:

sampleText = "As Python's creator, I'd like to say a few words about its origins.”

splitText(sampleText)

['As', 'Python', 's', 'creator', 'I', 'd', 'like', 'to', 'say', 'a', 'few', 'words', 'about', 'its', 'origins']

You must NOT use the method split() from the str type, however other methods >from the class are allowed. You must not use python library such as string.py.

This is the code I have got so far:

def split(text):
    final_lst = ""
    length = len(text)
    for x in range(length):
        if text[x].isalpha() == True:
            final_lst = final_lst + text[x]
        else:
            final_lst = final_lst + ", "

    final_len = len(final_lst)
    for a in range(final_len):
        if final_lst[:a] == " " or final_lst[:a] == "":
            final_lst = "'" + final_lst[a]
        if final_lst[a:] == " " or final_lst[a:] == ", ":
            final_lst = final_lst[a] + "'"
        elif final_lst[:a].isalpha() or final_lst[a:].isalpha():
            final_lst[a]


    print(final_lst)

split(sampleText)

But I do not know what to do to finish it off as I have tried so many things but the furthest I can get is this. When I run it I get this:

'A

I've tried lots of things to try and solve this but I have hit a wall. Thanks if you can help me out!



Trouble splitting text without using split()

Aucun commentaire:

Enregistrer un commentaire