Average Word Length
Note: This page contains a small but interesting piece of Python code which I call snippets. You can find more such codes on my Python snippets page.
Contents
Problem ¶
You are in a college level English class, your professor wants you to write an essay, but you need to find out the average length of all the words you use. It is up to you to figure out how long each word is and to average it out.
Task:
Takes in a string, figure out the average length of all the words and return a number representing the average length. Remove all punctuation. Round up to the nearest whole number.
Input Format:
A string containing multiple words.
Output Format:
A number representing the average length of each word, rounded up to the nearest whole number.
Sample Input:
this phrase has multiple words
Sample Output:
6
Solution ¶
Here is my solution to the above problem. Remember that there could be more than one way to solve a problem. If you have a more efficient or concise solution, please leave a comment.
import re
from math import ceil
s=re.sub("[^\w\s]","",input()).split()
print(ceil(sum([len(x) for x in s])/len(s)))
Explanation ¶
My approach (or the algorithm)¶
- Remove punctuation marks.
- Split the sentence into words.
- Find length of each word and compute average.
- Round up the average to the nearest whole numner.
The code¶
- Remove punctuation marks.
- The RegEx pattern
[^\w\s]
looks for anything that's not alphanumeric or a space. -
re.sub("[^\w\s]","",input())
substitutes the matches with "".
- The RegEx pattern
- Split the sentence into words.
-
.split()
splits words by space into a list.
-
- Find length of each word and compute average.
-
[len(x) for x in s]
generates a list containing lengths words. -
sum([len(x) for x in s])
gives the sum of word lengths -
len(s)
- number of words
-
- Round up the average to the nearest whole numner.
-
ceil()
rounds a floating point number to the next integer. For example, 5.1 becomes 6 instead of 5.
-
The problem question is picked from SoloLearn. Here is my SoloLearn code and my SoloLearn profile page.
Last updated 2021-01-08 21:16:55.364441 IST
Comments