How to resolve the algorithm Semordnilap step by step in the REXX programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Semordnilap step by step in the REXX programming language

Table of Contents

Problem Statement

A semordnilap is a word (or phrase) that spells a different word (or phrase) backward. "Semordnilap" is a word that itself is a semordnilap. Example: lager and regal

This task does not consider semordnilap phrases, only single words. Using only words from this list, report the total number of unique semordnilap pairs, and print 5 examples. Two matching semordnilaps, such as lager and regal, should be counted as one unique pair. (Note that the word "semordnilap" is not in the above dictionary.)

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Semordnilap step by step in the REXX programming language

Source code in the rexx programming language

/* REXX ***************************************************************
* 07.09.2012 Walter Pachl
**********************************************************************/
fid='unixdict.txt'                     /* the test dictionary        */
have.=''                               /* words encountered          */
pi=0                                   /* number of palindromes      */
Do li=1 By 1 While lines(fid)>0        /* as long there is input     */
  w=linein(fid)                        /* read a word                */
  If w>'' Then Do                      /* not a blank line           */
    r=reverse(w)                       /* reverse it                 */
    If have.r>'' Then Do               /* was already encountered    */
      pi=pi+1                          /* increment number of pal's  */
      If pi<=5 Then                    /* the first 5 ale listed     */
        Say have.r w
      End
    have.w=w                           /* remember the word          */
    End
  End
Say pi 'words in' fid 'have a palindrome' /* total number found      */


/*REXX program finds  N  semordnilap pairs using a specified dictionary  (UNIXDICT.TXT).*/
parse arg n iFID .                               /*obtain optional argument from the CL.*/
if    n=='' |    n=="," then    n= 5             /*Not specified?  Then use the default.*/
if iFID=='' | iFID=="," then iFID='UNIXDICT.TXT' /* "      "         "   "   "     "    */
#= 0                                             /*number of semordnilaps  (so far).    */
@.=                                              /*caseless non─duplicated dict. words. */
    do while lines(iFID)\==0;  _= linein(iFID);  u= space(_, 0);  upper u  /*get a word.*/
    if length(u)<2 | @.u\==''  then iterate      /*word can't be a unique semordnilap.  */
    r= reverse(u)                                /*obtain reverse of the dictionary word*/
    if @.r\==''  then do;   #= # + 1             /*found a semordnilap word; bump count.*/
                            if #<=n  then say right(@.r,  max(32, length(@.r) ) )','   u
                      end
    @.u= _                                       /*define reverse of the dictionary word*/
    end   /*while*/                              /*stick a fork in it,  we're all done. */
say
say "There're "     #     ' unique semordnilap pairs in the dictionary file: '    iFID


  

You may also check:How to resolve the algorithm Fractran step by step in the BQN programming language
You may also check:How to resolve the algorithm Catamorphism step by step in the Excel programming language
You may also check:How to resolve the algorithm Multi-base primes step by step in the REXX programming language
You may also check:How to resolve the algorithm Department numbers step by step in the Perl programming language
You may also check:How to resolve the algorithm Evaluate binomial coefficients step by step in the Python programming language