+ 3
To find average word length
I couldn't get two cases correct. What could be reason. Can anyone verify it. Thanks
12 Respostas
+ 2
Hi! please specify the programming language, the task condition, and your code attempt
+ 2
Karma Gayley
Formula should be
awl = total_characters / total_words;
Where total_characters should be without special character.
Then use Math.ceil(awl) to get nearest whole number.
+ 1
I used Java. And here is my code.
+ 1
import java.util.*;
public class Program
{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String s=sc.nextLine();
double sum=0;double av=0;int count=0;
String c[]=s.split(" ");
for(String word:c){
int wordlen=word.length();
sum=sum+wordlen;
count++;
}
av=Math.round((double)(sum/count));
System.out.println((int)av);
}}
+ 1
Karma Gayley
Special characters should not be count to get average word length.
+ 1
// The Java version:
import java.util.Scanner;
public class Java {
public static void main(String[] args) {
Scanner p = new Scanner(System.in);
String a[] = p.nextLine().split(" ");
char b[] = ",?!.".toCharArray();
double c = 0;
for (String x: a) {
for (char y: b) {
x = x.replace(Character.toString(y), "");
}
c += x.length();
}
String h = Double.toString(Math.ceil(c / a.length));
System.out.print((h.split("\\."))[0]);
}
}
+ 1
i use
text = input()
word = text.split()
only_alpha = text.replace(" ", "")
print(len(only_alpha) / len(word))
0
I did that. I couldn't get the first case right.
0
// The C version:
#include <stdio.h>
int main() {
char a[100];
scanf("%[^\n]s", a);
int len, nums;
nums = (a[0] == '\0') ? 0 : 1;
for (int i=0;a[i]!='\0';i++) {
if (a[i] == ' ' && a[i+1] != '\0') {
nums++;
continue;
}
len++;
switch (a[i]) {
case '?':
case '!':
case '.':
case ',':
case '-':
len--;
break;
}
}
printf("%d", len/nums + (len%nums!=0));
return 0;
}
# The Python version:
a = input().replace("?", "").replace(".", "")
x = (len(a)-a.count(" "))/(a.count(" ")+1)
print(int(x) + (x>x//1))
0
Karma Gayley Need to remove any punctuations marks from words..
(just count alphabets only )
And use Math.ceil(result); hope it works..
0
# The Python version
from math import ceil
s=input().split()
c=0
d=0
for i in range(len(s)):
for j in range(len(s[i])):
if(s[i][j]!='?'):
c+=1
d+=1
x=c/d
print(ceil(x))
Happy coding đđ