Dinesman's multiple-dwelling problem: Difference between revisions
Content added Content deleted
(Added AppleScript.) |
No edit summary |
||
Line 2,490: | Line 2,490: | ||
Miller lives on floor 5 |
Miller lives on floor 5 |
||
Smith lives on floor 1</pre> |
Smith lives on floor 1</pre> |
||
=={{header|M2000 Interpreter}}== |
|||
<syntaxhighlight lang="m2000 interpreter"> |
|||
Module Dinesman_s_multiple_dwelling_problem { |
|||
// this is the standard perimutation function |
|||
// which create a lambda function: |
|||
// pointer_to_array=Func(&BooleanVariable) |
|||
// when BooleanVariable = true we get the last permutation |
|||
Function PermutationStep (a as array) { |
|||
c1=lambda (&f, a) ->{ |
|||
=a : f=true |
|||
} |
|||
integer m=len(a) |
|||
if m=0 then Error "No items to make permutations" |
|||
c=c1 |
|||
While m>1 |
|||
c1=lambda c2=c,p=0%, m=(,) (&f, a, clear as boolean=false) ->{ |
|||
if clear then m=(,) |
|||
if len(m)=0 then m=a |
|||
=cons(car(m),c2(&f, cdr(m))) |
|||
if f then f=false:p++: m=cons(cdr(m), car(m)) : if p=len(m) then p=0 : m=(,):: f=true |
|||
} |
|||
c=c1 |
|||
m-- |
|||
End While |
|||
=lambda c, a (&f, clear as boolean=false) -> { |
|||
=c(&f, a, clear) |
|||
} |
|||
} |
|||
boolean k |
|||
object s=("Baker", "Cooper", "Fletcher", "Miller", "Smith") |
|||
StepA=PermutationStep(s) |
|||
while not k |
|||
s=StepA(&k) |
|||
if s#val$(4)= "Baker" then continue |
|||
if s#val$(0)="Cooper" then continue |
|||
if s#val$(0)="Fletcher" then continue |
|||
if s#val$(4)="Fletcher" then continue |
|||
if s#pos("Cooper")> s#pos("Miller") then continue |
|||
if abs(s#pos("Smith")-s#pos("Fletcher"))=1 then continue |
|||
if abs(s#pos("Cooper")-s#pos("Fletcher"))=1 then continue |
|||
exit // for one solution |
|||
end while |
|||
object c=each(s) |
|||
while c |
|||
Print array$(c)+" lives on floor "+(c^+1) |
|||
end while |
|||
} |
|||
Dinesman_s_multiple_dwelling_problem |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
Smith lives on floor 1 |
|||
Cooper lives on floor 2 |
|||
Baker lives on floor 3 |
|||
Fletcher lives on floor 4 |
|||
Miller lives on floor 5 |
|||
</pre> |
|||
=={{header|Mathematica}} / {{header|Wolfram Language}}== |
=={{header|Mathematica}} / {{header|Wolfram Language}}== |