Dinesman's multiple-dwelling problem: Difference between revisions
Content added Content deleted
m (→{{header|Icon}} and {{header|Unicon}}: more commentary) |
(→{{header|Python}}: Add second solution method using Amb.) |
||
Line 366: | Line 366: | ||
=={{header|Python}}== |
=={{header|Python}}== |
||
===Parsing the problem statement=== |
|||
This example parses the statement of the problem as given and allows some variability such as the number of people, floors and constraints can be varied although the type of constraints allowed and the sentence structure is limited |
This example parses the statement of the problem as given and allows some variability such as the number of people, floors and constraints can be varied although the type of constraints allowed and the sentence structure is limited |
||
===Setup=== |
====Setup==== |
||
Parsing is done with the aid of the multi-line regular expression at the head of the program. |
Parsing is done with the aid of the multi-line regular expression at the head of the program. |
||
Line 504: | Line 505: | ||
handler[groupname](txt)</lang> |
handler[groupname](txt)</lang> |
||
===Problem statement=== |
====Problem statement==== |
||
This is not much more than calling a function on the text of the problem! |
This is not much more than calling a function on the text of the problem! |
||
<lang python>if __name__ == '__main__': |
<lang python>if __name__ == '__main__': |
||
Line 529: | Line 530: | ||
adjacent to Cooper's. Where does everyone live?""")</lang> |
adjacent to Cooper's. Where does everyone live?""")</lang> |
||
===Output=== |
====Output==== |
||
This shows the output from the original problem and then for another, slightly different problem to cover some of the variability asked for in the task. |
This shows the output from the original problem and then for another, slightly different problem to cover some of the variability asked for in the task. |
||
<pre>Floors are numbered from 1 to 5 inclusive. |
<pre>Floors are numbered from 1 to 5 inclusive. |
||
Line 546: | Line 547: | ||
Floor 2 is occupied by Cooper |
Floor 2 is occupied by Cooper |
||
Floor 1 is occupied by Baker |
Floor 1 is occupied by Baker |
||
</pre> |
|||
===Using the [[Amb#Python|Amb]] operator=== |
|||
In this example, the problem needs to be turned into valid Python code for use with the Amb operator. Setup is just to import Amb. |
|||
The second set of results corresponds to this modification to the problem statement: |
|||
<pre> Baker, Cooper, Fletcher, Miller, Guinan, and Smith |
|||
live on different floors of an apartment house that contains |
|||
only seven floors. Guinan does not live on either the top or the third or the fourth floor. |
|||
Baker does not live on the top floor. Cooper |
|||
does not live on the bottom floor. Fletcher does not live on |
|||
either the top or the bottom floor. Miller lives on a higher |
|||
floor than does Cooper. Smith does not live on a floor |
|||
adjacent to Fletcher's. Fletcher does not live on a floor |
|||
adjacent to Cooper's. Where does everyone live</pre> |
|||
<lang python>from amb import Amb |
|||
if __name__ == '__main__': |
|||
amb = Amb() |
|||
maxfloors = 5 |
|||
floors = range(1, maxfloors+1) |
|||
# Possible floors for each person |
|||
Baker, Cooper, Fletcher, Miller, Smith = (amb(floors) for i in range(5)) |
|||
for _dummy in amb( lambda Baker, Cooper, Fletcher, Miller, Smith: ( |
|||
len(set([Baker, Cooper, Fletcher, Miller, Smith])) == 5 # each to a separate floor |
|||
and Baker != maxfloors |
|||
and Cooper != 1 |
|||
and Fletcher not in (maxfloors, 1) |
|||
and Miller > Cooper |
|||
and (Smith - Fletcher) not in (1, -1) # Not adjacent |
|||
and (Fletcher - Cooper) not in (1, -1) # Not adjacent |
|||
) ): |
|||
print 'Floors are numbered from 1 to %i inclusive.' % maxfloors |
|||
print '\n'.join(sorted(' Floor %i is occupied by %s' |
|||
% (globals()[name], name) |
|||
for name in 'Baker, Cooper, Fletcher, Miller, Smith'.split(', '))) |
|||
break |
|||
else: |
|||
print 'No solution found.' |
|||
print |
|||
print '# Add another person with more constraints and more floors:' |
|||
# The order that Guinan is added to any list of people must stay consistant |
|||
amb = Amb() |
|||
maxfloors = 7 |
|||
floors = range(1, maxfloors+1) |
|||
# Possible floors for each person |
|||
Baker, Cooper, Fletcher, Miller, Guinan, Smith = (amb(floors) for i in range(6)) |
|||
for _dummy in amb( lambda Baker, Cooper, Fletcher, Miller, Guinan, Smith: ( |
|||
len(set([Baker, Cooper, Fletcher, Miller, Guinan, Smith])) == 6 # each to a separate floor |
|||
and Guinan not in (maxfloors, 3, 4) |
|||
and Baker != maxfloors |
|||
and Cooper != 1 |
|||
and Fletcher not in (maxfloors, 1) |
|||
and Miller > Cooper |
|||
and (Smith - Fletcher) not in (1, -1) # Not adjacent |
|||
and (Fletcher - Cooper) not in (1, -1) # Not adjacent |
|||
) ): |
|||
print 'Floors are numbered from 1 to %i inclusive.' % maxfloors |
|||
print '\n'.join(sorted(' Floor %i is occupied by %s' |
|||
% (globals()[name], name) |
|||
for name in 'Baker, Cooper, Fletcher, Miller, Guinan, Smith'.split(', '))) |
|||
break |
|||
else: |
|||
print 'No solution found.' |
|||
print |
|||
</lang> |
|||
====Output==== |
|||
<pre>Floors are numbered from 1 to 5 inclusive. |
|||
Floor 1 is occupied by Smith |
|||
Floor 2 is occupied by Cooper |
|||
Floor 3 is occupied by Baker |
|||
Floor 4 is occupied by Fletcher |
|||
Floor 5 is occupied by Miller |
|||
# Add another person with more constraints and more floors: |
|||
Floors are numbered from 1 to 7 inclusive. |
|||
Floor 1 is occupied by Baker |
|||
Floor 2 is occupied by Cooper |
|||
Floor 3 is occupied by Miller |
|||
Floor 4 is occupied by Fletcher |
|||
Floor 5 is occupied by Guinan |
|||
Floor 6 is occupied by Smith |
|||
</pre> |
</pre> |