Question

In: Computer Science

n this task, you'll be asked to create a simple for-loop to loop over a simple...

n this task, you'll be asked to create a simple for-loop to loop over a simple data construct, in this case, to provide the maximum, minimum, and average length of words in a speech performing a lexicographical analysis not unlike what's used to measure reading level.

Specifications

  1. Keep working on the same notebook
  2. Create a function named lexicographics() that takes one parameter:
    1. to_analyze, a required string
  3. Using a single for loop, calculate the following for your text:
    1. The maximum number of words per line in to_analyze (eg, the length of the longest line in to_analyze)
    2. The minimum number of words per line in to_analyze (eg, the length of the shortest line in to_analyze)
    3. The average number of words per line in to_analyze, stored as a decimal.
  4. Return these values as a tuple, in the order in which they are defined above.

Solutions

Expert Solution

Python Code :

# Method to count the maximum, minimum and average number of
# word from the given string to_analyze
def  lexicographics(to_analyze):
    # variable
    max_words = 0
    min_words = 10000000
    sum = 0

    # iterate over each line and count number of words
    # if words are greater than max_words, change max_words
    # if words are less than min_words, change min_words
    # add number of words into sum
    lines = 0
    for i in to_analyze.strip().split('\n'):
        words = len(i.split())
        if words > max_words:
            max_words = words
        
        if words < min_words:
            min_words = words
        
        sum += words
        lines += 1

    # Calculate the average number of words
    avg_words = sum/lines

    # return tuple of max_words, min_words and avg_words
    return (max_words, min_words, avg_words)


if __name__ == '__main__':
    to_analyze = "I am writing this\nto just check the working of my program." \
                 "\nI love coding." \
                 "\nHope you like it." \
                 "\nPlease upvote the answer."
    ans = lexicographics(to_analyze)

    print(f"The maximum number of words per line: {ans[0]} \n"
          f"The minimum number of words per line: {ans[1]} \n"
          f"The average number of words per line: {ans[2]}")

Output :

The maximum number of words per line: 8
The minimum number of words per line: 3
The average number of words per line: 4.6

Hope you like it

Any Query? Comment Down!

I have written for you, Please upvote the answer as it encourage us to serve you Best !


Related Solutions

ORACLE TASK 2-2 USING A FOR LOOP Create a PL/SQL block using a FOR loop to...
ORACLE TASK 2-2 USING A FOR LOOP Create a PL/SQL block using a FOR loop to generate a payment schedule for a donor’s pledge, which is to be paid monthly in equal increments. Values variable for the block are starting payment due date, monthly payment amount and number of total monthly payments for the pledge. The list that’s generated should display a line for each monthly payment showing payment number, date due, payment amount, and donation balance (remaining amount of...
Create a program named CmdLineCalc.java that works like a simple calculator. You'll run this program from...
Create a program named CmdLineCalc.java that works like a simple calculator. You'll run this program from the command line: $ java CmdLineCalc 1 + 2 3.0 $ java CmdLineCalc 1 - 2.5 -1.5 $ java CmdLineCalc 3 + 4 - 5 2.0 $ java CmdLineCalc 6.5 - 7 + 8 7.5 To keep it simple, your program only needs to support addition (+) and subtraction (-). You may assume that, starting with the first argument, every other argument will be...
Introduction to Java Programing Using Loop Create a simple calculator program using loop Ask user to...
Introduction to Java Programing Using Loop Create a simple calculator program using loop Ask user to input two numbers using scanner class Print the instruction of the menu for the calculator program Ask user to press 0 to Quit Ask user to press 1 to Add Ask user to press 2 to Substract Ask user to press 3 to Multiply Ask user to press 4 to Divide Perform correct calcuation based on user inputs and print the result Print error...
For this writing task, you'll need to respond to the following scenario: Who are you? You...
For this writing task, you'll need to respond to the following scenario: Who are you? You work as department head in the information technology (IT) department at First Federal Bank. Part of your job is to conduct an ongoing assessment of risk for the institution and to recommend proper controls. Banking systems should be able to quickly collect and edit information, summarize results, and promptly correct any errors. You have identified a possible threat to "timeliness" of information. You have...
A simple random sample of size n equals n=400 individuals who are currently employed is asked...
A simple random sample of size n equals n=400 individuals who are currently employed is asked if they work at home at least once per week. Of the 400 employed individuals​ surveyed, 40 responded that they did work at home at least once per week. Construct a​ 99% confidence interval for the population proportion of employed individuals who work at home at least once per week.
C language and it has to be a while loop or a for loop. Use simple...
C language and it has to be a while loop or a for loop. Use simple short comments to walk through your code. Use indentations to make your code visibly clear and easy to follow. Make the output display of your program visually appealing. There is 10 points deduction for not following proper submission structure. An integer n is divisible by 9 if the sum of its digits is divisible by 9. Develop a program that: Would read an input...
A simple random sample of size n=300 individuals who are currently employed is asked if they...
A simple random sample of size n=300 individuals who are currently employed is asked if they work at home at least once per week. Of the 300 employed individuals​ surveyed, 27 responded that they did work at home at least once per week. Construct a​ 99% confidence interval for the population proportion of employed individuals who work at home at least once per week.
A simple random sample of size n=300 individuals who are currently employed is asked if they...
A simple random sample of size n=300 individuals who are currently employed is asked if they work at home at least once per week. Of the 300 employed individuals​ surveyed, 38 responded that they did work at home at least once per week. Construct a​ 99% confidence interval for the population proportion of employed individuals who work at home at least once per week. The lower bound is The upper bound is ​(Round to three decimal places as​ needed.)
please make it 100% correct... urgent task.. 1. Loblaw Manufacturing has asked you to create a...
please make it 100% correct... urgent task.. 1. Loblaw Manufacturing has asked you to create a cash budget in order to determine its borrowing needs for the June to October period. You have gathered the following information. Month                              Sales                      Other Payments June 2015                        $223,600             $104,000 July                                       184,600                   97,500 August                                 157,300                   91,000 September                        120,900                   65,000 October                                98,800                 58,500 November                         105,300 April and May sales were $149,500 and $175,500, respectively. The firm collects 25%...
In this program, you'll create a program that utilizes an enumerated data type to manipulate the...
In this program, you'll create a program that utilizes an enumerated data type to manipulate the array.   Here are the requirements: Write a program that contains an enumerated data type named Letters.    In the declaration, include the following three enumerators: ALPHA, BETA, DELTA. Then, create an array of integers three elements long. The array should be initialized to 0 using a 1-element initialization list. Instead of using integers as subscripts, use the enumerators from your enumerated data type to assign...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT