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

Exact or Normalised? چالش 2 از جلسه‌ی 76، آسان

تابع normalise(text) را بنویس: حروف کوچک، strip، فقط حرف و عدد و فاصله نگه دار (isalnum)، و فاصله‌های پشت سر هم را یکی کن. برنامه هر جفت را یک بار دقیق و یک بار یکسان‌شده مقایسه می‌کند. خروجی:
'Paris' / 'paris.': exact no, normalised yes
'The Sun' / ' the sun ': exact no, normalised yes
'12' / 'twelve': exact no, normalised no
'water vapour' / 'Water vapour!': exact no, normalised yes
exact 0/4, normalised 3/4

def normalise(text):
    return text


PAIRS = [("Paris", "paris."), ("The Sun", "  the sun "), ("12", "twelve"), ("water vapour", "Water  vapour!")]
exact = normal = 0
for expected, reply in PAIRS:
    e = expected == reply
    n = normalise(expected) == normalise(reply)
    exact += e
    normal += n
    print(f"{expected!r} / {reply!r}: exact {'yes' if e else 'no'}, normalised {'yes' if n else 'no'}")
print(f"exact {exact}/{len(PAIRS)}, normalised {normal}/{len(PAIRS)}")

راهنما و جواب این چالش · برگشت به درس جلسه‌ی 76: ارزیابی مدل · چالش بعدی: Key Words and Partial Credit

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