Find words which contains more than 3 e vowels: Difference between revisions

Content added Content deleted
(Added a JavaScript version)
Line 1,238: Line 1,238:


(Also possible to do this with regular expressions, but this works. Here, we counted how many times each vowel occurred, limited the maximum count to 4, and checked the resulting signature.)
(Also possible to do this with regular expressions, but this works. Here, we counted how many times each vowel occurred, limited the maximum count to 4, and checked the resulting signature.)

=={{header|JavaScript}}==
ECMAScript defines no file operations. Here, to read the dictionary, we use a library available to Apple's "JavaScript for Automation" embedding of a JS interpreter.
<syntaxhighlight lang="javascript">(() => {
"use strict";

// ----- MORE THAN THREE VOWELS, AND NONE BUT E ------

// p :: String -> Bool
const p = w => {
// True if the word contains more than three vowels,
// and none of its vowels are other than "e".
const
[eCount, noOtherVowels] = [...w].reduce(
([n, bool], c) => bool
? "e" === c
? [1 + n, bool]
: "aiou".includes(c)
? [n, false]
: [n, bool]
: [n, false],

// Initial accumulator.
[0, true]
);

return noOtherVowels && (3 < eCount);
};

// ---------------------- TEST -----------------------
const main = () =>
lines(
readFile(
"~/Desktop/unixdict.txt"
)
)
.filter(p)
.join("\n");

// --------------------- GENERIC ---------------------

// lines :: String -> [String]
const lines = s =>
// A list of strings derived from a single string
// which is delimited by \n or by \r\n or \r.
0 < s.length
? s.split(/\r\n|\n|\r/u)
: [];

// ----------------------- jxa -----------------------

// readFile :: FilePath -> IO String
const readFile = fp => {
// The contents of a text file at the
// given file path.
const
e = $(),
ns = $.NSString
.stringWithContentsOfFileEncodingError(
$(fp).stringByStandardizingPath,
$.NSUTF8StringEncoding,
e
);

return ObjC.unwrap(
ns.isNil() ? (
e.localizedDescription
) : ns
);
};

// MAIN ---
return main();
})();</syntaxhighlight>
{{Out}}
<pre>belvedere
dereference
elsewhere
erlenmeyer
evergreen
everywhere
exegete
freewheel
nevertheless
persevere
preference
referee
seventeen
seventeenth
telemeter
tennessee</pre>


=={{header|jq}}==
=={{header|jq}}==