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

m
syntax highlighting fixup automation
m (syntax highlighting fixup automation)
Line 12:
 
=={{header|11l}}==
<langsyntaxhighlight lang="11l">L(word) File(‘unixdict.txt’).read().split("\n")
I word.len > 5 & word[0.<3] == word[(len)-3..]
print(word)</langsyntaxhighlight>
 
{{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.
<langsyntaxhighlight Actionlang="action!">BYTE FUNC IsValidWord(CHAR ARRAY word)
BYTE len
 
Line 61:
 
FindWords(fname)
RETURN</langsyntaxhighlight>
{{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}}==
<langsyntaxhighlight Adalang="ada">with Ada.Text_Io;
with Ada.Strings.Fixed;
 
Line 92:
end loop;
Close (File);
end Find_Three_Equals;</langsyntaxhighlight>
 
=={{header|ALGOL 68}}==
<langsyntaxhighlight lang="algol68"># find 6 (or more) character words with the same first and last 3 letters #
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</langsyntaxhighlight>
{{out}}
<pre>
Line 146:
=={{header|Arturo}}==
 
<langsyntaxhighlight lang="rebol">words: read.lines relative "unixdict.txt"
equalHeadTail?: function [w][
equal? first.n: 3 w last.n: 3 w
Line 156:
print word
]
]</langsyntaxhighlight>
 
{{out}}
Line 170:
 
=={{header|AutoHotkey}}==
<langsyntaxhighlight AutoHotkeylang="autohotkey">FileRead, db, % A_Desktop "\unixdict.txt"
for i, word in StrSplit(db, "`n", "`r")
if StrLen(word) < 6
Line 177:
result .= word "`n"
MsgBox, 262144, , % result
return</langsyntaxhighlight>
{{out}}
<pre>antiperspirant
Line 189:
 
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<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>
</lang>
{{out}}
<pre>
Line 209:
 
=={{header|C++}}==
<langsyntaxhighlight lang="cpp">#include <cstdlib>
#include <fstream>
#include <iostream>
Line 228:
}
return EXIT_SUCCESS;
}</langsyntaxhighlight>
 
{{out}}
Line 243:
 
