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 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.53.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.
def processString(string):
lines=string.split('.')
for line in lines:
words=line.strip().split()
t=0
for word in words:
t+=len(word)
print(round(t/len(words),1),end=' ')
Get Answers For Free
Most questions answered within 1 hours.