--- id: 69f35a5bb823ed620fcb7cbd title: "Challenge 284: I Before E" challengeType: 29 dashedName: challenge-284 --- # --description-- Given a word or sentence, return a corrected version where every word follows the "I before E except after C" rule. - If a word contains `"ei"` not preceded by `"c"`, replace it with `"ie"`. - If a word contains `"ie"` preceded by `"c"`, replace it with `"ei"`. - All other words are left unchanged. # --hints-- `i_before_e("beleive")` should return `"believe"`. ```js ({test: () => { runPython(` from unittest import TestCase TestCase().assertEqual(i_before_e("beleive"), "believe")`) }}) ``` `i_before_e("recieve")` should return `"receive"`. ```js ({test: () => { runPython(` from unittest import TestCase TestCase().assertEqual(i_before_e("recieve"), "receive")`) }}) ``` `i_before_e("we recieved a breif")` should return `"we received a brief"`. ```js ({test: () => { runPython(` from unittest import TestCase TestCase().assertEqual(i_before_e("we recieved a breif"), "we received a brief")`) }}) ``` `i_before_e("she beleived the friendly niece could percieve the greif")` should return `"she believed the friendly niece could perceive the grief"`. ```js ({test: () => { runPython(` from unittest import TestCase TestCase().assertEqual(i_before_e("she beleived the friendly niece could percieve the greif"), "she believed the friendly niece could perceive the grief")`) }}) ``` `i_before_e("we recieved relief after the theif gave us a breif piece of feirce deceit")` should return `"we received relief after the thief gave us a brief piece of fierce deceit"`. ```js ({test: () => { runPython(` from unittest import TestCase TestCase().assertEqual(i_before_e("we recieved relief after the theif gave us a breif piece of feirce deceit"), "we received relief after the thief gave us a brief piece of fierce deceit")`) }}) ``` # --seed-- ## --seed-contents-- ```py def i_before_e(sentence): return sentence ``` # --solutions-- ```py import re def i_before_e(sentence): words = [] for word in sentence.split(" "): word = re.sub(r"([^c])ei", r"\1ie", word) word = re.sub(r"cie", "cei", word) words.append(word) return " ".join(words) ```