我修改后的解决方案是这样的:我们用文字将其分解 - 我们删除所有不必要的内容。在循环中,我们检查该单词是否被禁止并增加计数器 - 我们还添加了禁止单词,将其翻译为大写()和一个空格并将其放入集合中
我收到这样的提示:方向是由Enikeyshchik指示的,但是单词可以以大写字母开头,例如“and”和“And”是不同的单词。
import codewars_test as test
import re
except_word = {"a", "the", "on", "at", "of", "upon", "in", "as",
"A", "The", "On", "At", "Of", "Upon", "In", "As", ""
}
def word_count(s):
all_word = re.sub(r'([^A-Za-z]+)', r' ', s).split(' ')
cnt = 0
for word in all_word:
if word in except_word:
continue
else:
cnt += 1
return cnt
if __name__ == '__main__':
test.assert_equals(word_count("hello there"), 2)
test.assert_equals(word_count("hello there and a hi"), 4)
test.assert_equals(word_count("I'd like to say goodbye"), 6)
test.assert_equals(word_count("Slow-moving user6463 has been here"), 6)
test.assert_equals(word_count("%^&abc!@# wer45tre"), 3)
test.assert_equals(word_count("abc123abc123abc"), 3)
test.assert_equals(word_count("Really2374239847 long ^&#$&(*@# sequence"), 3)
long_text = r"""
I’d been using my sphere as a stool. I traced counterclockwise circles on it with my fingertips and it shrank until I could palm it. My bolt had shifted while I’d been sitting. I pulled it up and yanked the pleats straight as I careered around tables, chairs, globes, and slow-moving fraas. I passed under a stone arch into the Scriptorium. The place smelled richly of ink. Maybe it was because an ancient fraa and his two fids were copying out books there. But I wondered how long it would take to stop smelling that way if no one ever used it at all; a lot of ink had been spent there, and the wet smell of it must be deep into everything.
"""
test.assert_equals(word_count(long_text), 112)
现在我的函数可以生成基本测试的正确答案
来自服务器的随机测试未通过,与所有其他随机测试一样,结果总是多 1 个单词
st = """R;!GV9sZrD 5x=jncajkU Z#0qd' rR"8oeAy:; IO$"EhYI), NG("Cy28 +ix>i kq&rV/ ',Sl[PL~ Ae/[< \-T<:/ D-$W9p5,Qh yF)DFS XFR@x{8[. L+(]M-cW J+i~E =g5B"1rGoM @c*ByGCt _2amHl =Y8f*E) P_F}01o t}1q!+A@| B3"S;; dP..'U >FqS>4D(S %rf/# =jB$+F<M G$:|K"rJn m92Fkv I^xuZ )s@~R65W x(;~$DWj,J auRs^b S*H-Y]FV T~PS*F8} OA<<qt n]`wK:ED }^B)]! kD[&y uwb_1nY&(F 7_K"c\T 7G0](BA3 ?UpyrP"Y5) @rTKasu`J uk,HFb^{ }IE|N L>$lwGtd dfQ;[.O}zY S`y/$ <\pzJ2\( S+g0IZ /9QKZ29BC jy3b%0KZb~ Eb"192nt Q8^[| <EZ'oN K8~uLh>Z?N KF*8,s3u C<4lxn%> sFARC r_s?Q~,)9y W)q=oQK]cK ?n^k}qwECZ I#@9CF "T1w@ &j(QfUio :O"m~{ <\&rxFVI !k9(Eq} Ezhc_="""
Правильный ответ с сайта 159 у меня 160
st = """"&p!2g Nz"E{ 0=cfc;*^{U bPf<:n/ 1>?j!#+|Ww kQsGs}] }48%s W?Kk4RdIK+ WCtv~_ t_.q4-! #%~=K /[r/,t#Se 1:fyl: KZx,o9 2*W[hp>X0" L0u0oFBY @"wHc bgRTj ,~OXJWt.Z jY*k2S:L x/jF>_#QRX Va(A/3 {4f32ZC|s 'f1MsZRK s+y@i lU<keS4! c!sA&i| rS'+-oNOj ']OzY?e# NDU!Sd) k5z#!n<4 p]kD,X5 c}aY|1"J ^KF}4sl ailA6Uw>$8 W{aA5ON~} 2}<C"p T&+h?\Y {Y9E$;4/.k GDYYXcK` ][h!l>*o!` &0?ST%v @+9T'-0F5U {Wh&ye:7 9CF0"n3 Nw/,L=+qK :jTZ;b,. wb=i_d5F i:(WF1 e;>9`#ToF ~z~-/ <uN], jc0[p$f3I r9M;Vm|%p 0)0<PS< CUtg2Gl@$ nnL]'O?N>C O9`YU 730|Y K!Sx,JL""""
服务器的答案是 134 - 我有 135 - 对于所有随机测试都是如此
请解释一下,这是什么原因?就好像我忘记删除某些东西(我不明白是什么)。