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

جواب «Followers Table» چالش 1 از جلسه‌ی 35

سؤال

جدول بعدی‌ها را کامل کن: برای هر کلمه (به جز آخری) کلمه‌ی بعدی را به لیستش اضافه کن. برنامه followers["the"] و followers["cat"] را چاپ می‌کند: ['cat', 'mat', 'cat', 'fish'] و ['sat', 'ate'].

راهنما

if word not in followers: followers[word] = [] و بعد followers[word].append(next_word).

یک جواب درست

text = "the cat sat on the mat the cat ate the fish"
words = text.split()

followers = {}
for i in range(len(words) - 1):
    word = words[i]
    next_word = words[i + 1]
    if word not in followers:
        followers[word] = []
    followers[word].append(next_word)

print(followers["the"])
print(followers["cat"])

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