بتل کد Battle Codeدارد باز می‌شود…

جواب «Bug 2: Capital Letters» چالش 4 از جلسه‌ی 47

سؤال

دستیار «Where do Sand Foxes sleep?» را جواب نمی‌دهد ولی «where do sand foxes sleep?» را می‌دهد. words را درست کن: حرف‌ها باید کوچک شوند و علامت‌های .,?! فاصله شوند. خروجی درست:
['foxes', 'sand', 'sleep', 'where']
3
2

راهنما

به حلقه نگاه کن: روی text می‌چرخد، نه روی text.lower().

یک جواب درست

STOP = {"the", "a", "an", "is", "of", "in", "on", "and", "what", "how", "do", "does", "it", "at", "to", "many"}

def words(text):
    clean = ""
    for ch in text.lower():
        if ch in ".,?!":
            clean += " "
        else:
            clean += ch
    result = set()
    for w in clean.split():
        if w not in STOP:
            result.add(w)
    return result

def score(question, chunk):
    return len(words(question) & words(chunk))


# ---- test (do not change) ----
print(sorted(words("Where do Sand Foxes sleep?")))
print(score("Where do Sand Foxes sleep?", "The sand foxes of Nilu sleep in the day."))
print(score("WHEN does sprout water the beds", "Sprout waters the beds at 7."))

اول خودت امتحان کن؛ جواب‌های دیگر هم قبول است. برگرد و خودت حلش کن