0
Average word length
n = input() s = "._?!-,@#
amp;*:;£€¥^=" for i in n: if i is s: n.remove(i) n=n.split() l=[] for i in n: l.append(len(i)) print(round(sum(l)/len(l))) this code failed 2 test cases can any what wrong with this4 Antworten
+ 2
G Ramya
I can think of 2 methods.
The first one, convert the string into a list, and use a list method to remove the unwanted character.
The second one, use a string method to replace an unwanted character.
What's the method name?
Try to search it yourself.
+ 2
2 possible reasons for failure in your test cases could be:
1- If there are non-alphabet characters in the string and you're removing them using the 'is' operator, the code will not remove the correct characters because the 'is' operator checks for object identity, not object equality. Instead, you should use the 'in' operator.
Here is the corrected code snippet:
n = input()
s = "._?!-,@#amp;*:;£€¥^="
for i in n:
if i in s:
n.remove(i)
n=n.split()
l=[]
for i in n:
l.append(len(i))
print(round(sum(l)/len(l)))
2- The test cases may have contained words with apostrophes or special characters that are not handled correctly by the 'split()' method. In this case, the words after the apostrophe or special character are treated as separate words. To avoid this, you can use a regular expression to split the string correctly.
Here is the corrected code snippet using regular expressions:
import re
n = input()
s = "._?!-,@#amp;*:;£€¥^="
for i in n:
if i in s:
n.remove(i)
n=re.split('\W+', n)
l=[]
for i in n:
l.append(len(i))
print(round(sum(l)/len(l)))
With these corrections, the code should pass all the test cases.
+ 1
G Ramya ,
Various problems. I'll just say one. You can't do this,
n.remove(i)
because n is type str, and str has no remove method.
0
Then please suggest me how to remove special symbol in word like !,.