جواب «Build the Table» چالش 2 از جلسهی 34
سؤال
یک جمله با input خوانده و به کلمهها شکسته شده. جدول کلمهی بعدی را بساز: برای هر i از 0 تا len(words) - 2، اگر words[i] در table نبود یک لیست خالی برایش بساز، بعد words[i + 1] را به لیستش اضافه کن. برای the cat sat on the mat:{'the': ['cat', 'mat'], 'cat': ['sat'], 'sat': ['on'], 'on': ['the']}
راهنما
for i in range(len(words) - 1):، if words[i] not in table: table[words[i]] = []، بعد table[words[i]].append(words[i + 1]).
یک جواب درست
words = input().split()
table = {}
for i in range(len(words) - 1):
if words[i] not in table:
table[words[i]] = []
table[words[i]].append(words[i + 1])
print(table)
اول خودت امتحان کن؛ جوابهای دیگر هم قبول است. برگرد و خودت حلش کن