Knowee
Questions
Features
Study Tools

Your job is to write the function prevword_ave_len(word) which takes a single argument word (a str) and returns the average length (in characters) of the word that precedes word in the text each time it appears. That is, for each occurrence of word in the text, you are to determine the (single) word which precedes it, and calculate the average length of all those preceding words. If one of the occurrences of word happens to be the first word occurring in the text, the length of the preceding word for that occurrence should be counted as zero. In the instance that word doesn't occur in the text, the function should return -1. Note that we define a "word" to simply be a string that is delimited by "whitespace" (i.e. punctuation following a word is included as part of the word). Additionally, the casing in the original text (and in word) should be preserved.>>> prevword_ave_len('soul;')2.0>>> prevword_ave_len('whale')-1>>> prevword_ave_len('and')6.714285714285714

Question

Your job is to write the function prevword_ave_len(word) which takes a single argument word (a str) and returns the average length (in characters) of the word that precedes word in the text each time it appears. That is, for each occurrence of word in the text, you are to determine the (single) word which precedes it, and calculate the average length of all those preceding words. If one of the occurrences of word happens to be the first word occurring in the text, the length of the preceding word for that occurrence should be counted as zero. In the instance that word doesn't occur in the text, the function should return -1. Note that we define a "word" to simply be a string that is delimited by "whitespace" (i.e. punctuation following a word is included as part of the word). Additionally, the casing in the original text (and in word) should be preserved.>>> prevword_ave_len('soul;')2.0>>> prevword_ave_len('whale')-1>>> prevword_ave_len('and')6.714285714285714

...expand
🧐 Not the exact question you are looking for?Go ask a question

Solution

The problem is asking to write a function that will calculate the average length of the word that comes before a specific word in a text.

Here is a step by step solution in Python:

  1. First, we need to import the re module for regular expressions.
import re
  1. Define the function prevword_ave_len(word) that takes a string as an argument.
def prevword_ave_len(word):
  1. Inside the function, define a variable text that contains the text you want to search in. For this example, I will use a placeholder text.
    text = "This is a placeholder text. Replace it with your own text."
  1. Split the text into a list of words using the split() method.
    words = text.split()
  1. Initialize two variables, total_length and count, to 0. These will be used to calculate the average length of the preceding words.
    total_length = 0
    count = 0
  1. Loop through the list of words. If the word is found, add the length of the preceding word to total_length and increment count by 1. If the word is the first word in the text, add 0 to total_length.
    for i in range(len(words)):
        if words[i] == word:
            if i == 0:
                total_length += 0
            else:
                total_length += len(words[i-1])
            count += 1
  1. After the loop, check if count is 0. If it is, return -1. Otherwise, return the average length of the preceding words.
    if count == 0:
        return -1
    else:
        return total_length / count
  1. Finally, call the function with the word you want to search for.
print(prevword_ave_len('your_word'))

This function will return the average length of the word that precedes the specified word in the text. If the word does not appear in the text, the function will return -1.

This problem has been solved

Similar Questions

Write a function repeat_word_count(text, n) that takes a string text and a positive integer n, converts text into a list of words based on simple whitespace separation (with no removal of punctuation or changing of case), and returns a sorted list of words that occur n or more times in text. For example:>>> repeat_word_count("buffalo buffalo buffalo buffalo", 2)['buffalo']>>> repeat_word_count("one one was a racehorse two two was one too", 3)['one']>>> repeat_word_count("how much wood could a wood chuck chuck", 1)['a', 'chuck', 'could', 'how', 'much', 'wood']

Have the function LetterCount(str) take the str parameter being passed and return the first word with the greatest number of repeated letters. For example: "Today, is the greatest day ever!" should return greatest because it has 2 e's (and 2 t's) and it comes before ever which also has 2 e's. If there are no words with repeating letters return -1. Words will be separated by spaces.ExamplesInput: "Hello apple pie"Output: HelloInput: "No words"Output: -1

We are given a function countWordOccurrences that takes two string parameters sentence and word."Sentence" is a string made up of multiple words separated by spaces.We have to find how many times 'word' occurs in the given 'sentence'.The 'word' should be an exact match.Note: The case (uppercase/lowercase) of the letters in the word does NOT matter.InputFirst line contains space separated series of words which represents the string "sentence".Second line contains a single word "word".Constraints1 ≤ length(sentence), length(word) ≤ 104length(word) ≤ length(sentence)OutputWe have to return an integer which is the number of occurrences of the single word 'word' in the string 'sentence'.

Define a function first_word(arg1),with a string argument arg1. In the function:a. use split() function to separate arg1 by "," into a list of wordsb. Return res that is the last element of the list of words

Write a function, lenWords(STRING), that takes a string as an argumentand returns a tuple containing length of each word of a string.For example, if the string is "Come let us have some fun", thetuple will have (4, 3, 2, 4, 4, 3)

1/1

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.