Problem: Average Word Length | Sololearn: Learn to code for FREE!

+2

Problem: Average Word Length

Given a sentence as input, calculate and output the average word length of that sentence. To calculate the average word length, you need to divide the sum of all word lengths by the number of words in the sentence. Sample Input: this is some text Sample Output: 3.5 My code that works: sentence = input() words = sentence.split() total = sum(map(len, words))/len(words) print(total)

2/22/2021 1:38:36 PM

Max_Mnemo

38 Answers

New Answer

+15

text = input() length_words = 0 total_words = 0 words = text.split( ) for x in words: length_words += len(x) total_words += 1 average = length_words / total_words print(average)

+5

words = input() len_words= len(words) - words.count(" ") tot = len_words / (words.count(" ")+1) print(tot)

+3

0 text = input() a = text.split() b=len(a) only_alpha = "" for i in text: if i.isalpha(): only_alpha += i print(len(only_alpha)/b) its work try you

+3

New approach without using split and join (). s=str(input()) print((len(s)-s.count(' '))/(s.count(' ')+1))

+2

txt = input("") length = len(txt)-txt.count(" ") splits = txt.split() words = 0 for splits[words] in splits: words += 1 print(length/words)

+2

new way to do it without using split() good for interview def average(text): space=" " word=[] i=0 new_text=text.replace(" ","") length_of_text=len(new_text) while i < len(text): if text[i] not in space: word_start = i while i<len(text) and text[i] not in space: i +=1 word.append(text[word_start:i]) i +=1 print(length_of_text/len(word)) average(input())

+1

Here are go. Did this in Python sentence = input("") words = sentence.split() count = 0 sum =0 for x in words: sum += len(x) count += 1 total = sum / count print(total)

+1

Here is my code solution: text = input() WORDS = text.split(" ") count = 0 for i in text: if not i == " ": count += 1 average = count/len(WORDS ) print (average )

+1

Here's a new approach and keeping it a 2 line code text = input().split() print(len("".join(text)) / len(text))

+1

text = input() new_list=text.split() text = text.replace(" ","") print(len(text)/len(new_list))

+1

text = input() result = len(text.replace(' ', '')) / len(text.split()) print (result)

+1

check this out here: text = input() text_list=list(text.split()) text_length=len(text_list) letter=list(text) letter_length=len(letter) -(text_length-1) average=letter_length/text_length print(average)

+1

text = input() a = text.split() d = 0 for i in a: c = i.split() e = len(i) d += e print(d/len(a))

+1

text = input() length_words = 0 total_words = 0 words = text.split( ) for x in words: length_words += len(x) total_words += 1 average = length_words / total_words print(average)

+1

import numpy as np n = str(input()) b = len(n.split()) def func(a, b, c=0): for i in a: if i != " " and i !="?": c += 1 print (int(np.ceil(c/b))) func(n, b)

+1

When I tried it like this....the first case has refused but the other 4 cases are ryt...what could be wrong Mohamed Amr Pratap Vasava LAKSHMI NARAYANAN M story = input() i = 0 punc = ["?","!",".",","] for word in story: for char in word: if char in punc: word = word.replace(char,"") else: u = len(word) i=i+u new_story = story.split() answer = i/(len(new_story)) print(round(answer))

+1

nsenji.inc add a space to 'punc' list. Your 2nd for loop is no need.

0

Is that full description? Is it code coach problem? what is your question? edit: It's not a place for advertisement. use your feed post.

0

EDIT : https://www.sololearn.com/Discuss/2700398/average-word-length-in-python-solved I believe it's that one : "Given a sentence as input, calculate and output the average word length of that sentence. To calculate the average word length, you need to divide the sum of all word lengths by the number of words in the sentence. Sample Input: this is some text Sample Output: 3.5"

0

Guy y is this shit so hard