Find words whose first and last three letters are equal: Difference between revisions

m
(→‎{{header|Ruby}}: removed chomped temp array)
m (→‎{{header|Wren}}: Minor tidy)
 
(8 intermediate revisions by 6 users not shown)
Line 143:
found 8 words with the same first and last 3 characters
</pre>
 
=={{header|AppleScript}}==
<syntaxhighlight lang="applescript">on task()
script o
property wrds : words of ¬
(read file ((path to desktop as text) & "www.rosettacode.org:unixdict.txt") as «class utf8»)
property output : {}
end script
repeat with thisWord in o's wrds
if ((thisWord's length > 5) and (thisWord ends with thisWord's text 1 thru 3)) then ¬
set end of o's output to thisWord
end repeat
return o's output's contents
end task
 
task()</syntaxhighlight>
 
{{output}}
<syntaxhighlight lang="applescript">{"antiperspirant", "calendrical", "einstein", "hotshot", "murmur", "oshkosh", "tartar", "testes"}</syntaxhighlight>
 
=={{header|Arturo}}==
Line 206 ⟶ 227:
tartar
testes
</pre>
 
=={{header|C}}==
<syntaxhighlight lang="c">
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
 
/* "Using the dictionary unixdict.txt" */
#define DICTIONARY_FILE_NAME "unixdict.txt"
 
/* take into account the first and last 3 letters */
#define CHUNK_LENGTH 3
 
/* "The length of any word shown should be >5" */
#define MIN_WORD_LENGTH 6
 
 
char *create_word_buffer( const char *file_name, size_t *buffer_size );
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 &&
0==memcmp(buffer,buffer+l-CHUNK_LENGTH,CHUNK_LENGTH) ) {
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 wait_for_return( void ) {
puts( "\nPress Return to exit... " );
while( '\n'!=getchar() );
return 0;
}
</syntaxhighlight>
 
{{out}}
<pre>
antiperspirant
calendrical
einstein
hotshot
murmur
oshkosh
tartar
testes
 
Press Return to exit...
</pre>
 
Line 240 ⟶ 387:
7. tartar
8. testes
</pre>
 
=={{header|Delphi}}==
{{works with|Delphi|6.0}}
{{libheader|SysUtils,StdCtrls}}
Runs in 13 ms.
 
<syntaxhighlight lang="Delphi">
var Dict: TStringList; {List holds dictionary}
 
 
procedure FindFirst3Last3Match(Memo: TMemo);
{Find words where the first and last 3 characters are identical}
var I: integer;
var First3,Last3: string;
begin
for I:=0 to Dict.Count-1 do
if Length(Dict[I])>5 then
begin
First3:=Copy(Dict[I],1,3);
Last3:=Copy(Dict[I],Length(Dict[I])-2,3);
if First3=Last3 then
begin
Memo.Lines.Add(Dict[I]);
end
end;
end;
 
 
initialization
{Create/load dictionary}
Dict:=TStringList.Create;
Dict.LoadFromFile('unixdict.txt');
Dict.Sorted:=True;
finalization
Dict.Free;
end.
 
</syntaxhighlight>
{{out}}
<pre>
antiperspirant
calendrical
einstein
hotshot
murmur
oshkosh
tartar
testes
</pre>
 
Line 417 ⟶ 613:
=={{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 564 ⟶ 762:
tartar
testes</pre>
 
=={{header|Lambdatalk}}==
<syntaxhighlight lang="scheme">
The unixdict.txt has been loaded from https://web.archive.org and stored in a wiki page, lib_UNIXDICT, from which it can be required on demand in any other one.
 
{require lib_UNIXDICT}
 
{def look
{lambda {}
{S.replace \s by {div} in
{S.map {lambda {:w}
{if {and {> {W.length :w} 5}
{W.equal? {W.slice 0 3 :w}
{W.slice -3 {W.length :w} :w} }}
then :w
else}} {S.replace else by el.se in UNIXDICT}} }}}
-> look
{look}
->
antiperspirant
calendrical
einstein
hotshot
murmur
oshkosh
tartar
testes
 
Note: Due to a "weakness" of the {if then else} special form, where the else separator is not protected, every occurences of the string "else" in the input text must be replaced by some "broken word" before the search, for instance "el.se" and restored after if necessary.
</syntaxhighlight>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
Line 841 ⟶ 1,070:
8. testes
done...
</pre>
 
=={{header|RPL}}==
The only way to use unixdict.txt as input is to convert it into a list of 25104 strings. Luckily, emulators can handle such a big data structure in RAM.
{{works with|Halcyon Calc|4.2.7}}
≪ { }
<span style="color:red">1</span> <span style="color:green">UnixDict</span> SIZE '''FOR''' j
‘<span style="color:green">UnixDict</span>’ j GET
'''IF''' DUP SIZE <span style="color:red">5</span> ≤ '''THEN''' DROP '''ELSE'''
DUP <span style="color:red">1 3</span> SUB
OVER DUP SIZE DUP <span style="color:red">2</span> - SWAP SUB
'''IF''' == '''THEN''' + '''ELSE''' DROP '''END'''
'''END'''
'''NEXT'''
≫ ≫ ‘<span style="color:blue">SAME3</span>’ STO
{{out}}
<pre>
1: { "antiperspirant" "calendrical" "einstein" "hotshot" "murmur" "oshkosh" "tartar" "testes" }
</pre>
 
Line 1,007 ⟶ 1,254:
=={{header|Wren}}==
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascriptwren">import "io" for File
import "./fmt" for Fmt
 
var wordList = "unixdict.txt" // local copy
9,477

edits