June 2018 Update

This commit is contained in:
Ingy döt Net 2018-06-22 20:57:24 +00:00
parent ba8067c3b7
commit 22f33d4004
5278 changed files with 84726 additions and 14379 deletions

View file

@ -0,0 +1,124 @@
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;
}

View file

@ -0,0 +1,23 @@
USING: combinators formatting generalizations io.encodings.utf8
io.files kernel literals math prettyprint regexp sequences ;
IN: rosetta-code.i-before-e
: correct ( #correct #incorrect rule-str -- )
pprint " is correct for %d and incorrect for %d.\n" printf ;
: plausibility ( #correct #incorrect -- str )
2 * > "plausible" "implausible" ? ;
: output ( #correct #incorrect rule-str -- )
[ correct ] curry
[ plausibility "This is %s.\n\n" printf ] 2bi ;
"unixdict.txt" utf8 file-lines ${
R/ cei/ R/ cie/ R/ [^c]ie/ R/ [^c]ei/
[ count-matches ]
[ map-sum ]
[ 4 apply-curry ] bi@
} cleave
"I before E when not preceded by C"
"E before I when preceded by C" [ output ] bi@

View file

@ -0,0 +1,42 @@
# Project : I before E except after C
# Date : 2017/11/26
# Author : Gal Zsolt (~ CalmoSoft ~)
# Email : <calmosoft@gmail.com>
fn1 = "unixdict.txt"
fp = fopen(fn1,"r")
str = fread(fp, getFileSize(fp))
fclose(fp)
strcount = str2list(str)
see "The number of words in unixdict : " + len(strcount) + nl
cei = count(str, "cei")
cie = count(str, "cie")
ei = count(str, "ei")
ie = count(str, "ie")
see "Instances of cei : " + cei + nl
see "Instances of cie : " + cie + nl
see "Rule: 'e' before 'i' when preceded by 'c' is = "
if cei>cie see "plausible" + nl else see"not plausible" + nl ok
see "Instances of *ei, where * is not c : " + (ei-cei) + nl
see "Instances of *ie, where * is not c: " + (ie-cie) + nl
see "Rule: 'i' before 'e' when not preceded by 'c' is = "
if ie>ei see "plausible" + nl else see "not plausible" + nl ok
see "Overall the rule is : "
if cei>cie and ie>ei see "PLAUSIBLE" + nl else see "NOT PLAUSIBLE" + nl ok
func getFileSize fp
c_filestart = 0
c_fileend = 2
fseek(fp,0,c_fileend)
nfilesize = ftell(fp)
fseek(fp,0,c_filestart)
return nfilesize
func count(cString,dString)
sum = 0
while substr(cString,dString) > 0
sum = sum + 1
cString = substr(cString,substr(cString,dString)+len(string(sum)))
end
return sum