Find words whose first and last three letters are equal

From Rosetta Code
Revision as of 11:13, 10 February 2021 by rosettacode>Gerard Schildberger (removed forced TOC.)
Find words whose first and last three letters are equal is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.
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.


Other tasks related to string operations:
Metrics
Counting
Remove/replace
Anagrams/Derangements/shuffling
Find/Search/Determine
Formatting
Song lyrics/poems/Mad Libs/phrases
Tokenize
Sequences



Julia

See Alternade_words#Julia for the foreachword function. Yawn. <lang julia>matchfirstlast3(word, _) = length(word) > 5 && word[1:3] == word[end-2:end] ? word : ""

foreachword("unixdict.txt", matchfirstlast3, numcols=4)</lang>

Output:
Word source: unixdict.txt

antiperspirant calendrical    einstein       hotshot
murmur         oshkosh        tartar         testes

Phix

<lang Phix>function last3(string word) return length(word)>5 and word[1..3]=word[-3..-1] end function sequence last3s = filter(get_text("demo/unixdict.txt",GT_LF_STRIPPED),last3) printf(1,"%d words: %s\n",{length(last3s),join(shorten(last3s,"",3))})</lang>

Output:
8 words: antiperspirant calendrical einstein hotshot murmur oshkosh tartar testes

REXX

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. <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 /* " " " " " " */ 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. = # - 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</lang>
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

<lang ring> 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 </lang> Output:

working...
Words are:
1. antiperspirant
2. calendrical
3. einstein
4. hotshot
5. murmur
6. oshkosh
7. tartar
8. testes
done...