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

m
(Added AppleScript.)
m (→‎{{header|Wren}}: Minor tidy)
 
(8 intermediate revisions by 3 users not shown)
Line 1,041:
telemeter
tennessee
</pre>
 
 
=={{header|FutureBasic}}==
<syntaxhighlight lang="futurebasic">
include resources "unixdict.txt"
 
local fn ThreePlusEWord( string as CFStringRef ) as BOOL
long i, count = len(string), ecount = 0
for i = 0 to count -1
unichar ch = fn StringCharacterAtIndex( string, i )
select (ch)
case _"a", _"A", _"i", _"I", _"o", _"O", _"u", _"U" : exit fn = NO
case _"e", _"E" : ecount++
case else : continue
end select
next
if ecount > 3 then exit fn = YES
end fn = NO
 
void local fn CheckWords
CFURLRef url = fn BundleURLForResource( fn BundleMain, @"unixdict", @"txt", NULL )
CFStringRef string = fn StringWithContentsOfURL( url, NSUTF8StringEncoding, NULL )
CFArrayRef words = fn StringComponentsSeparatedByCharactersInSet( string, fn CharacterSetNewlineSet )
CFMutableStringRef mutStr = fn MutableStringNew
NSUInteger count = 1
CFStringRef wordStr
for wordStr in words
if fn ThreePlusEWord( wordStr ) then MutableStringAppendString( mutStr, fn StringWithFormat( @"%2lu. %@\n", count, wordStr ) ) : count++
next
print mutStr
end fn
 
fn CheckWords
 
HandleEvents
</syntaxhighlight>
{{output}}
<pre>
1. belvedere
2. dereference
3. elsewhere
4. erlenmeyer
5. evergreen
6. everywhere
7. exegete
8. freewheel
9. nevertheless
10. persevere
11. preference
12. referee
13. seventeen
14. seventeenth
15. telemeter
16. tennessee
</pre>
 
Line 1,119 ⟶ 1,176:
valid w = not (any (`elem` "aiou") w) && length (filter (=='e') w) > 3</syntaxhighlight>
 
Or, defining the predicate in terms of a single fold:
{{out}}
 
<syntaxhighlight lang="haskell">import System.IO (readFile)
import Data.Bifunctor (first)
 
 
---------- MORE THAN THREE VOWELS, AND NONE BUT E --------
 
p :: String -> Bool
p = uncurry (&&) . first (3 <) . foldr rule (0, True)
rule :: Char -> (Int, Bool) -> (Int, Bool)
rule _ (n, False) = (n, False)
rule 'e' (n, b) = (succ n, b)
rule c (n, b)
| c `elem` "aiou" = (n, False)
| otherwise = (n, b)
 
--------------------------- TEST -------------------------
main :: IO ()
main = readFile "unixdict.txt"
>>= (putStrLn . unlines . filter p . lines)
</syntaxhighlight>
{{out}}
<pre>belvedere
dereference
Line 1,159 ⟶ 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.)
 
=={{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
[noOtherVowels, eCount] = [...w].reduce(
([bool, n], c) => bool
? "e" === c
? [bool, 1 + n]
: "aiou".includes(c)
? [false, n]
: [bool, n]
: [false, n],
 
// Initial accumulator.
[true, 0]
);
 
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}}==
Line 1,747 ⟶ 1,917:
done...
</pre>
 
=={{header|RPL}}==
As RPL is a language for calculators, it is not suitable for reading large text files.
« → w
« { 5 } 0 CON
1 w SIZE '''FOR''' j
w j DUP SUB
'''IF''' "AEIOUaeiou" SWAP POS '''THEN'''
LASTARG 1 - 5 MOD 1 +
DUP2 GET 1 + PUT
'''END'''
'''NEXT'''
DUP 2 GET 3 ≥ SWAP
[ 1 0 1 1 1 ] DOT NOT AND
» » ‘<span style="color:blue>EEE?</span>’ STO
 
"Seventeen" <span style="color:blue>EEE?</span> <span style="color:grey>@ returns 1 (true)</span>
"Eighteen" <span style="color:blue>EEE?</span> <span style="color:grey>@ returns 0 (false)</span>
 
=={{header|Ruby}}==
Line 1,874 ⟶ 2,062:
=={{header|Wren}}==
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascriptwren">import "io" for File
import "./fmt" for Fmt
 
var hasAIOU = Fn.new { |word| word.any { |c| "aiou".contains(c) } }
9,485

edits