CheckiO Elementary Monkey Typing
自分の解答
def count_words(text, words): count=0 for word in words: if (word in text.lower()) == True: count+=1 else: pass return count
ちゃんとカウントという変数を用意している。Cでも読んでいる気分が否めない。。。
プロの解答
def count_words(text, words): return sum(w in text.lower() for w in words)
なるほど、count変数を用意しなくても足し上げることが可能なのか。
text.lower()は問題を逆手に取った方法なので無視するとして、
こういう改行しない書き方は未だに挑戦できていない。。。
in演算子を使うと、True or Falseで返ってくるのに、足し算でいいというのが気づき。 Trueも足し算可能。なるほど