Checkio "Three words"の解答

checkioで遊んでみた

プログラミング初心者アンドはてブ初心者です。
GWに特に予定もないのでpythonのcheckioで遊んでみました。

  • 問題

Let's teach the Robots to distinguish words and numbers.
You are given a string with words and numbers separated by whitespaces (one space). The words contains only letters. You should check if the string contains three words in succession.
Hints: You can easily solve this task with these useful functions: str.split, str.isalpha and str.isdigit.
Input: A string with words.
Output: True or False, a boolean.

  • 出力の例
checkio("Hello World hello") == True
checkio("He is 123 man") == False
checkio("1 2 3 4") == False
checkio("bla bla bla bla") == True
checkio("Hi") == False
  • ワイの回答
def checkio(a):
    found = False
    counter = 0
    for line in a.split():
        if line.isalpha():
            counter += 1
            if counter >= 3:
                found = True
                break
        if line.isdigit():
            found = False
            counter = 0    
    return found

初心者な回答って感じなのだろうか。
「三文字連続したらcheckioしてね」って文章ちゃんと読んでなかったのでグダりました


で上手い人のコードが見れるのがいいところなので見てみましょう。

  • クリエイティブな回答(Twisted, Puzzling, Obfuscated and Weird.)
checkio=lambda x:"www" in "".join('w' if w.isalpha() else 'd' for w in x.split())
  • クリアーな回答(Clear, Readable, Documented and Educational.)
def checkio(words):
    succ = 0
    for word in words.split():
        succ = (succ + 1)*word.isalpha()
        if succ == 3: return True
    else: return False
  • スピーディな回答(Speedy and Algorithmic. It should be fast.)
def checkio(words):
    k=0
    for word in words.split():
        if word.isalpha():
            k+=1
            if k==3:
                return True
        else:
            k=0
    return False

クリエイティブな回答みたいなワンライナー見るとpythonっぽいと思ってみたり。