Semordnilap: Difference between revisions

Content added Content deleted
m (added whitespace, added section headers.)
m (→‎version 2: added/changed comments and whitespace, changed indentations.)
Line 1,996: Line 1,996:
This REXX version makes use of sparse (stemmed) arrays.
This REXX version makes use of sparse (stemmed) arrays.
<br><br>The dictionary file wasn't assumed to be in any particular case (upper/lower/mixed).
<br><br>The dictionary file wasn't assumed to be in any particular case (upper/lower/mixed).
<br>For instance, '''DNA''' &amp; '''and''' would be considered palindromes.
<br>For instance, &nbsp; '''DNA''' &nbsp; <big> &amp; </big> &nbsp; '''and''' &nbsp; would be considered palindromes.
<br>The UNIXDICT dictionary specified to be used ''is'' all lowercase, however, but the REXX
<br>The UNIXDICT dictionary specified to be used ''is'' all lowercase, however, but the REXX
<br>program assumes that the words may be in any ''case'' &nbsp; (upper, lower, mixed).
<br>program assumes that the words may be in any ''case'' &nbsp; (upper, lower, mixed).
Line 2,004: Line 2,004:
<br>The palindrome pairs are shown with a comma delimiter in case there're phrases (words with imbedded blanks like Sing Sing).
<br>The palindrome pairs are shown with a comma delimiter in case there're phrases (words with imbedded blanks like Sing Sing).
<br>The (first five) palindrome pairs are shown as they are specified (respective to case) in the dictionary.
<br>The (first five) palindrome pairs are shown as they are specified (respective to case) in the dictionary.
<lang rexx>/*REXX program finds palindrome pairs using a dictionary (UNIXDICT.TXT).*/
<lang rexx>/*REXX program finds palindrome pairs in a dictionary (the default is UNIXDICT.TXT). */
#=0 /*# palindromes (so far)*/
#=0 /*number palindromes (so far).*/
parse arg iFID .; if iFID=='' then iFID='UNIXDICT.TXT' /*use default?*/
parse arg iFID .; if iFID=='' then iFID='UNIXDICT.TXT' /*Not specified? Use default.*/
@.= /*caseless no-duped word*/
@.= /*uppercase no─duplicated word*/
do while lines(iFID)\==0; _=space(linein(iFID),0); parse upper var _ u
do while lines(iFID)\==0; _=space(linein(iFID),0) /*read a word from dictionary.*/
if length(_)<2 | @.u\=='' then iterate /*can't be a unique pal.*/
parse upper var _ u /*obtain an uppercase version.*/
r=reverse(u) /*get the reverse of U. */
if length(_)<2 | @.u\=='' then iterate /*can't be a unique palindrome*/
if @.r\=='' then do; #=#+1 /*found palindrome pair?*/
r=reverse(u) /*get the reverse of the word.*/
if #<6 then say @.r',' _ /*only list first 5 pals*/
if @.r\=='' then do; #=#+1 /*find a palindrome pair ? */
end /* [↑] bump count, show*/
if #<6 then say @.r',' _ /*just show 1st 5 palindromes.*/
@.u=_ /*define palindromic pal*/
end /* [↑] bump palindrome count.*/
end /*while*/ /* [↑] read dictionary.*/
@.u=_ /*define a unique palindrome. */
end /*while*/ /* [↑] read the dictionary. */
say
say
say "There're" # 'unique palindrome pairs in the dictionary file: ' iFID
say "There're " # ' unique palindrome pairs in the dictionary file: ' iFID
/*stick a fork in it, we're done.*/</lang>
/*stick a fork in it, we're done. */</lang>
'''output''' when using the default dictionary as the input:
'''output''' &nbsp; when using the default dictionary as the input:
<pre>
<pre>
ac, ca
ac, ca