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

The Most Common Next Word چالش 3 از جلسه‌ی 34، متوسط

جدول از شش شعر POEMS ساخته شده. تابع most_common(word) را بنویس: از best = table[word][0] شروع کن و هر کلمه‌ای از table[word] که countاش بیشتر بود، best شود. برنامه با دمای صفر از the یک جمله‌ی شش‌کلمه‌ای می‌سازد:
the sun is warm and bright

POEMS = [
    "the cat sat on the mat",
    "the cat ran to the sun",
    "the dog sat in the sun",
    "the sun is warm and bright",
    "the moon is round and white",
    "a bird can sing in the tree",
]

table = {}
for line in POEMS:
    words = line.split()
    for i in range(len(words) - 1):
        if words[i] not in table:
            table[words[i]] = []
        table[words[i]].append(words[i + 1])

def most_common(word):
    best = None
    return best

line = ["the"]
for i in range(5):
    line.append(most_common(line[-1]))
print(" ".join(line))

راهنما و جواب این چالش · برگشت به درس جلسه‌ی 34: کلمه‌ی بعدی را حدس بزن · چالش بعدی: A Random Poet

بیشتر بخوان: تابع در پایتون: آموزش def، پارامتر و return با مثال