+ 5
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)
57 Answers
+ 23
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)
+ 8
words = input()
len_words= len(words) - words.count(" ")
tot = len_words / (words.count(" ")+1)
print(tot)
+ 3
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)
+ 3
txt = input("")
length = len(txt)-txt.count(" ")
splits = txt.split()
words = 0
for splits[words] in splits:
words += 1
print(length/words)
+ 3
New approach without using split and join ().
s=str(input())
print((len(s)-s.count(' '))/(s.count(' ')+1))
+ 2
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
+ 2
text = input()
new_list=text.split()
text = text.replace(" ","")
print(len(text)/len(new_list))
+ 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())
+ 2
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)
+ 2
nsenji.inc add a space to 'punc' list.
Your 2nd for loop is no need.
+ 1
Can anyone help me wit it
+ 1
# Super simple!!!
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
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
text = input()
length = len(text) - text.count(' ')
count = len(text.split())
print(length/count)
+ 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()
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
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))
Hot today
Python — File Handling
0 Votes
Loop question, I've tried everything that I knew I just don't know. Please help me solve it out
1 Votes
Help me solve this (using loop)
1 Votes
What is wrong? Error on test.
1 Votes
Help me wiht python
1 Votes
Question - Java: Pyramid layers
2 Votes