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

جواب «Real or Made Up?» چالش 6 از جلسه‌ی 34

سؤال

شاعر پنج جمله می‌سازد. برای هر جمله، اگر عیناً در POEMS بود کنارش بنویس (real)، وگرنه (made up) و made_up را یکی زیاد کن. یعنی به‌جای print(text) بنویس print(text, "(real)") یا print(text, "(made up)"). خط آخر می‌گوید چند جمله‌ی ساختگی ساخت که شبیه شعرهای واقعی‌اند. این توهم کوچک ماست!

راهنما

if text in POEMS: print(text, "(real)") و else: با print(text, "(made up)") و made_up += 1.

یک جواب درست

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])

import random
random.seed(8)

made_up = 0
for n in range(5):
    line = ["the"]
    while line[-1] in table and len(line) < 6:
        line.append(random.choice(table[line[-1]]))
    text = " ".join(line)
    if text in POEMS:
        print(text, "(real)")
    else:
        print(text, "(made up)")
        made_up += 1
print("made up:", made_up, "of 5")

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