46 lines
1.3 KiB
Text
46 lines
1.3 KiB
Text
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
|