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

A Prompt with Sources چالش 5 از جلسه‌ی 42، سخت

build_prompt(question, chunks, picked) پرامپت را بسازد: خط اول دقیقاً Use ONLY the handout parts below. If the answer is not there, say: I don't know.؛ بعد برای هر اندیس در picked (به همان ترتیب) یک خط [2] + متن تکه (شماره = اندیس + ۱)؛ و آخر Question: + سؤال. خطوط با \n جدا شوند.

HANDOUT = """Nilu is a small made-up planet with two moons, Pip and Tov.
A day on Nilu lasts 30 hours, and a year lasts 200 days.

The glow trees of Nilu shine blue at night. They drink rain water
and grow 2 meters every year.

The sand foxes of Nilu sleep in the day and hunt at night.
They eat glow berries and small beetles.

Pip is the bigger moon. It turns around Nilu in 12 days.
Tov is tiny and turns around Nilu in 5 days."""

STOP = {"the", "a", "an", "is", "of", "in", "on", "and", "what", "how", "do", "does", "it", "at", "to", "many"}

def words(text):
    clean = ""
    for ch in text.lower():
        if ch in ".,?!":
            clean += " "
        else:
            clean += ch
    result = set()
    for w in clean.split():
        if w not in STOP:
            result.add(w)
    return result

def split_chunks(text):
    chunks = []
    for part in text.split("\n\n"):
        part = part.strip()
        if part != "":
            chunks.append(part)
    return chunks

def score(question, chunk):
    return len(words(question) & words(chunk))

def build_prompt(question, chunks, picked):
    return question


# ---- test (do not change) ----
chunks = split_chunks(HANDOUT)
print(build_prompt("How fast do glow trees grow?", chunks, [1]))
print("---")
print(build_prompt("Tell me about the moons", chunks, [3, 0]))

راهنما و جواب این چالش · برگشت به درس جلسه‌ی 42: امتحان کتاب‌باز · چالش بعدی: The Handout Bot

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