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 named "replacement" that takes a string as a parameter and returns an identical...
Write a function named "replacement" that takes a string as a parameter and returns an identical string except with every instance of the character "w" replaced with the character "v" My code: function replacement(word){ var str=word; var n=str.replace("w","v"); return n; } Syntax Error: function replacement incorrect on input Not sure how to fix? Can't use a loop for answer
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.
python Write a function pack_to_5(words) that takes a list of string objects as a parameter and...
python Write a function pack_to_5(words) that takes a list of string objects as a parameter and returns a new list containing each string in the title-case version. Any strings that have less than 5 characters needs to be expanded with the appropriate number of space characters to make them exactly 5 characters long. For example, consider the following list: words = ['Right', 'SAID', 'jO'] The new list would be: ['Right', 'Said ', 'Jo '] Since the second element only contains...
The below_freezing function takes a string as its parameter and return True if the temperature specified...
The below_freezing function takes a string as its parameter and return True if the temperature specified by the string is below freezing point for water. It returns False if the temperature is at or above the freezing point. The string takes the format of a decimal number followed by a letter where F stands for Fahrenheit, C for Celsius, and K for Kelvin. For example, below_freezing("45.5F") and below_freezing("300K") both return False. below_freezing("-1.2C") and below_freezing("272K") both return True It's critical that...
Write a program that prints out the characters of a string each on a line and...
Write a program that prints out the characters of a string each on a line and counts these characters.  (Do not use the length function len() ).
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT