+ 3

Guys I have tried to complete the python average word length program a lot of times but I can't get the result!

Pls help me in getting the code ?

26th Jul 2021, 3:58 PM
Hemalatha.M
Hemalatha.M - avatar
28 Respostas
+ 5
Ok... I didn't expect that at all! You were a C programmer before, weren't you? In python it's much simpler than that, given that the input consists of only alphabetic characters and single spaces, you can just test = input() words = len(test.split(" ")) spaces = words-1 print((len(test)-spaces)/words)
26th Jul 2021, 4:19 PM
Angelo
Angelo - avatar
+ 5
The easiest language is harder to learn than the language u already learned. Coding python while thinking C..🥺🥺🥺.
28th Jul 2021, 6:12 AM
Oma Falk
Oma Falk - avatar
+ 4
Runtime Terror Have you ever heard of code golf? Might be something for you: https://en.m.wikipedia.org/wiki/Code_golf
26th Jul 2021, 4:43 PM
Simon Sauter
Simon Sauter - avatar
+ 3
https://code.sololearn.com/c1E7k4MWw1r7/?ref=app
27th Jul 2021, 7:03 AM
Tharul Nejana
Tharul Nejana - avatar
+ 2
You can copy-paste it here or save it in code playground
26th Jul 2021, 4:10 PM
Angelo
Angelo - avatar
+ 2
Hemalatha.M, this may shock you... That is the complete code...
26th Jul 2021, 4:30 PM
Angelo
Angelo - avatar
+ 2
~LoneWolf yes Python it's not only easier, it's a completely different way of thinking
26th Jul 2021, 4:32 PM
Angelo
Angelo - avatar
+ 2
Runtime Terror Is that an attempt to present the most unreadable and least useful correct answer?
26th Jul 2021, 4:36 PM
Simon Sauter
Simon Sauter - avatar
+ 2
First, you have to split the input string using space as separator, and get the count of number of words using len() method .ie; text = input() words = text.split(" ") words_count = len(words) Then, iterate over the string to count the number of characters (Excluding spaces) You can use a condition like this: count = 0 for i in text: if not i == " ": count += 1 Then now you can divide the word count with the number of characters and print the output I hope this helps :-)
26th Jul 2021, 8:56 PM
Mwaniki Grace Waigumo
Mwaniki Grace Waigumo - avatar
+ 2
Show your code
27th Jul 2021, 6:18 PM
Varnayu Attree
Varnayu Attree - avatar
+ 2
Calvin Thomas I'll give you a better one-liner Also, @ everyone, the problem consists of space separated words, so don't check for characters that are not there in the first place Also, it wants the mean, not the ceil nor the floor of it ( lambda x: print(len("".join(x))/len(x)) )(input().split(" "))
27th Jul 2021, 9:31 PM
Angelo
Angelo - avatar
+ 1
Can you show us one of your attempts?
26th Jul 2021, 4:05 PM
Angelo
Angelo - avatar
+ 1
Yes but how?
26th Jul 2021, 4:08 PM
Hemalatha.M
Hemalatha.M - avatar
+ 1
import string punc= string.punctuation test= input() phrase=" " count_letter =0 count_word =0 for i in range(len(test)): phrase +=str(test[i]) print("character: " + phrase[i] + " ~ ") if phrase[i] == " ": count_word +=1 print("Adding a Word") elif phrase[i] != " " and (phrase[i] not in punc): # not counting spaces and punctuations count_letter +=1 print("Adding a Letter") print(round(((count_letter)//count_word)+1))
26th Jul 2021, 4:12 PM
Hemalatha.M
Hemalatha.M - avatar
+ 1
Yes
26th Jul 2021, 4:23 PM
Hemalatha.M
Hemalatha.M - avatar
+ 1
Thank you guys
26th Jul 2021, 4:31 PM
Hemalatha.M
Hemalatha.M - avatar
+ 1
Runtime Terror I do like one-liners... But that is unreadable
26th Jul 2021, 4:35 PM
Angelo
Angelo - avatar
+ 1
Here's a one-liner possibility without any external modules: (lambda x: print(int(k:=len(x.replace(" ",""))/(x.count(" ")+1))+(k>k//1)))("".join(x for x in input() if x not in ".,!?")) # Hope this helps
27th Jul 2021, 6:53 AM
Calvin Thomas
Calvin Thomas - avatar
+ 1
Angelo Yours doesn't seem to work.
28th Jul 2021, 2:26 AM
Calvin Thomas
Calvin Thomas - avatar
+ 1
Calvin Thomas what input have you tested?
28th Jul 2021, 11:00 AM
Angelo
Angelo - avatar