Anonymous user
Dinesman's multiple-dwelling problem: Difference between revisions
Dinesman's multiple-dwelling problem (view source)
Revision as of 15:29, 21 September 2020
, 3 years ago→{{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, the ground floor is the 1<sup>st</sup> floor, the next floor up is the 2<sup>nd</sup> floor, etc.
The REXX program is broken up into several parts:
Line 3,241:
::* displaying the possible solution(s), if any.
::* displaying the number of solutions found.
<br>Note that the '''TH''' 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 '''names''' variable).
<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
do
do !.5=1 for #
end /*p*/
do j=1 for #-1 /* [↓] people don't live on same floor*/
do k=j+1 to #
if !.j==!.k then iterate !.5 /*cohab? Then not valid.*/
end /*k*/
end /*j*/
say 'found' sols "solution"s(sols). /*display the number of solutions found*/
exit
/*──────────────────────────────────────────────────────────────────────────────────────*/▼
▲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▼
▲ 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))
▲/*──────────────────────────────────────────────────────────────────────────────────────*/
Waldo: if Baker == top then return
if Cooper == bottom then return
if Fletcher == bottom | Fletcher == top then return /*| ≡ "or".*/
if Miller \> Cooper 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= 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>
|