جواب «Cut into Chunks» چالش 1 از جلسهی 71
سؤال
تابع chunk_words(text, size) را بنویس که متن را به تکههای size کلمهای تقسیم کند (تکهی آخر ممکن است کوتاهتر باشد) و لیست تکهها را برگرداند. برنامه با size = 20 تکهها را شمارهدار چاپ میکند. ببین جملهی «This is called condensation» کجا نصف شده! خروجی:0: The water cycle moves water around the Earth. The sun heats water in seas and lakes. The warm water turns1: into vapour and rises. This is called evaporation. High in the sky the vapour cools and forms clouds. This is2: called condensation. When the drops in a cloud get heavy, they fall as rain or snow. This is called precipitation.
راهنما
for start in range(0, len(words), size): و داخلش chunks.append(" ".join(words[start:start + size])).
یک جواب درست
NOTES = ("The water cycle moves water around the Earth. The sun heats water in seas and lakes. "
"The warm water turns into vapour and rises. This is called evaporation. High in the sky "
"the vapour cools and forms clouds. This is called condensation. When the drops in a cloud "
"get heavy, they fall as rain or snow. This is called precipitation.")
def chunk_words(text, size):
words = text.split()
chunks = []
for start in range(0, len(words), size):
chunks.append(" ".join(words[start:start + size]))
return chunks
for i, chunk in enumerate(chunk_words(NOTES, 20)):
print(f"{i}: {chunk}")
اول خودت امتحان کن؛ جوابهای دیگر هم قبول است. برگرد و خودت حلش کن