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
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")
Get Answers For Free
Most questions answered within 1 hours.