Words and Scores چالش 3 از جلسهی 42، متوسط
words(text) مجموعهی کلمهها را برگرداند: متن را lower() کن، هر کدام از .,?! را فاصله کن، با split() بشکن و کلمههایی را که در STOP نیستند به یک set اضافه کن. score(question, chunk) تعداد کلمههای مشترک است: len(words(question) & words(chunk)). خروجی test:['day', 'long', 'nilu']['foxes', 'nilu', 'sand']1141
HANDOUT = """Nilu is a small made-up planet with two moons, Pip and Tov.
A day on Nilu lasts 30 hours, and a year lasts 200 days.
The glow trees of Nilu shine blue at night. They drink rain water
and grow 2 meters every year.
The sand foxes of Nilu sleep in the day and hunt at night.
They eat glow berries and small beetles.
Pip is the bigger moon. It turns around Nilu in 12 days.
Tov is tiny and turns around Nilu in 5 days."""
STOP = {"the", "a", "an", "is", "of", "in", "on", "and", "what", "how", "do", "does", "it", "at", "to", "many"}
def words(text):
return set(text.split())
def score(question, chunk):
return 0
# ---- test (do not change) ----
print(sorted(words("How long is a day on Nilu?")))
print(sorted(words("The sand foxes, of Nilu!")))
chunks = HANDOUT.split("\n\n")
for chunk in chunks:
print(score("What do the sand foxes of Nilu eat?", chunk))
راهنما و جواب این چالش · برگشت به درس جلسهی 42: امتحان کتابباز · چالش بعدی: The Best Parts
بیشتر بخوان: تابع در پایتون: آموزش def، پارامتر و return با مثال