Find words which first and last three letters are equals
- Task
Use the dictionary unixdict.txt
Find the words which first and last three letters are equals.
The length of any word shown should have a length > 5.
- Metrics
- Counting
- Word frequency
- Letter frequency
- Jewels and stones
- I before E except after C
- Bioinformatics/base count
- Count occurrences of a substring
- Remove/replace
- XXXX redacted
- Remove vowels from a string
- Strip block comments
- Strip comments from a string
- Strip a set of characters from a string
- Strip whitespace from a string -- top and tail
- Strip control codes and extended characters from a string
- Anagrams/Derangements/shuffling
- Word wheel
- ABC problem
- Anagrams
- Anagrams/Deranged anagrams
- Permutations/Derangements
- Superpermutation minimisation
- Sattolo cycle
- Knuth shuffle
- Ordered words
- Textonyms (using a phone text pad)
- Find/Search/Determine
- ABC words
- Odd words
- Semordnilap
- String matching
- Alternade words
- Changeable words
- String comparison
- Extract file extension
- Levenshtein distance
- Palindrome detection
- Compare a list of strings
- Longest common prefix
- Longest common suffix
- Longest common substring
- Find common directory path
- Words from neighbour ones
- Change e letters to i in words
- Non-continuous subsequences
- Longest common subsequence
- Longest palindromic substrings
- Longest increasing subsequence
- Words containing "the" substring
- Determine if a string is numeric
- Determine if a string is collapsible
- Determine if a string is squeezable
- Determine if a string has all unique characters
- Determine if a string has all the same characters
- Find words which contains all the vowels
- Find words which contains most consonants
- Find words which contains more than 3 vowels
- Find words which first and last three letters are equals
- Find words which odd letters are consonants and even letters are vowels or vice_versa
- Formatting
- String case
- Align columns
- Literals/String
- Repeat a string
- Brace expansion
- Brace expansion using ranges
- Reverse a string
- Phrase reversals
- Comma quibbling
- Special characters
- String concatenation
- Substring/Top and tail
- Commatizing numbers
- Reverse words in a string
- Suffixation of decimal numbers
- Long literals, with continuations
- Numerical and alphabetical suffixes
- Abbreviations, easy
- Abbreviations, simple
- Abbreviations, automatic
- Song lyrics/poems/Mad Libs/phrases
- 99 Bottles of Beer
- The Twelve Days of Christmas
- The Old lady swallowed a fly
- The Name Game (a song)
- Magic 8-ball
- Mad Libs
- Tokenize
- Word break problem
- Tokenize a string
- Tokenize a string with escaping
- Split a character string based on change of character
- Sequences
Contents
AWK[edit]
# 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))
END {
exit(0)
}
- Output:
antiperspirant calendrical einstein hotshot murmur oshkosh tartar testes
C++[edit]
#include <cstdlib>
#include <fstream>
#include <iostream>
int main(int argc, char** argv) {
const char* filename(argc < 2 ? "unixdict.txt" : argv[1]);
std::ifstream in(filename);
if (!in) {
std::cerr << "Cannot open file '" << filename << "'.\n";
return EXIT_FAILURE;
}
std::string word;
int n = 0;
while (getline(in, word)) {
const size_t len = word.size();
if (len > 5 && word.compare(0, 3, word, len - 3) == 0)
std::cout << ++n << ": " << word << '\n';
}
return EXIT_SUCCESS;
}
- Output:
1. antiperspirant 2. calendrical 3. einstein 4. hotshot 5. murmur 6. oshkosh 7. tartar 8. testes
F#[edit]
// 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")
- Output:
antiperspirant calendrical einstein hotshot murmur oshkosh tartar testes
Factor[edit]
Read entire file[edit]
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 and
.
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
- Output:
antiperspirant calendrical einstein hotshot murmur oshkosh tartar testes
Read file line by line[edit]
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.
USING: combinators.short-circuit io io.encodings.ascii io.files
kernel math sequences ;
"unixdict.txt" ascii [
[
readln dup
[
dup
{
[ length 5 > ]
[ [ 3 head-slice ] [ 3 tail-slice* ] bi = ]
} 1&&
[ print ] [ drop ] if
] when*
] loop
] with-file-reader
- Output:
As above.
Lazy file I/O[edit]
This version lazily reads the input file by treating a stream like a lazy list with the llines
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.
USING: io io.encodings.ascii io.files kernel lists lists.lazy
math sequences ;
"unixdict.txt" ascii <file-reader> llines
[ length 5 > ] lfilter
[ [ 3 head-slice ] [ 3 tail-slice* ] bi = ] lfilter
[ print ] leach
- Output:
As above.
FreeBASIC[edit]
#define NULL 0
type node
word as string*32 'enough space to store any word in the dictionary
nxt as node ptr
end type
function addword( tail as node ptr, word as string ) as node ptr
'allocates memory for a new node, links the previous tail to it,
'and returns the address of the new node
dim as node ptr newnode = allocate(sizeof(node))
tail->nxt = newnode
newnode->nxt = NULL
newnode->word = word
return newnode
end function
function length( word as string ) as uinteger
'necessary replacement for the built-in len function, which in this
'case would always return 32
for i as uinteger = 1 to 32
if asc(mid(word,i,1)) = 0 then return i-1
next i
return 999
end function
dim as string word
dim as node ptr tail = allocate( sizeof(node) )
dim as node ptr head = tail, curr = head, currj
dim as uinteger ln
tail->nxt = NULL
tail->word = "XXXXHEADER"
open "unixdict.txt" for input as #1
while true
line input #1, word
if word = "" then exit while
if length(word)>5 then tail = addword( tail, word )
wend
close #1
while curr->nxt <> NULL
word = curr->word
ln = length(word)
for i as uinteger = 1 to 3
if mid(word,i,1) <> mid(word,ln-3+i,1) then goto nextword
next i
print word
nextword:
curr = curr->nxt
wend
- Output:
antiperspirant calendrical einstein hotshot murmur oshkosh tartar testes
Go[edit]
package main
import (
"bytes"
"fmt"
"io/ioutil"
"log"
"unicode/utf8"
)
func main() {
wordList := "unixdict.txt"
b, err := ioutil.ReadFile(wordList)
if err != nil {
log.Fatal("Error reading file")
}
bwords := bytes.Fields(b)
count := 0
for _, bword := range bwords {
s := string(bword)
if utf8.RuneCountInString(s) > 5 && (s[0:3] == s[len(s)-3:]) {
count++
fmt.Printf("%d: %s\n", count, s)
}
}
}
- Output:
1: antiperspirant 2: calendrical 3: einstein 4: hotshot 5: murmur 6: oshkosh 7: tartar 8: testes
Julia[edit]
See Alternade_words#Julia for the foreachword function.
matchfirstlast3(word, _) = length(word) > 5 && word[1:3] == word[end-2:end] ? word : ""
foreachword("unixdict.txt", matchfirstlast3, numcols=4)
- Output:
Word source: unixdict.txt antiperspirant calendrical einstein hotshot murmur oshkosh tartar testes
Perl[edit]
as one-liner ..
// 20210212 Perl programming solution
perl -ne '/(?=^(.{3}).*\1$)^.{6,}$/&&print' unixdict.txt
# minor variation
perl -ne 's/(?=^(.{3}).*\1$)^.{6,}$/print/e' unixdict.txt
Phix[edit]
function flaste(string word)
return length(word)>5 and word[1..3]=word[-3..-1]
end function
sequence flastes = filter(get_text("demo/unixdict.txt",GT_LF_STRIPPED),flaste)
printf(1,"%d words: %s\n",{length(flastes),join(shorten(flastes,"",3))})
- Output:
8 words: antiperspirant calendrical einstein hotshot murmur oshkosh tartar testes
Raku[edit]
# 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,*) }
- Output:
antiperspirant calendrical einstein hotshot murmur oshkosh tartar testes
REXX[edit]
This REXX version doesn't care what order the words in the dictionary are in, nor does it care what
case (lower/upper/mixed) the words are in, the search for the words and vowels is caseless.
The program verifies that the first and last three characters are, indeed, letters.
It also allows the length (3) of the first and last number of letters to be specified, and also the minimum length of the
words to be searched on the command line (CL) as well as specifying the dictionary file identifier.
/*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 /* " " " " " " */
if many=='' | many=="," then many= 3 /* " " " " " " */
if iFID=='' | iFID=="," then iFID='unixdict.txt' /* " " " " " " */
do #=1 while lines(iFID)\==0 /*read each word in the file (word=X).*/
x= strip( linein( iFID) ) /*pick off a word from the input line. */
@.#= x /*save: the original case of the word.*/
end /*#*/
#= # - 1 /*adjust word count because of DO loop.*/
say copies('─', 30) # "words in the dictionary file: " iFID
finds= 0 /*word count which have matching end. */
/*process all the words that were found*/
do j=1 for #; $= @.j; upper $ /*obtain dictionary word; uppercase it.*/
if length($)<minL then iterate /*Word not long enough? Then skip it.*/
lhs= left($, many); rhs= right($, many) /*obtain the left & right side of word.*/
if \datatype(lhs || rhs, 'U') then iterate /*are the left and right side letters? */
if lhs \== rhs then iterate /*Left side match right side? No, skip*/
finds= finds + 1 /*bump count of only "e" vowels found. */
say right( left(@.j, 30), 40) /*indent original word for readability.*/
end /*j*/
/*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
- output when using the default inputs:
────────────────────────────── 25104 words in the dictionary file: unixdict.txt antiperspirant calendrical einstein hotshot murmur oshkosh tartar testes ────────────────────────────── 8 words found that the left 3 letters match the right letters which a word has a minimal length of 6
Ring[edit]
load "stdlib.ring"
cStr = read("unixdict.txt")
wordList = str2list(cStr)
num = 0
see "working..." + nl
see "Words are:" + nl
ln = len(wordList)
for n = ln to 1 step -1
if len(wordList[n]) < 6
del(wordList,n)
ok
next
for n = 1 to len(wordList)
if left(wordList[n],3) = right(wordList[n],3)
num = num + 1
see "" + num + ". " + wordList[n] + nl
ok
next
see "done..." + nl
Output:
working... Words are: 1. antiperspirant 2. calendrical 3. einstein 4. hotshot 5. murmur 6. oshkosh 7. tartar 8. testes done...
Swift[edit]
import Foundation
do {
try String(contentsOfFile: "unixdict.txt", encoding: String.Encoding.ascii)
.components(separatedBy: "\n")
.filter{$0.count > 5 && $0.prefix(3) == $0.suffix(3)}
.enumerated()
.forEach{print("\($0.0 + 1). \($0.1)")}
} catch {
print(error.localizedDescription)
}
- Output:
1. antiperspirant 2. calendrical 3. einstein 4. hotshot 5. murmur 6. oshkosh 7. tartar 8. testes
Wren[edit]
import "io" for File
import "/fmt" for Fmt
var wordList = "unixdict.txt" // local copy
var count = 0
File.read(wordList).trimEnd().split("\n").
where { |w|
return w.count > 5 && (w[0..2] == w[-3..-1])
}.
each { |w|
count = count + 1
Fmt.print("$d: $s", count, w)
}
- Output:
1: antiperspirant 2: calendrical 3: einstein 4: hotshot 5: murmur 6: oshkosh 7: tartar 8: testes