363 I before E

I Before E Rule — 363[Easy]

“I before E except after C” is perhaps the most famous English spelling rule. For the purpose of this challenge, the rule says:

  • if “ei” appears in a word, it must immediately follow “c”.
  • If “ie” appears in a word, it must not immediately follow “c”.

So how do we approach this problem? We have 2 in total. We have the main challenge, and we’re provided with the following list list: “a”,”zombie”,”transceiver”,”veil”, and “icier”; however, after that we have a larger challenge that uses the following text file with a lot more names in it.


Short rundown how I handled this script, or to be more precise, the check function.

  • From the start, I only care if the word is bad (breaks a rule)
  • There is an optional character just in case you want to make your own rule in the future, it’s an optional param, so it is defaulted to “c
  • I make use of the find function to see if the “ei” exists in the string–failure to find returns a -1
  • Check the bad case first. If it’s found (icier for example) and increase the bad counter.
  • Next we check to see if “ei” was found in the word. If it was, we check to see if it’s a good case–transceiver for example
  • If that bad count is not zero, we return false, and handle it on the main thread.

def check(inStr, special = "c"):
	#search for ei
	badCount = 0
	ei = inStr.find("ei")
	good = inStr.find(special + "ei")
	bad = inStr.find(special + "ie")

	if(bad > -1):
		badCount = badCount + 1

	if(ei > -1):
		if(good == -1):
			badCount = badCount + 1

	if(badCount > 0):
		return False
		return True	

if __name__ == "__main__":	
	print "# Challenge"
	wordlist = ["a","zombie","transceiver","veil","icier"]
	for word in wordlist:
		exe = check(word.lower())
		print "check(\"" + word + "\") =>" + str(exe)

	print "# Optional Bonus 1"
	exceptions = 0	
	with open("enable1.txt") as f:
	    for line in f:
	    	exe = check(line.lower())
	    	if(exe == False):
	    		exceptions += 1
	print "I before E except after C Exceptions: " + str(exceptions)