Find words which contains all the vowels: Difference between revisions

m
m (→‎{{header|FutureBasic}}: Another minor tweak to output)
m (→‎{{header|Wren}}: Minor tidy)
 
(14 intermediate revisions by 7 users not shown)
Line 443:
stupefaction
sulfonamide</pre>
 
=={{header|C}}==
<syntaxhighlight lang="c">
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
 
/* "Use the dictionary unixdict.txt" */
#define DICTIONARY_FILE_NAME "unixdict.txt"
 
/* "The length of any word shown should be >10" */
#define MIN_WORD_LENGTH 11
 
 
char *create_word_buffer( const char *file_name, size_t *buffer_size );
int verify_vowels( const char *word, size_t word_length );
int wait_for_return( void );
 
 
int main() {
size_t buffer_size = 0;
char *buffer = create_word_buffer( DICTIONARY_FILE_NAME, &buffer_size );
 
if( buffer ) {
FILE *f = fopen( DICTIONARY_FILE_NAME, "r" );
 
if( f ) {
while( fgets(buffer,buffer_size,f) ) {
size_t l = strlen( buffer );
if( '\n'==buffer[l-1] ) buffer[--l] = '\0';
 
if( l>=MIN_WORD_LENGTH && verify_vowels(buffer,l) ) {
printf( "%s\n", buffer );
}
}
 
fclose( f ); f = NULL; /* cleanup */
} else puts( "Couldn't open dictionary file." );
 
free( buffer ); buffer = NULL; /* cleanup */
} else puts( "Couldn't create word buffer." );
 
return wait_for_return();
}
 
 
/*==============================================================================
No need to verify any parameters in any of the following function - the caller
did his homeworks.
==============================================================================*/
 
 
size_t get_line_length( FILE *f ) {
size_t line_length = 0;
int c, ok;
 
do {
c = fgetc(f);
ok = '\n'!=c&&EOF!=c;
line_length += ok;
} while( ok );
 
return line_length;
}
 
 
size_t find_longest_line_in_file( const char *file_name ) {
size_t max_line_length = ((size_t)-1);
 
FILE *f = fopen( file_name, "r" );
 
if( f ) {
max_line_length = 0;
 
while( !feof(f) ) {
size_t line_length = get_line_length( f );
 
if( line_length>max_line_length )
max_line_length = line_length;
}
 
fclose( f ); f = NULL;
}
 
return max_line_length;
}
 
 
char *create_word_buffer( const char *file_name, size_t *buffer_size ) {
char *buffer = NULL;
 
size_t max_line_length = find_longest_line_in_file( file_name );
 
if( ((size_t)-1)!=max_line_length ) {
buffer = calloc( max_line_length+2, sizeof(*buffer) );
if( buffer ) *buffer_size = max_line_length+2;
}
 
return buffer;
}
 
 
int verify_vowels( const char *word, size_t word_length ) {
int vowel_instances[5] = {0};
size_t i;
 
for( i=0; i<word_length; ++i ) {
switch( word[i] ) {
case 'A': case 'a': vowel_instances[0]++; break;
case 'E': case 'e': vowel_instances[1]++; break;
case 'I': case 'i': vowel_instances[2]++; break;
case 'O': case 'o': vowel_instances[3]++; break;
case 'U': case 'u': vowel_instances[4]++; break;
default: ;
}
}
 
return 1==vowel_instances[0] &&
1==vowel_instances[1] &&
1==vowel_instances[2] &&
1==vowel_instances[3] &&
1==vowel_instances[4];
}
 
 
int wait_for_return( void ) {
puts( "\nPress Return to exit... " );
while( '\n'!=getchar() );
return 0;
}
</syntaxhighlight>
 
{{out}}
<pre>
ambidextrous
bimolecular
cauliflower
communicable
communicate
consanguine
consultative
countervail
exclusionary
grandiloquent
importunate
incommutable
incomputable
insupportable
loudspeaking
malnourished
mensuration
oneupmanship
pandemonium
permutation
perturbation
portraiture
praseodymium
stupefaction
sulfonamide
 
Press Return to exit...
</pre>
 
=={{header|C++}}==
Line 1,048 ⟶ 1,211:
=={{header|FutureBasic}}==
<syntaxhighlight lang="futurebasic">
 
#plist NSAppTransportSecurity @{NSAllowsArbitraryLoads:YES}
 
local fn Words as CFArrayRef
CFURLRef url = fn URLWithString( @"http://wiki.puzzlers.org/pub/wordlists/unixdict.txt" )
Line 1,191 ⟶ 1,357:
</pre>
 
=={{header|Haskell}}==
<syntaxhighlight lang="haskell">
condition :: String -> Bool
condition s = all (\vow -> count vow s == 1 ) "aeiou"
where
count :: Char -> String -> Int
count letter word = length $ filter ( == letter ) word
 
main :: IO ( )
main = do
theLines <- readFile "unixdict.txt"
let selected = filter condition $ words theLines
mapM_ putStrLn selected</syntaxhighlight>
 
