Question

This is function.py def processString(string): # Implement this function This is # This should print 4.5...

This is function.py
def processString(string):
  # Implement this function

This is 
# This should print 4.5 3.0
processString("An example. Two")

# This should print 4.4 5.3 4.5
processString("This is the first sentence. The second sentence starts after the period. Then a final sentence")

Download function.py and complete the processString(string) function. This function takes in a string as a parameter and prints the average number of characters per word in each sentence in the string. Print the average character count per word for each sentence with 1 decimal precision (see test cases below).

- Assume a sentence always ends with a period (.) or when the string ends.

- Assume there is always a blank space character (" ") between each word.

- Do not count the blank spaces between words or the periods as a character.

Two example test cases are: >>> processString("An example. Dog") 4.5 3.0

(Note that the first sentence has 2 words with a total of 9 characters, so 4.5 characters per word on average. The second sentence has 1 word of 3 characters, so 3 characters on average.)

>>> processString("This is the first sentence. The second sentence starts after the period. Then a final sentence") 4.4 5.3 4.5

Homework Answers

Answer #1
def processString(string):
    sentences = string.split(".")
    for sentence in sentences:
        words = sentence.strip().split()
        total = 0
        for word in words:
            total += len(word)
        print(round(total / len(words), 1), end=' ')
    print()


# This should print 4.5 3.0
processString("An example. Two")

# This should print 4.4 5.3 4.5
processString("This is the first sentence. The second sentence starts after the period. Then a final sentence")

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
def processString(string): # Implement this function # This should print 4.5 3.0 processString("An example. Two") #...
def processString(string): # Implement this function # This should print 4.5 3.0 processString("An example. Two") # This should print 4.4 5.3 4.5 processString("This is the first sentence. The second sentence starts after the period. Then a final sentence") This function takes in a string as a parameter and prints the average number of characters per word in each sentence in the string. Print the average character count per word for each sentence with 1 decimal precision(see test cases below).-Assume a...
Write a program that accepts an input string from the user and converts it into an...
Write a program that accepts an input string from the user and converts it into an array of words using an array of pointers. Each pointer in the array should point to the location of the first letter of each word. Implement this conversion in a function str_to_word which returns an integer reflecting the number of words in the original string. To help isolate each word in the sentence, convert the spaces to NULL characters. You can assume the input...
Python The capitalize function should receive a word as a parameter in the form of a...
Python The capitalize function should receive a word as a parameter in the form of a string and return one version where the first character is written in capital letters. Other characters must be unchanged. For example ‘hey’ become ‘Hey’. Write the function clearly. Consider that str has the method upper that returns a new string where all characters are capitalized
Program Behavior Each time your program is run, it will prompt the user to enter the...
Program Behavior Each time your program is run, it will prompt the user to enter the name of an input file to analyze. It will then read and analyze the contents of the input file, then print the results. Here is a sample run of the program. User input is shown in red. Let's analyze some text! Enter file name: sample.txt Number of lines: 21 Number of words: 184 Number of long words: 49 Number of sentences: 14 Number of...
This is C. Please write it C. 1) Prompt the user to enter a string of...
This is C. Please write it C. 1) Prompt the user to enter a string of their choosing. Store the text in a string. Output the string. (1 pt) Ex: Enter a sample text: we'll continue our quest in space. there will be more shuttle flights and more shuttle crews and, yes, more volunteers, more civilians, more teachers in space. nothing ends here; our hopes and our journeys continue! You entered: we'll continue our quest in space. there will be...
Python 3 Implement the function condense, which takes a "passage" (a string containing multiple sentences separated...
Python 3 Implement the function condense, which takes a "passage" (a string containing multiple sentences separated by the '\n' character), and returns a condensed version of that passage based on the following logic: the frequency with which each word appears in the entire passage is computed a score for each sentence in the passage is computed by summing the frequency of each word in the sentence the sentences with the top 3 scores (we assume there are no ties) are...
Q1) Write a Python function partial_print, which takes one parameter, a string, and prints the first,...
Q1) Write a Python function partial_print, which takes one parameter, a string, and prints the first, third, fifth (and so on) characters of the strings, with each character both preceded and followed by the ^ symbol, and with a newline appearing after the last ^ symbol. The function returns no value; its goal is to print its output, but not to return it. Q2) Write a Python function called lines_of_code that takes a Path object as a parameter, which is...
JAVA ASSIGNMENT 1. Write program that opens the file and process its contents. Each lines in...
JAVA ASSIGNMENT 1. Write program that opens the file and process its contents. Each lines in the file contains seven numbers,which are the sales number for one week. The numbers are separated by comma.The following line is an example from the file 2541.36,2965.88,1965.32,1845.23,7021.11,9652.74,1469.36. The program should display the following: . The total sales for each week . The average daily sales for each week . The total sales for all of the weeks .The average weekly sales .The week number...
1.Write a function which takes a string that contains two words separated by any amount of...
1.Write a function which takes a string that contains two words separated by any amount of whitespace, and returns a string in which the words are swapped around and the whitespace is preserved. Hint: use regular expressions where \s detects the whitespaces in a string. Example: given "Hello     World", return "World         Hello" 2.Pandas exercises: Write a python program using Pandas to create and display a one-dimensional array-like object containing an array of data. Write a python program using Pandas to...
Develop a C++ PROGRAM which will find a hidden sentence in a list of random words...
Develop a C++ PROGRAM which will find a hidden sentence in a list of random words using map function Open this text file named shuffled_words.txt in your program If it matters, you may presume there are an even number of words in the file Place the contents of the file into an appropriate data structure in the following manner: Grab a pair of strings from the file (unless end of file is reached) Each string is separated by a space...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT