Word ladder: Difference between revisions

m
→‎{{header|REXX}}: added words to the REXX section header.
(→‎{{header|REXX}}: added the computer programming language REXX.)
m (→‎{{header|REXX}}: added words to the REXX section header.)
Line 276:
 
It also assumes that the dictionary file is in mixed case as well as the words entered on the CL.
 
To treat the dictionary and input words as caseless,   all words are translated to lowercase.
 
Programming note:     this REXX program uses the   '''lower'''   BIF   which Regina has).
Line 292 ⟶ 294:
L= length(base) /*length of the BASE (in characters). */
if L<2 then call err 'base word is too small or missing' /*oops, too small*/
if length(targ)\==L then call errmsg , "target word isn't the same length as the base word"
call letters /*assign letters, faster than SUBSTR. */
#= 0 /*# of words whose length matches BASE.*/
@.= /*default value of any dictionary word.*/
do recs=0 while lines(iFID)\==0 /*read each word in the file (word=X).*/
x= lower(strip( linein( iFID) ) ) /*pick off a word from the input line. */
if length(x)\==L then iterate /*Word not correct length? Then skip. */
#= # + 1; @.x= 1 /*bump number of# words with length L.; semaphore*/
@.x= 1 end /*recs*/ /*store the[↑] word in a semaphore arrayname is uppercased. */
end /*recs*/ /* [↑] semaphore name is uppercased. */
!.= 0
say copies('─', 30) recs "words in the dictionary file: " iFID
Line 309 ⟶ 310:
rung= targ
$= base
do f=1 for abcsm; call look; if result\=='' then leave /*Found? Quit.*/
end /*f*/
say
if f>abcsm then do;call msg say 'no word ladder solution possible for ' base " ──► " targ; exit 13targ
end
 
do f-2; $= base; !.= 0 /*process all the rungs that were found*/
do forever; do forever; call look; if result\=='' then leave /*Found? Quit.*/
end /*forever*/
end /*f-2*/
_=call show words(rung)
call show
exit 0 /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
errmsg: say; if arg()==2 then say '***error*** ' arg(12); else say arg(1); say; exit 13
show: say 'a solution: ' base; do j=1 to for _arg(1); say left('', 12) word(rung, j); end; return
letters: do abcsm=1 for length(abc); a.abcsm= substr(abc, abcsm, 1); end; return
/*──────────────────────────────────────────────────────────────────────────────────────*/
look: procedure expose @. !. a. $ abc rung base L rung targ search; rungs= word(rung, 1)
$$=; rungwrung#= words(rungs)
do i=1 for words($); y= word($, i); !.y= 1
do k=1 for L
do n=1 for 26; z= overlay(a.n, y, k) /*change a letter*/
Line 335 ⟶ 334:
if !.z then iterate /* " " a repeat? " " " */
if z==rungs then rung= y rung /*prepend a word to the rung list. */
if z==rungs & rungwrung#>1 then return z /*short─circuit. */
if z==targ then return z
$$= $$ z /*append a possible ladder word to $$*/
Line 357 ⟶ 356:
<pre>
────────────────────────────── 25104 words in the dictionary file: unixdict.txt
────────────────────────────── 2187 words in the dictionary file of length: 34
────────────────────────────── base word is: girl
────────────────────────────── target word is: lady
Line 373 ⟶ 372:
<pre>
────────────────────────────── 25104 words in the dictionary file: unixdict.txt
────────────────────────────── 2187 words in the dictionary file of length: 34
────────────────────────────── base word is: john
────────────────────────────── target word is: jane
Line 387 ⟶ 386:
<pre>
────────────────────────────── 25104 words in the dictionary file: unixdict.txt
────────────────────────────── 3161 words in the dictionary file of length: 35
────────────────────────────── base word is: child
────────────────────────────── target word is: adult