peterpete

I need help in this one because its messed up

Discussion created by peterpete on Mar 7, 2014
Please can u help me in the third function because I am desperate to know how it works!def split_on_separators(original, separators):
    """ (str, str) -> list of str

    Return a list of non-empty, non-blank strings from the original string
    determined by splitting the string on any of the separators.
    separators is a string of single-character separators.

    >>> split_on_separators("Hooray! Finally, we're done.", "!,")
    ['Hooray', ' Finally', " we're done."]
    """
    result = []
    newstring = ''

    for index,char in enumerate(original):
        if char in separators or index==len(original) -1:
            result.append(newstring)
            newstring=''
            if '' in result:
                result.remove('')
        else:
            newstring+=char
    return result
def average_sentence_length(text):
    """ (list of str) -> float

    Precondition: text contains at least one sentence. A sentence is defined
    as a non-empty string of non-terminating punctuation surrounded by
    terminating punctuation or beginning or end of file. Terminating
    punctuation is defined as !?.

    Return the average number of words per sentence in text.  

    >>> text = ['The time has come, the Walrus said\n',
         'To talk of many things: of shoes - and ships - and sealing wax,\n',
         'Of cabbages; and kings.\n'
         'And why the sea is boiling hot;\n'
         'and whether pigs have wings.\n']
    >>> average_sentence_length(text)
    17.5
    """
    words=0
    import re
    for line in text:
        line_values = list(filter(("-").__ne__, line.strip().split()))
        words += len(line_values)
       
    sentence =[x for x in re.compile('[.!?]').split(''.join(text).strip()) if x]   
    ASL=words/len(sentence)
    return ASL
   
def avg_sentence_complexity(text):
    """ (list of str) -> float

    Return the average number of phrases per sentence.
   
    A sentence is defined as a non-empty string of non-terminating
    punctuation surrounded by terminating punctuation
    or beginning or end of file. Terminating punctuation is defined as !?.
    Phrases are substrings of sentences, separated by one or more of the
    following delimiters ,;:

    >>> text = ['The time has come, the Walrus said\n',
         'To talk of many things: of shoes - and ships - and sealing wax,\n',
         'Of cabbages; and kings.\n',
         'And why the sea is boiling hot;\n',
         'and whether pigs have wings.\n']
    >>> avg_sentence_complexity(text)
    3.5  
    """
    Sentences=0
    Phrases=0
    sentence=split_on_separators(text,'?!.')
    for sep in sentence:
        Sentences+=1
    Phrase=split_on_separators(text, ',;:')
    for n in Phrase:
        Phrases+=1
    complex = float(Phrases) / float(Sentences)
    return complex

   # This is what i did...the second one was told by a friend but he didn't explain it to me so I am completely perplexed.

Attachments

Outcomes