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

m
(add sed)
m (→‎{{header|Wren}}: Minor tidy)
 
(11 intermediate revisions by 6 users not shown)
Line 279:
16: tennessee (4)
</pre>
 
=={{header|AppleScript}}==
<syntaxhighlight lang="applescript">on findWordsWhichContainsMoreThan3EVowels()
script o
property wrds : words of ¬
(read file ((path to desktop as text) & "unixdict.txt") as «class utf8»)
end script
set output to {}
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to "e"
repeat with thisWord in o's wrds
set thisWord to thisWord's contents
if (((count thisWord's text items) > 4) and not ¬
((thisWord contains "a") or (thisWord contains "i") or ¬
(thisWord contains "o") or (thisWord contains "u"))) then
set end of output to thisWord
end if
end repeat
set AppleScript's text item delimiters to linefeed
set output to output as text
set AppleScript's text item delimiters to astid
return output
end findWordsWhichContainsMoreThan3EVowels
 
findWordsWhichContainsMoreThan3EVowels()</syntaxhighlight>
 
{{output}}
<syntaxhighlight lang="applescript">"belvedere
dereference
elsewhere
erlenmeyer
evergreen
everywhere
exegete
freewheel
nevertheless
persevere
preference
referee
seventeen
seventeenth
telemeter
tennessee"</syntaxhighlight>
 
=={{header|Arturo}}==
Line 700 ⟶ 743:
tennessee
</pre>
 
=={{header|Delphi}}==
{{works with|Delphi|6.0}}
{{libheader|Classes,StdCtrls,SysUtils}}
This program makes extensive use of the standard Delphi TStringList component. It holds the dictionary, which is preloaded. It capture the E-Vowel-only information in a TStringList and uses it to display the words.
 
<syntaxhighlight lang="Delphi">
 
var Dict: TStringList; {List holds dictionary}
 
function HasEVowels(S: string): boolean;
{Test if string has exclusively E-Vowels and no "a,i,o,u"}
var I,ECount: integer;
begin
Result:=False;
ECount:=0;
for I:=1 to Length(S) do
begin
if S[I] in ['a','i','o','u'] then exit;
if S[I]='e' then Inc(ECount);
end;
Result:=ECount>3;
end;
 
 
procedure ShowEVowels(Memo: TMemo);
{Show words in dictionary that only}
{have e-vowels and have at least three}
var I: integer;
var SL: TStringList;
var S: string;
begin
SL:=TStringList.Create;
try
{Make list of words with least three E-vowels}
for I:=0 to Dict.Count-1 do
if HasEVowels(Dict[I]) then SL.Add(Dict[I]);
{Display all the words found}
S:='Found: '+IntToStr(SL.Count)+#$0D#$0A;
for I:=0 to SL.Count-1 do
begin
S:=S+Format('%-23s',[SL[I]]);
if (I mod 4)=3 then S:=S+#$0D#$0A;
end;
Memo.Text:=S;
finally SL.Free; end;
end;
 
 
 
initialization
{Create/load dictionary}
Dict:=TStringList.Create;
Dict.LoadFromFile('unixdict.txt');
Dict.Sorted:=True;
finalization
Dict.Free;
end.
 
</syntaxhighlight>
{{out}}
<pre>
Found: 16
belvedere dereference elsewhere erlenmeyer
evergreen everywhere exegete freewheel
nevertheless persevere preference referee
seventeen seventeenth telemeter tennessee
</pre>
 
 
=={{header|Draco}}==
Line 929 ⟶ 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,007 ⟶ 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,047 ⟶ 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,099 ⟶ 1,381:
belvedere dereference elsewhere erlenmeyer evergreen everywhere exegete freewheel
nevertheless persevere preference referee seventeen seventeenth telemeter tennessee
</pre>
=={{header|Kotlin}}==
<syntaxhighlight lang="kotlin">
import kotlin.io.path.Path
import kotlin.io.path.useLines
 
fun main() {
Path("unixdict.txt").useLines { lines ->
lines
.filter { line -> line.none { it in "aiou" } }
.filter { line -> line.count { it == 'e' } > 3 }
.forEach(::println)
}
}
</syntaxhighlight>
{{Output}}
<pre>
belvedere
dereference
elsewhere
erlenmeyer
evergreen
everywhere
exegete
freewheel
nevertheless
persevere
preference
referee
seventeen
seventeenth
telemeter
tennessee
</pre>
 
Line 1,602 ⟶ 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,729 ⟶ 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,476

edits