Jump to content

ABC problem: Difference between revisions

→‎{{header|REXX}}: added the REXX language. -- ~~~~
(Simpler D entry)
(→‎{{header|REXX}}: added the REXX language. -- ~~~~)
Line 166:
Can we spell 'SQUAD'? True
Can we spell 'conFUsE'? True</pre>
 
=={{header|REXX}}==
<lang rexx>/*REXX pgm tests if a word(s) can be created from a pool of toy blocks. */
blocks = 'bo xk dq cp na gt re tg qd fs jw hu vi an ob er fs ly pc zm'
list = 'A baRk bOOk trEat coMMon squaD conFuse' /*can be in any case. */
do k=0 to words(list) /*traipse through list. */
if k==0 then call can_make_word '' /*perform a NULL test.*/
else call can_make_word word(list,k) /*a vanilla test.*/
end /*k*/
exit /*stick a fork in it, we're done.*/
/*──────────────────────────────────CAN_MAKE_WORD subroutine────────────*/
can_make_word: procedure expose blocks; arg x . /*X: word to be built. */
z=' ' blocks " "; upper z /*pad pool, uppercase it*/
try=0; OK=0; L=length(x) /*set some REXX vars. */
 
do n=1 for L; y=substr(x,max(1,n),1) /*find particular letter*/
if try//2 then p= pos(y,z) /*try to find letter by */
else p=lastpos(y,z) /*one method or another.*/
 
if p==0 then do; try=try+1 /*Not found? Try again.*/
n=n-2; iterate /*back up (to previous).*/
end
if pos(' 'y,z)\==0 then q=' 'y /*try to locate Y≈ */
else q=y' ' /* " " ≈Y */
parse var z a (q) b /*split it up into two. */
z=a b
do k=1 for words(z); _=word(z,k) /*scrub the block pool. */
if length(_)==1 then z=delword(z,k,1) /*is block 1 char?*/
end /*k*/ /* [↑] elide any 1char.*/
OK= n==L /*a flag: built or not.*/
end /*n*/
 
if x=='' then x="(null)" /*express a NULL better.*/
say right(x,20) right(word("can't can",OK+1),6) 'be built.'
return OK /*also, return flag. */</lang>
'''output'''
<pre>
(null) can't be built.
A can be built.
BARK can be built.
BOOK can't be built.
TREAT can be built.
COMMON can't be built.
SQUAD can be built.
CONFUSE can be built.
</pre>
Cookies help us deliver our services. By using our services, you agree to our use of cookies.