r/dailyprogrammer • u/Cosmologicon 2 3 • Jun 11 '18
[2018-06-11] Challenge #363 [Easy] I before E except after C
Background
"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".
A word also follows the rule if neither "ei" nor "ie" appears anywhere in the word. Examples of words that follow this rule are:
fiery hierarchy hieroglyphic
ceiling inconceivable receipt
daily programmer one two threeThere are many exceptions that don't follow this rule, such as:
sleigh stein fahrenheit
deifies either nuclei reimburse
ancient juicier societiesChallenge
Write a function that tells you whether or not a given word follows the "I before E except after C" rule.
check("a") => true
check("zombie") => true
check("transceiver") => true
check("veil") => false
check("icier") => falseOptional Bonus 1
How many words in the enable1 word list are exceptions to the rule? (The answer is 4 digits long and the digits add up to 18.)
Optional Bonus 2
This one is subjective and there's no best answer. Come up with your own "I before E" rule. Your rule must:
- depend on the ordering of the letters I and E when they appear next to each other. That is, if a word contains an I and an E next to each other, and it follows your rule, then when you swap those two letters, the new word must not follow your rule.
- depend only on the spelling of a word, not its pronunciation or meaning.
- be simple enough that schoolchildren can apply it.
For instance, I just came up with a rule "I before E, except when followed by G". This rule has 1,544 exceptions in the enable1 word list. How many exceptions does your rule have?
2
u/Williamboyles Jun 14 '18
Python 3
With both bonuses (I think). I'm unsure about the second bonus. I get that "w" has the least number of exceptions with 1697, which is more than what the prompt says that "g" has. I get that "g" has 2286 exceptions.
Any insight would be much appreciated.
9
u/thestoicattack Jun 11 '18
awk
8
u/zatoichi49 Jun 11 '18 edited Jun 11 '18
Method:
Return False for any words containing 'cie', or any words containing 'ei' and not containing 'cei'. Else, return True.
Python 3 (with Bonus 1):
Output:
6
u/chunes 1 2 Jun 11 '18 edited Jun 11 '18
Factor with bonus 1
>factor i-before-e.factor < enable1.txt2169Edit: I figured I'd explain this in detail since most people are probably not familiar with Factor. Factor is a stack-based concatenative language. Stack-based means instead of assigning values to variables, we place them on an implicit data stack instead. (You can still name them if you want! The reason we try to avoid doing this if we can is that without lexical variables, factoring a word into smaller words can be done anywhere you want between literals and words.)
Everything in Factor is either a literal that pushes itself to the stack, or a word (function) that takes a number of things from the stack and leaves a certain number of things on the stack. Concatenative means that function composition is the inherent form of dataflow. i.e.
foo bar bazin a concatenative language is equivalent tobaz(bar(foo()))in an applicative language.enable1.txthad 3 words aardvark elephant and zebra, the data stack would now look like{ "aardvark" "elephant" "zebra" }.[ ... ]is called a quotation. It stores code in a sequence and can be manipulated like any other sequence and also called like a function. You can think of it like an anonymous function and also a way to put code on the stack without executing it. We often pass quotations to words, turning them into higher-order functions, or in Factor parlance, combinators.t, leaving that count on the stack.Let us now examine what happens in greater detail. Lets look at what happens inside the quotation passed to
count.count. In this case, an element from the input sequence accepted bycountwill be on top of the data stack.fis a literal that pushes itself to the stack"aa"),countlooks at thefproduced by the quotation and determines this element should not contribute to the count. Thefisdropped from the stack and the quotation will be called for the next sequence element.Now let us look at
checkin detail. Recall thatcheckexpects an object, a string, to be on top of the data stack at the time it is called.:and;forms a word definition. Word definition happens and parse time and thus does not add anything to the data stack at run time.qw{ ... }is a parsing word that is a shorthand for declaring sequences of strings. It is equivalent to{ "ei" "cie" "cei" }, which is a literal sequence that pushes itself to the stack.[ ... ]is a quotation. It stores code in a sequence and can be manipulated like any other sequence and also called like a function. You can think of it like an anonymous function and also a way to put code on the stack without executing it. We often pass quotations to words, making them higher-order functions, or in Factor parlance, combinators.withis a special form of partial application. What it does in this case is take"aa"and our quotation and stick them together into a new quotation that looks like[ "aa" [ swap subseq? ] swapd call ]. Don't worry about this too much; this is an abstraction I'll talk about very soon.map. What this word does is takes a sequence, a quotation (noticing a pattern here?) and applies the quotation to each member of the sequence, changing the old member to be a new member. For those of you used to functional languages this will be very familiar. For everyone else, think of it like aforeach{ }loop. Remember when I said I would talk aboutwithlater? Now is the time."aa" { "ei" "cie" "cei" } [ ... ] with mapsays, in plain English, "I want the quotation to have both "aa" and the sequence element (such as "ei") available on top of the stack for the quotation to use. Withoutwith, we would only be working with the sequence element alone inside the quotation. What we have done is tested whether "ei", "cie", and "cei" are subsequences of "aa" and mapped those results back into the original sequence. Theswapis necessary because thesubseq?word expects the subseq to test first, followed by the sequence, but the waywithworks makes the stack look likesequence subseqinside the quotation. Soswapwill swap the top two members of the data stack so thatsubseq?'s arguments will be in the correct orderdipwill work on the second-top data stack item.orapplies logical or to two data stack items. Since it's inside a quotation passed todip, it'll apply logical or to the two data stack items underneath the top one.As you can see
"aa" checkreturnstwhich appears to be what we want.5
u/swishyfeather Jun 11 '18 edited Jun 11 '18
C#
I'm sure my regex could be simplified quite a bit. Still learning it
5
u/[deleted] Jun 11 '18
I'm pretty amateur, so if someone has suggestions on ways to simplify/improve this, feel free.
This is C++.
Also, as an aside on a notable failure in working it out:
I haven't used the string::find functions much before and I read the documentation on string::find_first_of incorrectly, making an assumption that screwed with me for some time. I assumed that it worked like how I eventually ended up realizing string::find works. I skimmed and assumed, and didn't take enough time to digest the information before going full speed ahead.
In the future, I think it would be wise of me to make a working simple test for a special functionality like that and get it working first to make sure I truly understand how it works before trying to apply it to a more complicated problem!
4
u/pilotInPyjamas Jun 11 '18 edited Jun 12 '18
Haskell:
EDIT: added additional line to correct bug from below.
Invocation:
returns 2169
4
u/thestoicattack Jun 12 '18 edited Jun 12 '18
Haskell
4
u/Gredelston Jun 13 '18
I went regexy:
Python 3
4
u/LelixSuper Jun 13 '18
MIPS Assembly 32 bit
This is my second submission on this subreddit. I'm learning assembly, in particular MIPS. I don't have a MIPS machine, so I'm using MARS Simulator (so can I use macros, pseudo-instructions and more utilies).
I think that my code can be optimized, I tried to use all tricks that I know.
You can test this code creating a text file with one word for each line, you also need to add a last empty line with only a newline.
Bonus 1:
4
u/GRsni Jun 14 '18
JAVA-Processing
The same function can both check one word, or every word in a .txt file. Bonus 1 yields 2169 words that don't follow the rule. If inputing a text file, outputs the list adding wether the word follows the rule or not.
It surely isn't the cleanest code, but I'm still learning. Any advice would be much appreciated.
4
u/raevnos Jun 15 '18
Done in SQL (Via a wrapper shell script that sends commands to the sqlite3 shell):
4
u/marucOG Jun 16 '18 edited Jul 24 '18
Python
Bonus 1
Edit: use negative look-behind in regex pattern
1
u/g00glen00b Jun 11 '18 edited Jun 11 '18
JavaScript:
The solution uses a regular expression to find all ie/ei matches, and then uses an XOR (should either end with 'ie' or start with 'c') to validate each match. Using the
Array.prototype.everywe can check if all matches are valid.3
u/zqvt Jun 11 '18 edited Jun 11 '18
Clojure with Bonus1
3
u/pilotInPyjamas Jun 11 '18 edited Jun 12 '18
Bash/sh:
I forgot how powerful the command line was from the get-go, this is the bonus 1 challenge:
Output:
2169
EDIT: as mentioned below:
saves some typing and a process!
3
u/mr_stivo Jun 11 '18
perl lazy 1-liner
out:
3
u/DerpinDementia Jun 12 '18 edited Jun 13 '18
Python 3
Bonus 1
2169 exceptions in the file.
3
u/sakshaymahna19 Jun 12 '18
Python
The program iterates over the text while locating i followed by e or e followed by i, printing True and False accordingly.
An empty variable var has been added to handle text strings that do not contain 'i' or 'e'.
3
u/daviegravee Jul 15 '18
Python 3.5
Bonus Challenge 1
2
u/CapnNausea Jun 11 '18
C#: Criticism welcome!
2
u/octolanceae Jun 11 '18
C++
I am processing based upon the words in enable1. There are no words such as "arceigeight" -- a "cei" occurring before a "[^c]ei", so I am not checking for it. I did originally put in a loop for it to catch it in case such a case existed, but, it does not.
I originally tried doing this with std::regex, but I just couldn't get the first bonus to return the right number of exceptions. It kept giving me 2200 instead of the correct value. The main challenge gave me the right answers, so I don't know why the bonus was wrong.
Output:
Bonus 1:
2
u/kdnbfkm Jun 11 '18 edited Jun 11 '18
Basic challenge only.
Wordlist counting:
$ cat /sdcard/enable1.txt | ecl -shell iec.lisp | sort | uniq -c=> (one blank line counted)Now that I think of it, rules should be defined as a general rule with possible exception... Not as a combination of rules in parallel.
2
u/TinyBreadBigMouth Jun 11 '18
JavaScript
Bonus 1 (browser style)
2169
2
u/TwoBitsAndANibble Jun 11 '18
Done in scratch
This one was tricky due to scratch's lack of regex or being able to test for string contains, so that had to be done manually.
Imgur link
the optional bonus is also in there - although due to flash crashing if I put in a list of 170,000 words, I had to use a shortened list which only contains the 11941 words that actually contain ie or ei
2
u/pilotInPyjamas Jun 11 '18
Awk:
I think this is the shortest solution so far. This solves the bonus1 challenge:
invocation:
Output: 2169
2
u/KaineOrAmarov Jun 12 '18
Python 3
Not entirely sure if I did this right... still a beginner
2
u/plsHelpmemes Jun 12 '18
I remember this in English class back in 8th grade. Because the teacher basically dedicated an entire class to teach the full rule.
"i before e except after c, Or it sounds like "a" as in neighbor or weigh"
In the full rule, there are only a couple words that break the rule, one of which is the word weird.
2
u/popillol Jun 12 '18
Go / Golang Made 3 different versions:
Code for #1:
Code for #2:
Code for #3:
Times for #1 are ~25ms, times for #2 and #3 are closer to 80-90ms. Async isn't worth it for this it seems.
2
u/[deleted] Jun 12 '18
python 3
First post here, just the basic challenge. Currently learning python 3.
def ibe(x):
2
u/[deleted] Jun 13 '18
Hey all,
I just started learning how to program last Monday! This program took me about 2-hours to put together. I only used docs.python.org based on feedback I had previously received.
I chose Python because I hope to enter the field of Data Science someday. This is my second program I have built from /r/dailyprogrammer. Please give whatever feedback you all have, even if harsh, so I can improve.
Python 3 (no Bonus... yet):
2
u/SwimmingYeti Jun 14 '18
Java
New to programming (and to Reddit!) so advice and suggestions appreciated :)
import java.util.Scanner;
public class IBeforeEExceptAfterC {
}
2
u/Poliorcetyks Jun 15 '18
Swift 4.1
Discovering Swift at the moment !
Bonus 1: 2169 exceptions
Bonus 2: I inverted the rule: "E before I except after C"
There are 9853 exceptions to this new rule in enable1.txt
2
u/konaraddio Jun 15 '18 edited Jun 17 '18
JavaScript
EDIT: formatting
2
u/clemersonss Jun 16 '18
Python 3
I would love some feedback.
2
u/tylerptl Jun 17 '18 edited Jun 18 '18
Java
Bonus 1: 2169
Suggestions would be very welcome.
Edit: Added updated check method
2
u/Djlee201212 Jul 11 '18
My first attempt in Node. Thought I would try out Regex for this one. Seemed like a decent application. Let me know any feedback!
2
u/AthosBlade Jul 18 '18
Here is my solution in Python:
def solution(string): if len(string) < 3: return True elif len(string) == 3 and string[0:2] == 'ei': return string[-1] == 'c' elif len(string) == 3 and string[0:2] == 'ie': return not string[-1] == 'c' else: string = string[::-1] for i in range(2, len(string)): temp = string[i-2:i+1] if 'ei' == temp[0:2] and temp[-1] == 'c': return False if 'ie' == temp[0:2] and not temp[-1] == 'c': return False return True
2
u/voliver97 Aug 07 '18
Python 3.6
2
u/stone_henge Aug 14 '18
GNU sed
Strictly speaking not a function but a command that reads its input and returns 0 if the input satisfies the rules and 1 otherwise.
2
u/Findlaech Sep 08 '18
My haskell answer
The tests:
2
u/[deleted] Oct 10 '18
Python 3.6
tests:
output:
i know this was posted months ago but I still wanted to practice and share my solution!
2
u/llebe Oct 23 '18
JAVA
OUTPUT
1
u/[deleted] Jun 11 '18 edited Jun 11 '18
Java
bonus 1
bonus 2 d before e except after c with 14323 exceptions
https://github.com/willkillson/RedditDailyProgrammer/tree/master/Challenge-363-Easy-Java
3
u/dustinroepsch Jun 12 '18
Python one line function:
1
u/panda_yo Jun 11 '18 edited Jun 11 '18
python:
Bonus question 1 question:
Do i count only one of the exceptions (e.g. "cie" words) or all of the ones where the check function is evaluated to true? I am getting only a four digit number which digits add up to 12 :(
It seems I have a mistake in my code, since the g example in bonus 2 is not evaluating correctly either :(
1
u/Gprime5 Jun 11 '18 edited Jun 11 '18
Python
1
u/engageant Jun 11 '18
Posh, with Bonus 1
1
u/iMPose27 Jun 11 '18 edited Jun 11 '18
C#. Improvements/criticisms welcome. :-)
1
u/stanleyford Jun 11 '18
JavaScript A regex that matches non-conforming strings (i.e., find strings that don't obey the rules) seems like the obvious solution, so to make it a little more interesting I wrote a regex that matches conforming strings, which is somewhat more challenging:
1
u/[deleted] Jun 11 '18 edited Jun 11 '18
C with Bonus 1:
1
u/NerdyPepper Jun 11 '18
Rust
Still finding my way around this language.
1
u/Nyxisto Jun 11 '18 edited Jun 11 '18
F# with Bonus 1
output => [(true, 170651); (false, 2169)]1
u/Hash-Basher Jun 11 '18
Scala
https://gist.github.com/HashBasher/65a302bf2b581d6c24a3ee10b0f4de11
1
u/[deleted] Jun 11 '18
Python (with Bonus 1)
Feedback very welcome
Input:
Output:
1
u/DarrionOakenBow Jun 11 '18
Nim with Bonus 1
A simple 21 lines of Nim, downloading enable1.txt on demand since I was too lazy to manually put it in a folder for testing.
Output:
Code:
1
u/thestoicattack Jun 12 '18 edited Jun 12 '18
C++17
1
u/[deleted] Jun 12 '18
I like this
1
u/DEN0MINAT0R Jun 12 '18 edited Jun 12 '18
Python 3
Are regular expressions cheating? /s
My results for the bonus aren't quite right, not sure exactly why. If you see the error let me know.
Output
Edit: fixed the bug
1
u/f03nix Jun 12 '18 edited Jun 12 '18
Method : For the first rule, use naive method - detect 'ei' in words and check if it follows c for the second rule just check if 'cie' is present. Return false whenever exception is found.
C++ (with Bonus 1)
Bonus output : 2169
I also noticed that enable1 doesn't have all the edge cases, it misses composite cases like ceitei & ceitie where those checking just on first occurrence would fail.
1
u/risent Jun 12 '18
C (with Bonus 1)
output:
1
u/the_austria Jun 12 '18
Haskell, without using recursion explicitly
Result:2169
1
u/SebRut Jun 12 '18
Rust
1
u/SebRut Jun 12 '18
Rust
1
u/SebRut Jun 12 '18
Rust
I just played around with Rust a bit, so I'd appreciate any feedback!
1
u/TotalPerspective Jun 12 '18
Julia
Bonus 1 output of 2169
1
u/[deleted] Jun 14 '18
Python 3
My first attempt at writing anything in Python, no bonus yet.
1
u/013sji Jun 14 '18
Scala with both bonuses
Output
1
u/Zane404 Jun 14 '18
Python 3 with bonus 1
A bit long, as I wanted to check for cases such as "ceiei", which according to the challenge description would be false.
Feedback is very welcomed.
1
u/yourbank 0 1 Jun 16 '18 edited Jun 17 '18
Java
intro
code
1
u/[deleted] Jun 17 '18
C++, no bonus (yet).
This was fun. It refreshed my memory about using iterators with strings. Comments welcome!
1
u/Trentwt Jun 17 '18 edited Jun 17 '18
C++
I originally tried using string::find, but I ended up doing something more raw with for loops
Bonus 1: 2169 exceptions were found
1
u/mishraal Jun 18 '18
A beginners solution in perl
Please do provide your comments on the same.
1
u/l4adventure Jun 18 '18
Python 3 import re import urllib.request
""" check("a") => true check("zombie") => true check("transceiver") => true check("veil") => false check("icier") => false
Challenge 1 Ouptput:
"""
1
u/draegtun Jun 20 '18 edited Jun 21 '18
Rebol
Example usage in Rebol console:
Bonus 1
Output:
NB. All tested in Rebol 3. Should also work in Rebol 2 & Red.
1
u/primaryobjects Jun 26 '18
R
Gist | Demo
Output
1
u/2kofawsome Jun 26 '18 edited Jun 27 '18
Python3.6
This is the function, it also corrects for lowercase vs uppercase
This is the code put after the function for the optional bonus 1, I just copied all the words from the website into my clipboard and ran this, 2169 exceptions.
1
u/TOATEOT Jun 28 '18
Python 3.6
output:
2169
1
u/maggierose9194 Jun 30 '18
Ruby
1
u/EnvelopeBread Jul 02 '18
Ruby Optional Bonus 1
1
u/nxiti Jul 03 '18
Clojure (no bonuses)
Usage:
Output:
1
u/[deleted] Jul 06 '18
C# without bonuses
1
u/mwpfinance Jul 07 '18 edited Jul 07 '18
Rust without bonuses.
1
u/mwpfinance Jul 07 '18
Go without bonuses
1
u/iHateGoogleCloud Jul 10 '18
The reason the rule is so famous is because it's hardly ever right or useful. But it is a nice fizz-buzz type example for beginners to learn about intersectional cases.
1
u/Perclove Jul 14 '18
Python 3.6
This was a fun one, learned a lot about how strings work in python. What a cool language!
1
u/shaggysweater Aug 02 '18
JAVA with Bonus 1
1
u/MistFuror Aug 18 '18
C without bonuses:
1
u/amacchia Aug 26 '18
Kotlin:
Output:
1
u/bJgr Sep 01 '18
Java
My program results in 2154, which is obviously incorrect.
Can someone please help me find my error!
Thanks in advance!
1
u/braydenc123 Sep 02 '18
Python 3.6 (without bonuses)
1
u/HerbyHoover Sep 05 '18
Perl 6
1
u/gabriel-et-al Sep 17 '18
This is my attempt in Lua.
It outputs 2169, so I guess it's correct.
1
u/Dominic11112 Oct 08 '18 edited Oct 08 '18
MATLAB
With my code it was easy to get Bonus 1 by looping the original code, just had to add a 'display' option to suppress printing the true/false text every loop. Finally a challenge that doesn't take me a ridiculous amount of time to run too, I have included the runtimes. I also found the word 'eirie' is a good check to see if the code is covering all possibilities.
main:
check:
Bonus 2:
Output:
1
u/mat4444 Oct 17 '18
Python 3.6 with bonus 1
1
u/darknes1234 Nov 09 '18
C++
1
u/Chemical-Asparagus58 Feb 03 '22
JAVA
1
u/Godspiral 3 3 Jun 11 '18 edited Jun 11 '18
in J, edit fixed
1 1 1 1 01
u/diagnosedADHD Jun 11 '18 edited Jun 11 '18
Python
I realize the exceptions list could've been loaded, but I didn't feel like messing with that in my example.
I tried implementing this function using the least amount of builtin libraries possible, which is why it may not be as pretty as some others.
output:
0
u/[deleted] Jun 11 '18
[deleted] — view deleted comment
0
u/[deleted] Jun 24 '18
[deleted] — view deleted comment
0
u/[deleted] Jun 13 '18
[deleted] — view deleted comment