=={{header|F_Sharp|F#}}==
<langsyntaxhighlight lang="fsharp">
// 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>
</lang>
{{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>.
<langsyntaxhighlight lang="factor">USING: io io.encodings.ascii io.files kernel math sequences ;
 
"unixdict.txt" ascii file-lines
[ length 5 > ] filter
[ [ 3 head-slice ] [ 3 tail-slice* ] bi = ] filter
[ print ] each</langsyntaxhighlight>
{{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.
<langsyntaxhighlight lang="factor">USING: combinators.short-circuit io io.encodings.ascii io.files
kernel math sequences ;
 
Line 297:
] when*
] loop
] with-file-reader</langsyntaxhighlight>
{{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.
<langsyntaxhighlight lang="factor">USING: io io.encodings.ascii io.files kernel lists lists.lazy
math sequences ;
 
Line 309:
[ length 5 > ] lfilter
[ [ 3 head-slice ] [ 3 tail-slice* ] bi = ] lfilter
[ print ] leach</langsyntaxhighlight>
{{out}}
As above.
Line 315:
=={{header|Forth}}==
{{works with|Gforth}}
<langsyntaxhighlight lang="forth">: first-last-three-equal { addr len -- ? }
len 5 <= if false exit then
addr 3 addr len 3 - + 3 compare 0= ;
Line 338:
 
main
bye</langsyntaxhighlight>
 
{{out}}
Line 353:
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">#define NULL 0
 
type node
Line 403:
nextword:
curr = curr->nxt
wend</langsyntaxhighlight>
{{out}}
<pre>
Line 416:
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import (
Line 441:
}
}
}</langsyntaxhighlight>
 
{{out}}
Line 456:
 
=={{header|J}}==
<langsyntaxhighlight Jlang="j"> >(#~ ((3&{. -: _3&{.)*5<#)@>) cutLF fread 'unixdict.txt'
antiperspirant
calendrical
Line 464:
oshkosh
tartar
testes</langsyntaxhighlight>
 
=={{header|jq}}==
{{works with|jq}}
'''Works with gojq, the Go implementation of jq'''
<langsyntaxhighlight lang="jq">select(length > 5 and .[:3] == .[-3:])</langsyntaxhighlight>
{{out}}
Invocation example: jq -rRM -f program.jq unixdict.txt
Line 485:
=={{header|Julia}}==
See Alternade_words#Julia for the foreachword function.
<langsyntaxhighlight lang="julia">matchfirstlast3(word, _) = length(word) > 5 && word[1:3] == word[end-2:end] ? word : ""
foreachword("unixdict.txt", matchfirstlast3, numcols=4)</langsyntaxhighlight>{{out}}
<pre>
Word source: unixdict.txt
Line 495:
 
=={{header|Ksh}}==
<langsyntaxhighlight lang="ksh">#!/bin/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}</langsyntaxhighlight>
 
{{out}}
Line 530:
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">dict = Once[Import["https://web.archive.org/web/20180611003215/http://www.puzzlers.org/pub/wordlists/unixdict.txt"]];
dict //= StringSplit[#, "\n"] &;
dict //= Select[StringLength /* GreaterThan[5]];
Select[dict, StringTake[#, 3] === StringTake[#, -3] &]</langsyntaxhighlight>
{{out}}
<pre>{"antiperspirant", "calendrical", "einstein", "hotshot", "murmur", "oshkosh", "tartar", "testes"}</pre>
 
=={{header|Nim}}==
<langsyntaxhighlight Nimlang="nim">for word in "unixdict.txt".lines:
if word.len > 5:
if word[0..2] == word[^3..^1]:
echo word</langsyntaxhighlight>
 
{{out}}
Line 555:
=={{header|Perl}}==
as one-liner ..
<langsyntaxhighlight lang="perl">// 20210212 Perl programming solution
 
perl -ne '/(?=^(.{3}).*\1$)^.{6,}$/&&print' unixdict.txt
Line 561:
# minor variation
 
perl -ne 's/(?=^(.{3}).*\1$)^.{6,}$/print/e' unixdict.txt</langsyntaxhighlight>
 
=={{header|Phix}}==
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<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>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 576:
 
=={{header|PL/I}}==
<langsyntaxhighlight lang="pli">firstAndLast3Equal: procedure options(main);
declare dict file;
open file(dict) title('unixdict.txt');
Line 589:
put skip list(word);
end;
end firstAndLast3Equal;</langsyntaxhighlight>
{{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">
<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>
</lang>
{{out}}
<pre>
Line 630:
=={{header|Quackery}}==
 
<langsyntaxhighlight Quackerylang="quackery"> [ [] swap ]'[ swap
witheach [
dup nested
Line 641:
filter [ size 5 > ]
filter [ 3 split -3 split nip = ]
witheach [ echo$ cr ]</langsyntaxhighlight>
 
{{out}}
Line 656:
 
=={{header|R}}==
<langsyntaxhighlight lang="rsplus">dict <- scan("https://web.archive.org/web/20180611003215/http://www.puzzlers.org/pub/wordlists/unixdict.txt", what = character())
dict[nchar(dict) > 5 & substr(dict, 1, 3) == substr(dict, nchar(dict) - 2, nchar(dict))]</langsyntaxhighlight>
 
=={{header|Racket}}==
 
<langsyntaxhighlight lang="racket">#lang racket
 
(define ((prefix-and-suffix-match? len) str)
Line 670:
 
(module+ main
(filter (prefix-and-suffix-match? 3) (file->lines "../../data/unixdict.txt")))</langsyntaxhighlight>
 
{{out}}
Line 677:
 
=={{header|Raku}}==
<syntaxhighlight lang="raku" perl6line># 20210210 Raku programming solution
 
my ( \L, \N, \IN ) = 5, 3, 'unixdict.txt';
 
for IN.IO.lines { .say if .chars > L and .substr(0,N) eq .substr(*-N,*) }
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 696:
 
=={{header|Red}}==
<langsyntaxhighlight lang="rebol">Red[]
 
foreach word read/lines %unixdict.txt [
Line 705:
print word
]
]</langsyntaxhighlight>
{{out}}
<pre>
Line 726:
It also allows the length ('''3''') of the first and last number of letters to be specified, &nbsp; 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.
<langsyntaxhighlight lang="rexx">/*REXX pgm finds words in an specified dict. which have the same 1st and last 3 letters.*/
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</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default inputs:}}
<pre style="font-size:89%">
Line 766:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
load "stdlib.ring"
 
Line 791:
 
see "done..." + nl
</syntaxhighlight>
</lang>
Output:
<pre>
Line 808:
 
=={{header|Ruby}}==
<langsyntaxhighlight lang="ruby">words = File.readlines("unixdict.txt").map(&:chomp)
puts words.select{|w| w.end_with?(w[0,3]) && w.size > 5}
</syntaxhighlight>
</lang>
{{out}}
<pre>antiperspirant
Line 822:
</pre>
=={{header|Sidef}}==
<langsyntaxhighlight lang="ruby">File("unixdict.txt").open_r.each {|word|
word.len > 5 || next
if (word.ends_with(word.first(3))) {
say word
}
}</langsyntaxhighlight>
{{out}}
<pre>
Line 841:
 
=={{header|Swift}}==
<langsyntaxhighlight lang="swift">import Foundation
 
do {
Line 851:
} catch {
print(error.localizedDescription)
}</langsyntaxhighlight>
 
{{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">
<lang vb>
with createobject("ADODB.Stream")
.charset ="UTF-8"
Line 902:
end if
next
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 927:
=={{header|Wren}}==
{{libheader|Wren-fmt}}
<langsyntaxhighlight lang="ecmascript">import "io" for File
import "/fmt" for Fmt
 
Line 939:
count = count + 1
Fmt.print("$d: $s", count, w)
}</langsyntaxhighlight>
 
{{out}}
Line 954:
 
=={{header|XPL0}}==
<langsyntaxhighlight XPL0lang="xpl0">string 0; \Use zero-terminated strings
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;
]</langsyntaxhighlight>
 
{{out}}
10,333

edits