State name puzzle: Difference between revisions

m
→‎{{header|REXX}}: changed/added comments and whitespace, changed indentations.
m (→‎{{header|Java}}: use getOrDefault)
m (→‎{{header|REXX}}: changed/added comments and whitespace, changed indentations.)
Line 1,493:
Code was added to the REXX program to remove dead-end words (state names) that can't possibly be part of
<br>a solution, in particular, words that contain a unique letter (among all the state names).
<lang rexx>/*REXX pgmprogram (state name puzzle) rearranges two state's names ──► two new states. */
!='Alabama, Alaska, Arizona, Arkansas, California, Colorado, Connecticut, Delaware, Florida, Georgia,',
'Hawaii, Idaho, Illinois, Indiana, Iowa, Kansas, Kentucky, Louisiana, Maine, Maryland, Massachusetts, ',
Line 1,500:
'South Dakota, Tennessee, Texas, Utah, Vermont, Virginia, Washington, West Virginia, Wisconsin, Wyoming'
 
parse arg xtra; !=! ',' xtra /*add optional (fictitious) names.*/
@abcU='ABCDEFGHIJKLMNOPQRSTUVWXYZ'; !=space(!) /*ABCs; !: the state list., no extra blanks*/
deads=0; dups=0; L.=0; !orig=!; z=0; @@.= /*initialize some varsREXX variables. */
 
do de=0 for 2; !=!orig; @.= /*use original state list for each. */
 
do states=0 until !=='' /*parse until the cows come home. */
parse var ! x ',' !; x=space(x) /*remove all blanks from state name.*/
if @.x\=='' then do /*was state was already specified? */
if de then iterate /*don't tell error if doing 2nd pass*/
dups=dups+1 /*bump the duplicate counter. */
say 'ignoring the 2nd naming of the state: ' x
iterate
end
@.x=x /*indicate this state name exists. */
y=space(x,0); upper y; yLen=length(y) /*get upper name with no spaces; Len*/
 
if de then do /*Is the 1stfirstt pass? Then process. */
do j=1 for yLen /*see if it's a dead─end state name.*/
_=substr(y,j,1) /* _: is some state name character.*/
if L._\==1 then iterate /*Count ¬ 1? Then state name is O.KOK.*/
say 'removing dead─end state [which has the letter ' _"]: " x
deads=deads+1 /*bump number of dead─ends states. */
iterate states /*go and process another state name.*/
end /*j*/
z=z+1 /*bump counter of the state names. */
#.z=y; ##.z=x /*assign state name; and original. */
end
else do k=1 for yLen /*inventorize state name's letters. */
_=substr(y,k,1); L._=L._+1 /*count each letter in state name. */
end /*k*/
 
end /*states*/
end /*de*/
 
say; do i=1 for z /*list state names in order given. */
say right(i,9) ##.i /*show the index number, state name.*/
end /*i*/
 
say; say z 'state name's(z) "are useable."
if dups \==0 then say dups 'duplicate of a state's(dups) 'ignored.'
if deads\==0 then say deads 'dead─end state's(deads) 'deleted.'
say
sols=0 /*number of solutions found (so far)*/
 
do j=1 for z /*◄───────────────────────────────────────────────────────────────┐ */
do j=1 for z /*◄─────────────────────────────────────────────────────┐ */
/*look for mix and match states. │ */
do k=j+1 to z /* ◄─── state K, state J ►───────┘ */
if #.j<<#.k then JK=#.j || #.k /*is the state in the proper order? */
else JK=#.k || #.j /*No, then /*use the new state name. */
 
do m=1 for z; if m==j | m==k then iterate /*no state overlaps are allowed. */
if verify(#.m,jk)\==0 then iterate /*is this state name even possible? */
nJK=elider(JK,#.m) /*a new JK, after eliding #.m characterschars.*/
 
do n=m+1 to z; if n==j | n==k then iterate /*no overlaps are allowed. */
if verify(#.n,nJK)\==0 then iterate /*is it possible? */
if elider(nJK,#.n)\=='' then iterate /*any leftovers letters? */
if #.m<<#.n then MN=#.m || #.n /*is it in the proper order?*/
else MN=#.n || #.m /*we found a new state name. */
if @@.JK.MN\=='' | @@.MN.JK\==''"" then iterate /*was it done before? */
say 'found: ' ##.j',' ##.k " ───► " ##.m',' ##.n
@@.JK.MN=1 /*indicate this solution as being found*/
sols=sols+1 /*bump the number of solutions found. */
end /*n*/
end /*m*/
end /*k*/
end /*j*/
say /*show a blank line for easier reading.*/
if sols==0 then sols='No' /*use mucher gooder (sic) Englishings. */
say sols 'solution's(sols) "found." /*display the number of solutions found*/
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*───────────────────────────────────ELIDER───────────────────────────────────*/
elider: parse arg hay,pins /*remove letters (pins) from haystack. */
do e=1 for length(pins); p=pos(substr(pins,e,1), hay)
 
do e if p==10 then iterate for length(pins); phay=posoverlay(substr(pins,e,1),' ',hay,p)
if p==0 then iterate end ; /*e*/ hay=overlay(' ',hay,p) /* [↑] remove a letter from haystack. */
return space(hay,0) end /*e*/ /*remove blanks from the haystack. /* [↑] remove a letter.*/
/*──────────────────────────────────────────────────────────────────────────────────────*/</lang>
return space(hay,0) /*remove blanks from hay*/
/*──────────────────────────────────S subroutine──────────────────────────────*/
s: if arg(1)==1 then return arg(3);return word(arg(2) 's',1) /*pluralizer.*/</lang>
'''output''' when using the default input:
<pre style="height:60ex">