Word ladder: Difference between revisions

Content added Content deleted
m (→‎{{header|REXXt}}: added a REXX stub.)
(→‎{{header|REXX}}: added the computer programming language REXX.)
Line 273: Line 273:


=={{header|REXX}}==
=={{header|REXX}}==
This REXX entry does a little more error checking.
<lang rexx></lang>

output
It also assumes that the dictionary file is in mixed case as well as the words entered on the CL.

Programming note: &nbsp; &nbsp; this REXX program uses the &nbsp; '''lower''' &nbsp; BIF &nbsp; which Regina has).
<br>If your REXX doesn't support that BIF, &nbsp; here is an equivalent function:
<lang rexx>lower: procedure; parse arg a; @= 'abcdefghijklmnopqrstuvwxyz'; @u= @; upper @u
return translate(a, @, @u)</lang>
<lang rexx>/*REXX program finds words (within an identified dict.) to solve a word ladder puzzle.*/
parse arg base targ iFID . /*obtain optional arguments from the CL*/
if base=='' | base=="," then base= 'boy' /*Not specified? Then use the default.*/
if targ=='' | targ=="," then targ= 'man' /* " " " " " " */
if iFID=='' | iFID=="," then iFID='unixdict.txt' /* " " " " " " */
abc= 'abcdefghijklmnopqrstuvwxyz' /*the lowercase (Latin) alphabet. */
abcU= abc; upper abcU /* " uppercase " " */
base= lower(base); targ= lower(targ) /*lowercase the BASE and also the TARG.*/
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 err "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 /*bump number of words with length L. */
@.x= 1 /*store the word in a semaphore array. */
end /*recs*/ /* [↑] semaphore name is uppercased. */
!.= 0
say copies('─', 30) recs "words in the dictionary file: " iFID
say copies('─', 30) # "words in the dictionary file of length: " L
say copies('─', 30) ' base word is: ' base
say copies('─', 30) 'target word is: ' targ
rung= targ
$= base
do f=1 for abcs; call look; if result\=='' then leave /*Found? Quit.*/
end /*f*/
say
if f>abcs then do; say 'no word ladder solution possible for ' base " ──► " targ; exit 13
end

do f-2; $= base; !.= 0 /*process all the rungs that were found*/
do forever; call look; if result\=='' then leave /*Found? Quit.*/
end /*forever*/
end /*f-2*/
_= words(rung)
call show
exit 0 /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
err: say; say '***error***' arg(1); say; exit 13
show: say 'a solution: ' base; do j=1 for _; say left('', 12) word(rung, j); end; return
letters: do abcs=1 for length(abc); a.abcs= substr(abc, abcs, 1); end; return
/*──────────────────────────────────────────────────────────────────────────────────────*/
look: procedure expose @. !. a. $ abc rung base L targ search; rungs= word(rung, 1)
$$=; rungw= 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*/
if @.z=='' then iterate /*Is this not a word? Then skip it. */
if !.z then iterate /* " " a repeat? " " " */
if z==rungs then rung= y rung /*prepend a word to the rung list. */
if z==rungs & rungw>1 then return z /*short─circuit. */
if z==targ then return z
$$= $$ z /*append a possible ladder word to $$*/
end /*n*/
end /*k*/
end /*i*/
$= $$; return ''</lang>
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
────────────────────────────── 25104 words in the dictionary file: unixdict.txt
────────────────────────────── 796 words in the dictionary file of length: 3
────────────────────────────── base word is: boy
────────────────────────────── target word is: man

a solution: boy
bay
may
man
</pre>
{{out|output|text=&nbsp; when using the inputs of: &nbsp; &nbsp; <tt> girl &nbsp; lady </tt>}}
<pre>
────────────────────────────── 25104 words in the dictionary file: unixdict.txt
────────────────────────────── 2187 words in the dictionary file of length: 3
────────────────────────────── base word is: girl
────────────────────────────── target word is: lady

a solution: girl
gill
gall
gale
gaze
laze
lazy
lady
</pre>
{{out|output|text=&nbsp; when using the inputs of: &nbsp; &nbsp; <tt> john &nbsp; jane </tt>}}
<pre>
────────────────────────────── 25104 words in the dictionary file: unixdict.txt
────────────────────────────── 2187 words in the dictionary file of length: 3
────────────────────────────── base word is: john
────────────────────────────── target word is: jane

a solution: john
cohn
conn
cone
cane
jane
</pre>
{{out|output|text=&nbsp; when using the inputs of: &nbsp; &nbsp; <tt> child &nbsp; adult </tt>}}
<pre>
<pre>
────────────────────────────── 25104 words in the dictionary file: unixdict.txt
────────────────────────────── 3161 words in the dictionary file of length: 3
────────────────────────────── base word is: child
────────────────────────────── target word is: adult


no word ladder solution possible for child ──► adult
</pre>
</pre>