Find words whose first and last three letters are equal: Difference between revisions
Find words whose first and last three letters are equal (view source)
Revision as of 13:06, 27 August 2022
, 1 year agosyntax highlighting fixup automation
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
|||
Line 12:
=={{header|11l}}==
<
I word.len > 5 & word[0.<3] == word[(len)-3..]
print(word)</
{{out}}
Line 30:
=={{header|Action!}}==
In the following solution the input file [https://gitlab.com/amarok8bit/action-rosetta-code/-/blob/master/source/unixdict.txt unixdict.txt] is loaded from H6 drive. Altirra emulator automatically converts CR/LF character from ASCII into 155 character in ATASCII charset used by Atari 8-bit computer when one from H6-H10 hard drive under DOS 2.5 is used.
<
BYTE len
Line 61:
FindWords(fname)
RETURN</
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Find_words_whose_first_and_last_three_letters_are_equal.png Screenshot from Atari 8-bit computer]
Line 69:
=={{header|Ada}}==
<
with Ada.Strings.Fixed;
Line 92:
end loop;
Close (File);
end Find_Three_Equals;</
=={{header|ALGOL 68}}==
<
IF FILE input file;
STRING file name = "unixdict.txt";
Line 136:
print( ( newline, "found ", whole( count, 0 ), " words with the same first and last 3 characters", newline ) );
close( input file )
FI</
{{out}}
<pre>
Line 146:
=={{header|Arturo}}==
<
equalHeadTail?: function [w][
equal? first.n: 3 w last.n: 3 w
Line 156:
print word
]
]</
{{out}}
Line 170:
=={{header|AutoHotkey}}==
<
for i, word in StrSplit(db, "`n", "`r")
if StrLen(word) < 6
Line 177:
result .= word "`n"
MsgBox, 262144, , % result
return</
{{out}}
<pre>antiperspirant
Line 189:
=={{header|AWK}}==
<syntaxhighlight lang="awk">
# syntax: GAWK -f FIND_WORDS_WHICH_FIRST_AND_LAST_THREE_LETTERS_ARE_EQUALS.AWK unixdict.txt
(length($0) >= 6 && substr($0,1,3) == substr($0,length($0)-2,3))
Line 195:
exit(0)
}
</syntaxhighlight>
{{out}}
<pre>
Line 209:
=={{header|C++}}==
<
#include <fstream>
#include <iostream>
Line 228:
}
return EXIT_SUCCESS;
}</
{{out}}
Line 243:
=={{header|F_Sharp|F#}}==
<
// First and last three letters are equal. Nigel Galloway: February 18th., 2021
let fN g=if String.length g<6 then false else g.[..2]=g.[g.Length-3..]
seq{use n=System.IO.File.OpenText("unixdict.txt") in while not n.EndOfStream do yield n.ReadLine()}|>Seq.filter fN|>Seq.iter(printfn "%s")
</syntaxhighlight>
{{out}}
<pre>
Line 262:
===Read entire file===
This version reads the entire dictionary file into memory and filters it. This is the fastest version by far. Factor is optimized for making multiple passes over data; it actually takes longer if we combine the two filters into one, either with short-circuiting or non-short-circuiting <code>and</code>.
<
"unixdict.txt" ascii file-lines
[ length 5 > ] filter
[ [ 3 head-slice ] [ 3 tail-slice* ] bi = ] filter
[ print ] each</
{{out}}
<pre>
Line 282:
===Read file line by line===
This version reads the dictionary file line by line and prints out words that fit the criteria. This ends up being a bit more imperative and deeply nested, but unlike the version above, we only load one word at a time, saving quite a bit of memory.
<
kernel math sequences ;
Line 297:
] when*
] loop
] with-file-reader</
{{out}}
As above.
Line 303:
===Lazy file I/O===
This version lazily reads the input file by treating a stream like a lazy list with the <code>llines</code> word. This allows us the nice style of the first example with the memory benefits of the second example. Unlike in the first example, combining the filters would buy us some time here, as lazy lists aren't as efficient as sequences.
<
math sequences ;
Line 309:
[ length 5 > ] lfilter
[ [ 3 head-slice ] [ 3 tail-slice* ] bi = ] lfilter
[ print ] leach</
{{out}}
As above.
Line 315:
=={{header|Forth}}==
{{works with|Gforth}}
<
len 5 <= if false exit then
addr 3 addr len 3 - + 3 compare 0= ;
Line 338:
main
bye</
{{out}}
Line 353:
=={{header|FreeBASIC}}==
<
type node
Line 403:
nextword:
curr = curr->nxt
wend</
{{out}}
<pre>
Line 416:
=={{header|Go}}==
<
import (
Line 441:
}
}
}</
{{out}}
Line 456:
=={{header|J}}==
<
antiperspirant
calendrical
Line 464:
oshkosh
tartar
testes</
=={{header|jq}}==
{{works with|jq}}
'''Works with gojq, the Go implementation of jq'''
<
{{out}}
Invocation example: jq -rRM -f program.jq unixdict.txt
Line 485:
=={{header|Julia}}==
See Alternade_words#Julia for the foreachword function.
<
foreachword("unixdict.txt", matchfirstlast3, numcols=4)</
<pre>
Word source: unixdict.txt
Line 495:
=={{header|Ksh}}==
<
# Find list of words (> 5 chars) where 1st 3 and last 3 letters are the same
Line 517:
[[ ${first} == ${last} ]] && print ${word}
done < ${dict}</
{{out}}
Line 530:
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<
dict //= StringSplit[#, "\n"] &;
dict //= Select[StringLength /* GreaterThan[5]];
Select[dict, StringTake[#, 3] === StringTake[#, -3] &]</
{{out}}
<pre>{"antiperspirant", "calendrical", "einstein", "hotshot", "murmur", "oshkosh", "tartar", "testes"}</pre>
=={{header|Nim}}==
<
if word.len > 5:
if word[0..2] == word[^3..^1]:
echo word</
{{out}}
Line 555:
=={{header|Perl}}==
as one-liner ..
<
perl -ne '/(?=^(.{3}).*\1$)^.{6,}$/&&print' unixdict.txt
Line 561:
# minor variation
perl -ne 's/(?=^(.{3}).*\1$)^.{6,}$/print/e' unixdict.txt</
=={{header|Phix}}==
<!--<
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">flaste</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">word</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">return</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">word</span><span style="color: #0000FF;">)></span><span style="color: #000000;">5</span> <span style="color: #008080;">and</span> <span style="color: #000000;">word</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">..</span><span style="color: #000000;">3</span><span style="color: #0000FF;">]=</span><span style="color: #000000;">word</span><span style="color: #0000FF;">[-</span><span style="color: #000000;">3</span><span style="color: #0000FF;">..-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span> <span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">flastes</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">filter</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">unix_dict</span><span style="color: #0000FF;">(),</span><span style="color: #000000;">flaste</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%d words: %s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">flastes</span><span style="color: #0000FF;">),</span><span style="color: #7060A8;">join</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">shorten</span><span style="color: #0000FF;">(</span><span style="color: #000000;">flastes</span><span style="color: #0000FF;">,</span><span style="color: #008000;">""</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">))})</span>
<!--</
{{out}}
<pre>
Line 576:
=={{header|PL/I}}==
<
declare dict file;
open file(dict) title('unixdict.txt');
Line 589:
put skip list(word);
end;
end firstAndLast3Equal;</
{{out}}
<pre>antiperspirant
Line 602:
=={{header|Python}}==
Tested on Python 3+, the file download will work only if the link is still active. It is possible that you may be able to fetch the file in your browser but download via code may still fail. Check whether you are connected to a VPN, it works on open networks
<syntaxhighlight lang="python">
import urllib.request
urllib.request.urlretrieve("http://wiki.puzzlers.org/pub/wordlists/unixdict.txt", "unixdict.txt")
Line 615:
if len(word)>5 and word[:3].lower()==word[-3:].lower():
print(word)
</syntaxhighlight>
{{out}}
<pre>
Line 630:
=={{header|Quackery}}==
<
witheach [
dup nested
Line 641:
filter [ size 5 > ]
filter [ 3 split -3 split nip = ]
witheach [ echo$ cr ]</
{{out}}
Line 656:
=={{header|R}}==
<
dict[nchar(dict) > 5 & substr(dict, 1, 3) == substr(dict, nchar(dict) - 2, nchar(dict))]</
=={{header|Racket}}==
<
(define ((prefix-and-suffix-match? len) str)
Line 670:
(module+ main
(filter (prefix-and-suffix-match? 3) (file->lines "../../data/unixdict.txt")))</
{{out}}
Line 677:
=={{header|Raku}}==
<syntaxhighlight lang="raku"
my ( \L, \N, \IN ) = 5, 3, 'unixdict.txt';
for IN.IO.lines { .say if .chars > L and .substr(0,N) eq .substr(*-N,*) }
</syntaxhighlight>
{{out}}
<pre>
Line 696:
=={{header|Red}}==
<
foreach word read/lines %unixdict.txt [
Line 705:
print word
]
]</
{{out}}
<pre>
Line 726:
It also allows the length ('''3''') of the first and last number of letters to be specified, and also the minimum length of the
<br>words to be searched on the command line (CL) as well as specifying the dictionary file identifier.
<
parse arg minL many iFID . /*obtain optional arguments from the CL*/
if minL=='' | minL=="," then minL= 6 /* " " " " " " */
Line 750:
/*stick a fork in it, we're all done. */
say copies('─', 30) finds " words found that the left " many ' letters match the' ,
"right letters which a word has a minimal length of " minL</
{{out|output|text= when using the default inputs:}}
<pre style="font-size:89%">
Line 766:
=={{header|Ring}}==
<
load "stdlib.ring"
Line 791:
see "done..." + nl
</syntaxhighlight>
Output:
<pre>
Line 808:
=={{header|Ruby}}==
<
puts words.select{|w| w.end_with?(w[0,3]) && w.size > 5}
</syntaxhighlight>
{{out}}
<pre>antiperspirant
Line 822:
</pre>
=={{header|Sidef}}==
<
word.len > 5 || next
if (word.ends_with(word.first(3))) {
say word
}
}</
{{out}}
<pre>
Line 841:
=={{header|Swift}}==
<
do {
Line 851:
} catch {
print(error.localizedDescription)
}</
{{out}}
Line 867:
=={{header|VBScript}}==
After building a program checking for the 3 letters in any order, I found people just checked the same trigraph at start and end. I modified my program so it puts an asterisk after the words in the "standard" answer. Run the ssript with CScript.
<syntaxhighlight lang="vb">
with createobject("ADODB.Stream")
.charset ="UTF-8"
Line 902:
end if
next
</syntaxhighlight>
{{out}}
<pre>
Line 927:
=={{header|Wren}}==
{{libheader|Wren-fmt}}
<
import "/fmt" for Fmt
Line 939:
count = count + 1
Fmt.print("$d: $s", count, w)
}</
{{out}}
Line 954:
=={{header|XPL0}}==
<
int I, Ch, Len;
char Word(100); \(longest word in unixdict.txt is 22 chars)
Line 974:
[Text(0, Word); CrLf(0)];
until Ch = EOF;
]</
{{out}}
|