Dinesman's multiple-dwelling problem: Difference between revisions

→‎{{header|REXX}}: split compound DO statements (1 per line), added/changed whitespace and comments, use a template for the output section.
(Added Wren)
(→‎{{header|REXX}}: split compound DO statements (1 per line), added/changed whitespace and comments, use a template for the output section.)
Line 3,230:
with easy-to-read   '''if'''   statements.
 
Names of the tenants can be easily listed,   and the floors are numbered according to the American system,
<br>that is, &nbsp; the ground floor is the 1<sup>st</sup> floor, &nbsp; the next floor up is the 2<sup>nd</sup> floor, etc.
 
The REXX program is broken up into several parts:
Line 3,241:
::* &nbsp; displaying the possible solution(s), if any.
::* &nbsp; displaying the number of solutions found.
 
<br>Note that the &nbsp; '''TH''' &nbsp; function has extra boilerplate to handle larger numbers.
 
<br>With one more REXX statement, the tenants could be listed by the order of the floors they live on;
<br>Note that the &nbsp; '''TH''' &nbsp; function has extra boilerplate to handle larger numbers.
 
<br>With one more REXX statement, the tenants could be listed by the order of the floors they live on;
<br>(currently, the tenants are listed in the order they are listed in the &nbsp; '''names''' &nbsp; variable).
 
<br>The "rules" that contain &nbsp; '''==''' &nbsp; could be simplified to &nbsp; '''=''' &nbsp; for readability.
<lang rexx>/*REXX program solves the Dinesman's multiple─dwelling problem with "natural" wording.*/
names= 'Baker Cooper Fletcher Miller Smith' /*names of multiple─dwelling tenants. */
tenants=words(names) /*the number of tenants in the building*/
floors=5; top=floors; bottom=1; #=floors; /*floor 1 is the ground (bottom) floor.*/
sols= 0
do !.1=1 for #; do !.2=1 for #; do !.3=1 for #; do !.4=1 for #; do !.5=1 for #
do p=1 for tenants; _!.2=word(names,p);1 for upper _; call value _, !.p#
end do /*p*/ !.3=1 for #
do !.4=1 for end /*p*/#
do !.5=1 for #
say; do p=1 for tenants; _= word(names,p); upper _; tenant=right( word(namescall value _, !.p), 30)
end /*p*/
do j=1 for #-1 /* [↓] people don't live on same floor*/
do k=j+1 to #; if !.j==!.k then/*see iterateif !.5any people live on /*cohab?same floor.*/
if !.j==!.k then iterate !.5 /*cohab? Then not valid.*/
end /*k*/
end /*j*/
call Waldo call Waldo /* ◄══ where the rubber meets the road.*/
end; end; end; end; end /*!.5 & !.4 & !.3 & !.2 & !.1*/
Waldo: if Baker == top end then return/*!.4*/
if Cooper == bottomend then return/*!.3*/
if Miller end \> Cooper then return/*!.2*/
if Fletcherend == bottom | Fletcher == top then return /*!.1*/
 
say 'found' sols "solution"s(sols). /*display the number of solutions found*/
exit 0 /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
Waldo: if Baker == top then return
if Cooper == bottom then return
if Fletcher == bottom | Fletcher == top then return
if Miller \> Cooper then return
if Smith == Fletcher-1 | Smith == Fletcher+1 then return
if Fletcher == Cooper -1 | Fletcher == Cooper +1 then return
sols=sols+1
say; do p=1 for tenants; tenant=right( word(names, p), 30)
say tenant 'lives on the' !.p || th(!.p) "floor."
end /*p*/
return /* [↑] show tenants in order in NAMES.*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
s: if arg(1)=1 then return ''; return "s" /*a simple pluralizer function.*/
th: arg x; x=abs(x); return word('th st nd rd', 1 +x// 10* (x//100%10\==1)*(x//10<4))</lang>
/*──────────────────────────────────────────────────────────────────────────────────────*/
'''output'''
Waldo: if Baker == top then return
if Cooper == bottom then return
if Fletcher == bottom | Fletcher == top then return /*| ≡ "or".*/
if Miller \> Cooper then return
if Smith == Fletcher -1 | Smith == Fletcher +1 then return
if Fletcher == Cooper -1 | Fletcher == Cooper +1 then return
sols= sols + 1 /* [↑] "|" is REXX's "or" comparator.*/
say; do p=1 for tenants; tenant= right( word(names, p), 35)
say tenant 'lives on the' !.p || th(!.p) "floor."
end /*p*/ /* [↑] "||" is REXX's concatenation. */
return /* [↑] show tenants in order in NAMES.*/</lang>
{{out|output|text=&nbsp; when using the internal default values and definitions:}}
<pre>
Baker lives on the 3rd floor.
Cooper lives on the 2nd floor.
Fletcher lives on the 4th floor.
Miller lives on the 5th floor.
Smith lives on the 1st floor.
found 1 solution.
</pre>