State name puzzle: Difference between revisions

m
→‎{{header|REXX}}: added/changed comments and whitespace, added a subroutine..
m (→‎{{header|REXX}}: added/changed comments and whitespace, added a subroutine..)
Line 1,559:
'New York, North Carolina, North Dakota, Ohio, Oklahoma, Oregon, Pennsylvania, Rhode Island, South Carolina,',
'South Dakota, Tennessee, Texas, Utah, Vermont, Virginia, Washington, West Virginia, Wisconsin, Wyoming'
 
parse arg xtra; !=! ',' xtra /*add optional (fictitious) names.*/
@abcU= 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; !=space(!) /*!: the state list, no extra blanks*/
deads=0; dups=0; L.=0; !orig=!; z=0; @@.= /*initialize some REXX variables. */
say;z=0 do i=1 for z /*list state[↑] names inelide order given.dend─end (DE) states.*/
 
do de=0 for 2; !=!orig; @.= !=!orig /*use original state list for each. */
@.=
 
do states=0 by 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 if de then iterate /*don't tell error if doing 2nd pass*/
dups=dups+1 dups=dups + 1 /*bump the duplicate counter. */
say 'ignoring the 2nd naming of the state: ' x; iterate
iterate end
@.x=x end /*indicate this state name exists. */
@.x=x y=space(x,0); upper y; yLen=length(y) /*indicateget this stateupper name exists.with no spaces; Len*/
y=space(x,0); upper y; yLen=length(y) if de then do /*getIs upperthe namefirstt withpass? no spaces;Then Lenprocess.*/
say right(i,9) ##.i do j=1 for yLen /*showsee if theit's indexa number,dead─end state name.*/
 
if de then do _=substr(y, j, 1) /*Is the_: firsttis pass?some state Thenname processcharacter.*/
do j=1 for yLen if L._ \== 1 then iterate /*seeCount if¬ it's1? a dead─endThen state name is OK.*/
_=substr(y,j,1) say 'removing dead─end state [which has the letter ' /* _"]: " is some state name character.*/x
if L._\ deads==1deads then+ iterate1 /*Count ¬ 1? Then state/*bump namenumber isof OKdead─ends states. */
say 'removing dead─end stateiterate states [which has the letter ' _"]: " x /*go and process another state name.*/
deads=deads+1 end /*bump number of dead─ends states. j*/
z=z+1 iterate states /*gobump andcounter processof anotherthe state namenames. */
#.z=y; ##.z=x end /*jassign state name; also original.*/
z=z+1 /*bump counter of the state names. */end
else #.z=y;do ##.zk=x1 for yLen /*inventorize letters /*assignof state name; and original. */
_=substr(y,k,1); L._=L._ + 1 /*count each letter in state name. */
end
else do k=1 for yLen end /*inventorize state name's letters. k*/
end /*states*/ _=substr(y,k,1); L._=L._+1 /*count each letter in state name. /*the index STATES isn't incremented*/
end end /*kde*/
call list do k=j+1 to z /* ◄───list state K,names in stateorder Jgiven. ►───────┘ */
 
say; z say z 'state name's(z) "are useable."
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)*/
say /*[↑] look for mix and match states*/
 
do j=1 for z /* ◄──────────────────────────────────────────────────────────┐ */
do j=1 for z /*◄───────────────────────────────────────────────────────────────┐ */
do k=j+1 to z /* ◄─── state K, state J ►─────┘ /*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. */
 
doif verify(#.m, jk) \==1 0 for z; if m==j | m==k then iterate /*nois this state overlaps are allowed. name even possible? */
if verifynJK=elider(JK, #.m,jk)\==0 then iterate /*isa thisnew stateJK, nameafter eveneliding possible?#.m chars.*/
nJK=elider(JK,#.m) /*a new JK, after eliding #.m chars.*/
 
do n=m+1 to z; if n==j | n==k then iterate /*no overlaps are allowed. */
if verify(#.n, nJK) \==0 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*/
Line 1,629 ⟶ 1,617:
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: parse arg hay,pins /*remove letters (pins) from haystack. */
do e=1 for length(pins); p=pos( substr( pins, e, 1), hay)
if p==0 then iterate ; hay=overlay(' ', hay, p)
end /*e*/ /* [↑] remove a letter from haystack. */
return space(hay, 0) /*remove blanks from the haystack. */
/*──────────────────────────────────────────────────────────────────────────────────────*/</lang>
list: say; do i=1 for z; say right(i, 9) ##.i; end; say; return
'''output''' when using the default input:
s: if arg(1)==1 then return arg(3); return word(arg(2) 's', 1) /*pluralizer.*/</lang>
'''{{out|output'''|text=&nbsp; when using the default input:}}
<pre style="height:60ex">
removing dead─end state [which has the letter Z]: Arizona