{{out}}
(with my version of unixdict.txt)
<pre>
ambidextrous
aureomycin
bimolecular
cauliflower
colatitude
communicable
communicate
consanguine
consultative
countervail
dalhousie
denudation
deputation
dialogue
equivocal
euphorbia
euphoria
exclusionary
exhaustion
exhumation
exudation
exultation
facetious
fluoridate
gelatinous
grandiloquent
gregarious
houdaille
importunate
incommutable
incomputable
inoculate
insupportable
loudspeaking
malnourished
mendacious
mensuration
oneupmanship
pandemonium
permutation
persuasion
perturbation
pneumonia
portraiture
praseodymium
precarious
precaution
quasiorder
refutation
reputation
sequoia
stupefaction
sulfonamide
tambourine
tenacious
veracious
vexatious
</pre>
 
or, adding the restriction to words of more than 10 characters, and using Data.Set to identify words with a set of 5 unique vowel characters, which have no more than 5 vowel characters in total:
 
<syntaxhighlight lang="haskell">import Data.Set (Set, fromList, member, size)
 
---- WORDS OVER 10 CHARS WHICH CONTAIN EACH VOWEL ONCE ---
 
p :: String -> Bool
p = ((&&) . (10 <) . length) <*> eachVowelOnce
 
eachVowelOnce :: String -> Bool
eachVowelOnce w =
all (5 ==) $
[length, size . fromList] <*> [filter (`member` vowels) w]
 
vowels :: Set Char
vowels = fromList "aeiou"
 
--------------------------- TEST -------------------------
main :: IO ()
main =
readFile "unixdict.txt"
>>= (mapM_ putStrLn . filter p . lines)</syntaxhighlight>
{{Out}}
<pre>ambidextrous
bimolecular
cauliflower
communicable
communicate
consanguine
consultative
countervail
exclusionary
grandiloquent
importunate
incommutable
incomputable
insupportable
loudspeaking
malnourished
mensuration
oneupmanship
pandemonium
permutation
perturbation
portraiture
praseodymium
stupefaction
sulfonamide</pre>
 
=={{header|J}}==
Line 1,577 ⟶ 1,867:
sulfonamide
</pre>
 
=={{header|Quackery}}==
 
<syntaxhighlight lang="Quackery"> [ [] swap ]'[ swap
witheach
[ dup nested
unrot over do
iff [ dip join ]
else nip ]
drop ] is filter ( [ --> [ )
 
[ 2dup peek
1+ unrot poke ] is pink ( [ n --> [ )
 
[ ' [ 0 0 0 0 0 0 ]
swap witheach
[ lower
$ "aeiou" find
pink ]
-1 split drop
' [ 1 1 1 1 1 ] = ] is valid ( [ --> b )
 
$ "rosetta/unixdict.txt" sharefile drop nest$
filter [ size 10 > ]
filter valid
70 wrap$</syntaxhighlight>
 
{{out}}
 
<pre>ambidextrous bimolecular cauliflower communicable communicate
consanguine consultative countervail exclusionary grandiloquent
importunate incommutable incomputable insupportable loudspeaking
malnourished mensuration oneupmanship pandemonium permutation
perturbation portraiture praseodymium stupefaction sulfonamide</pre>
 
=={{header|R}}==
Line 1,836 ⟶ 2,160:
done...
 
</pre>
 
=={{header|RPL}}==
The only way to use <code>unixdict.txt</code> as input is to convert it into a list of 25104 strings. Fortunately, emulators can handle such a big data structure in RAM.
{{works with|Halcyon Calc|4.2.7}}
≪ { }
1 UnixDict SIZE '''FOR''' j
'UnixDict' j GET
'''IF''' DUP SIZE 10 < '''THEN''' DROP '''ELSE'''
{ 5 } 0 CON
1 3 PICK SIZE '''FOR''' j
OVER j DUP SUB
'''IF''' "AEIOUaeiou" SWAP POS '''THEN'''
LAST 1 - 5 MOD 1 +
DUP2 GET 1 + PUT
'''END'''
'''NEXT'''
'''IF''' [ 1 1 1 1 1 ] == '''THEN''' + '''ELSE''' DROP '''END'''
'''END NEXT'''
» '<span style="color:blue">AEIOU</span>' STO
 
{{out}}
<pre>
1: { "ambidextrous" "aureomycin" "bimolecular" "cauliflower" "colatitude" "communicable" "communicate" "consanguine" "consultative" "countervail" "denudation" "deputation" "exclusionary" "exhaustion" "exhumation" "exultation" "fluoridate" "gelatinous" "grandiloquent" "gregarious" "importunate" "incommutable" "incomputable" "insupportable" "loudspeaking" "malnourished" "mendacious" "mensuration" "oneupmanship" "pandemonium" "permutation" "persuasion" "perturbation" "portraiture" "praseodymium" "precarious" "precaution" "quasiorder" "refutation" "reputation" "stupefaction" "sulfonamide" "tambourine" }
</pre>
 
Line 1,870 ⟶ 2,218:
sulfonamide
</pre>
 
=={{header|sed}}==
<syntaxhighlight lang="sed">#!/bin/sed -f
 
/^.\{11\}/!d
/^[^a]*a[^a]*$/!d
/^[^e]*e[^e]*$/!d
/^[^i]*i[^i]*$/!d
/^[^o]*o[^o]*$/!d
/^[^u]*u[^u]*$/!d</syntaxhighlight>
 
=={{header|Swift}}==
Line 1,944 ⟶ 2,302:
=={{header|Wren}}==
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascriptwren">import "io" for File
import "./fmt" for Fmt
 
var wordList = "unixdict.txt" // local copy
9,483

edits