Question

In: Computer Science

This function takes in a string as a parameter and prints the average number of characters...

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

Solutions

Expert Solution

def processString(string):
    '''convert string into list and separator is "."'''
    string = string.split(".") 
    answer = 0
    ''' traverse sentence by sentence from string'''
    for sentence in string:  
        word_count = 0
        total_char =0
        '''split sentence into word'''
        for word in sentence.split():
            '''count the total character and total word in each sentence'''
            total_char+=len(word)
            word_count+=1
            ''' round is use for printing one value after decimal'''
        print(round((total_char/word_count),1),end=" ")
    print()


processString("An example. Dog")
processString("This is the first sentence. The second sentence starts after the period. Then a final sentence")

Function "process String" takes a string as a parameter. Then string is converted in to list of sentences. Now we go through the each sentence and count number of words and characters.
After counting in one sentence then I print average.
average=total number of character/total word in one sentence
I use round function for printing the average. I pass 1 because I have to print only one digit after the decimal.


working of round function:
x = round(6.8834,2)
print(x)
output: 6.88

if you like this answer then please upvote it.


Related Solutions

Write a function named "characters" that takes a string as a parameter and returns the number...
Write a function named "characters" that takes a string as a parameter and returns the number of characters in the input string
Python Programcomplete theprocessString(string)function. This function takesin a string as a parameter and prints the...
Python Program complete theprocessString(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...
Write a function that takes a C string as an input parameter and reverses the string.
in c++ Write a function that takes a C string as an input parameter and reverses the string. The function should use two pointers, front and rear. The front pointer should initially reference the first character in the string, and the rear pointer should initially reference the last character in the string. Reverse the string by swapping the characters referenced by front and rear, then increment front to point to the next character and decrement rear to point to the...
Write a Java program that takes in a string and a number and prints back the...
Write a Java program that takes in a string and a number and prints back the string from the number repeatedly until the first character... for example Pasadena and 4 will print PasaPasPaP. Ask the user for the string and a number Print back the string from the number repeatedly until the first character For both programs please utilize: methods arrays loops Turn in screenshots
in c++ Write a function that takes a C string as an input parameter and reverses...
in c++ Write a function that takes a C string as an input parameter and reverses the string. The function should use two pointers, front and rear. The front pointer should initially reference the first character in the string, and the rear pointer should initially reference the last character in the string. Reverse the string by swapping the characters referenced by front and rear, then increment front to point to the next character and decrement rear to point to the...
Write a function called fnReadInParameters() that takes as parameter(s): • a string indicating the name of...
Write a function called fnReadInParameters() that takes as parameter(s): • a string indicating the name of the parameter le This function should return a dictionary that stores all of the parameter le data of the SIR model in a format that we will describe further below.
Write a function which takes one parameter int num, and prints out a countdown timer with...
Write a function which takes one parameter int num, and prints out a countdown timer with minutes and seconds separated by a colon (:). It should print out one line for each second elapsed and then pause one second before printing out the next line. A few things to note: - You can assume that calling the function usleep(1000000) makes the program pause for one second - It should count down from num minutes:zero seconds to zero minutes:zero seconds -...
Can someone do this python program? Write a function that takes a number as a parameter...
Can someone do this python program? Write a function that takes a number as a parameter and then prints out all of the factors for that number.
Write a static method called "evaluate" that takes a string as a parameter
In Java language  Write a static method called "evaluate" that takes a string as a parameter. The string will contain a postfix expression, consisting only of integer operands and the arithmetic operators +, -, *, and / (representing addition, subtraction, multiplication, and division respectively). All operations should be performed as integer operations. You may assume that the input string contains a properly-formed postfix expression. The method should return the integer that the expression evaluates to. The method MUST use a stack...
Use Python Write a function that takes a mobile phone number as a string and returns...
Use Python Write a function that takes a mobile phone number as a string and returns a Boolean value to indicate if it is a valid number or not according to the following rules of a provider: * all numbers must be 9 or 10 digits in length; * all numbers must contain at least 4 different digits; * the sum of all the digits must be equal to the last two digits of the number. For example '045502226' is...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT