حلولي في كودرهب

استبدال The

سهل5 نقاطنصوصPythonJavaScript

الوصف

قم بكتابة function تستقبل قيمة نصية من نوع string تقوم الـدالة function باستبدال the الى a أو an حسب القاعدة الإنجليزية ثم تقوم بإرجاع النتيجة من نوع string

أمثلة

المدخلاتالمخرجات المتوقعة
txt = "Here is the plan""Here is a plan"
txt = "I ate the apple""I ate an apple"
txt = "I go to the mall""I go to a mall"
txt = "I want the orange""I want an orange"

جرّب تحله في كودرهب

الحل

def replaceThe(txt: str) -> str:
    words = txt.split()

    for i, word in enumerate(words):
        if word == 'the':
            if words[i+1][0] in ['a', 'e', 'i', 'o', 'u']:
                words[i] = 'an'
            else:
                words[i] = 'a'

    return ' '.join(words)

في هذه الصفحة