How to solve this python question? I can't figure out at all...this one is stubborn

717
3
02-28-2014 03:09 PM
PeterPete
New Contributor
def average_word_length(text):
    """ (list of str) -> float

    Precondition: text is non-empty. Each str in text ends with \n and at
    least one str in text contains more than just \n.

    Return the average length of all words in text. Surrounding punctuation
    is not counted as part of the words.

    >>> text = ['James Fennimore Cooper\n', 'Peter, Paul and Mary\n']
    >>> average_word_length(text)
    5.142857142857143
    """
Tags (2)
0 Kudos
3 Replies
CodyScott
Occasional Contributor
def average_word_length(text):
    """ (list of str) -> float

    Precondition: text is non-empty. Each str in text ends with \n and at
    least one str in text contains more than just \n.

    Return the average length of all words in text. Surrounding punctuation
    is not counted as part of the words.

    >>> text = ['James Fennimore Cooper\n', 'Peter, Paul and Mary\n']
    >>> average_word_length(text)
    5.142857142857143
    """


I would approach it by first removing the '\n' from the string and removing the punctuation from the string.
After that, split the string by looking for the spaces in it, which should provide you with a tuple of each word in the string.
Then, loop each word getting its length and calculate an average from each word.

Here is a guess at the code below. I haven't tested this so who knows if the syntax is correct...
Also look here: http://stackoverflow.com/questions/12761510/python-how-can-i-calculate-the-average-word-length-in-a-...
Best of luck!


text = ['James Fennimore Cooper\n', 'Peter, Paul and Mary\n']

for scen in text:
    string = scen.replace('\n','')
    #this would be any punctuation you want to replace
    string = string.replace("'","")

    #confirm syntax 
    string = string.split(' ')

    wordcount = len(string)
    wordlen = 0

    for word in string:
        tmplen = 0
        for letter in word:
            tmplen += 1
    
        wordlen += tmplen
    
    avglen = (wordcount/wordlen)

    print avglen

0 Kudos
CodyScott
Occasional Contributor
I would approach it by first removing the '\n' from the string and removing the punctuation from the string.
After that, split the string by looking for the spaces in it, which should provide you with a tuple of each word in the string.
Then, loop each word getting its length and calculate an average from each word.

Here is a guess at the code below. I haven't tested this so who knows if the syntax is correct...
Also look here: http://stackoverflow.com/questions/12761510/python-how-can-i-calculate-the-average-word-length-in-a-...
Best of luck!


text = ['James Fennimore Cooper\n', 'Peter, Paul and Mary\n']

for scen in text:
    string = scen.replace('\n','')
    #this would be any punctuation you want to replace
    string = string.replace("'","")

    #confirm syntax 
    string = string.split(' ')

    wordcount = len(string)
    wordlen = 0

    for word in string:
        tmplen = 0
        for letter in word:
            tmplen += 1
    
        wordlen += tmplen
    
    avglen = (wordcount/wordlen)

    print avglen



Oh, if you had a ton of punctuation you wanted to get rid of everytime, write a function to parse the text and return the "stripped" version of your text
0 Kudos
XanderBakker
Esri Esteemed Contributor
You could use this:

def average_word_length(text):
    ''' Return the average length of all words in text. Do not
    include surrounding punctuation in words.
    text is a non-empty list of strings each ending in \n.
    At least one line in text contains a word.'''

    words = text.split()
    for word in words:
            average=sum(float(len(word)) for word in words)/float(len(words))
    return average


Sounds a lot like this post:
http://forums.arcgis.com/threads/103785-Hey-I-was-wondering-about-this-function-problem-for-a-while

... on comparing signatures ...

Kind regards,

Xander
0 Kudos