Find words which contains all the vowels: Difference between revisions
Find words which contains all the vowels (view source)
Revision as of 12:15, 5 December 2023
, 6 months ago→{{header|Wren}}: Minor tidy
(→{{header|Haskell}}: Added a variant, expressed in terms of Data.Set, which includes the restriction to 10 chars) |
m (→{{header|Wren}}: Minor tidy) |
||
(9 intermediate revisions by 4 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,270 ⟶ 1,436:
<syntaxhighlight lang="haskell">import Data.Set (Set, fromList, member, size)
---- WORDS OVER 10 CHARS WHICH CONTAIN EACH VOWEL ONCE ---
Line 1,280 ⟶ 1,443:
eachVowelOnce :: String -> Bool
eachVowelOnce w =
[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
Line 1,716 ⟶ 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 2,117 ⟶ 2,302:
=={{header|Wren}}==
{{libheader|Wren-fmt}}
<syntaxhighlight lang="
import "./fmt" for Fmt
var wordList = "unixdict.txt" // local copy
|