How to resolve the algorithm I before E except after C step by step in the D programming language
How to resolve the algorithm I before E except after C step by step in the D programming language
Table of Contents
Problem Statement
The phrase "I before E, except after C" is a widely known mnemonic which is supposed to help when spelling English words.
Using the word list from http://wiki.puzzlers.org/pub/wordlists/unixdict.txt, check if the two sub-clauses of the phrase are plausible individually:
If both sub-phrases are plausible then the original phrase can be said to be plausible. Something is plausible if the number of words having the feature is more than two times the number of words having the opposite feature (where feature is 'ie' or 'ei' preceded or not by 'c' as appropriate).
As a stretch goal use the entries from the table of Word Frequencies in Written and Spoken English: based on the British National Corpus, (selecting those rows with three space or tab separated words only), to see if the phrase is plausible when word frequencies are taken into account.
Show your output here as well as your program.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm I before E except after C step by step in the D programming language
Source code in the d programming language
import std.file;
import std.stdio;
int main(string[] args) {
if (args.length < 2) {
stderr.writeln(args[0], " filename");
return 1;
}
int cei, cie, ie, ei;
auto file = File(args[1]);
foreach(line; file.byLine) {
auto res = eval(cast(string) line);
cei += res.cei;
cie += res.cie;
ei += res.ei;
ie += res.ie;
}
writeln("CEI: ", cei, "; CIE: ", cie);
writeln("EI: ", ei, "; IE: ", ie);
writeln("'I before E when not preceded by C' is ", verdict(ie, ei));
writeln("'E before I when preceded by C' is ", verdict(cei, cie));
return 0;
}
string verdict(int a, int b) {
import std.format;
if (a > 2*b) {
return format("plausible with evidence %f", cast(double)a/b);
}
return format("not plausible with evidence %f", cast(double)a/b);
}
struct Evidence {
int cei;
int cie;
int ei;
int ie;
}
Evidence eval(string word) {
enum State {
START,
C,
E,
I,
CE,
CI,
}
State state;
Evidence cnt;
for(int i=0; i<word.length; ++i) {
char c = word[i];
switch(state) {
case State.START:
if (c == 'c') {
state = State.C;
}
if (c == 'e') {
state = State.E;
}
if (c == 'i') {
state = State.I;
}
break;
case State.C:
if (c == 'e') {
state = State.CE;
} else if (c == 'i') {
state = State.CI;
} else if (c != 'c') {
state = State.START;
}
break;
case State.E:
if (c == 'c') {
state = State.C;
} else if (c == 'i') {
cnt.ei++;
state = State.I;
} else if (c != 'e') {
state = State.START;
}
break;
case State.I:
if (c == 'c') {
state = State.C;
} else if (c == 'e') {
cnt.ie++;
state = State.E;
} else if (c != 'i') {
state = State.START;
}
break;
case State.CE:
if (c == 'i') {
cnt.cei++;
state = State.I;
}
if (c == 'c') {
state = State.C;
}
state = State.START;
break;
case State.CI:
if (c == 'e') {
cnt.cie++;
state = State.E;
}
if (c == 'c') {
state = State.C;
}
state = State.START;
break;
default:
assert(0);
}
}
return cnt;
}
You may also check:How to resolve the algorithm Happy numbers step by step in the Groovy programming language
You may also check:How to resolve the algorithm Self numbers step by step in the Java programming language
You may also check:How to resolve the algorithm Polymorphism step by step in the Ada programming language
You may also check:How to resolve the algorithm Singly-linked list/Element insertion step by step in the ooRexx programming language
You may also check:How to resolve the algorithm Substring step by step in the Common Lisp programming language