langs a-z
This commit is contained in:
parent
db842d013d
commit
d066446780
11389 changed files with 98361 additions and 1020 deletions
|
|
@ -0,0 +1,26 @@
|
|||
sub next_perm ( @a is copy ) {
|
||||
my $j = @a.end - 1;
|
||||
return Nil if --$j < 0 while [>] @a[ $j, $j+1 ];
|
||||
|
||||
my $aj = @a[$j];
|
||||
my $k = @a.end;
|
||||
$k-- while [>] $aj, @a[$k];
|
||||
@a[ $j, $k ] .= reverse;
|
||||
|
||||
my $r = @a.end;
|
||||
my $s = $j + 1;
|
||||
@a[ $r--, $s++ ] .= reverse while $r > $s;
|
||||
return @a;
|
||||
}
|
||||
|
||||
# Contains only five floors. 5! = 120 permutations.
|
||||
for [1..5], &next_perm ...^ !* -> [ $b, $c, $f, $m, $s ] {
|
||||
say "Baker=$b Cooper=$c Fletcher=$f Miller=$m Smith=$s"
|
||||
if $b != 5 # Baker !live on top floor.
|
||||
and $c != 1 # Cooper !live on bottom floor.
|
||||
and $f != 1|5 # Fletcher !live on top or the bottom floor.
|
||||
and $m > $c # Miller lives on a higher floor than Cooper.
|
||||
and $s != $f-1|$f+1 # Smith !live adjacent to Fletcher
|
||||
and $f != $c-1|$c+1 # Fletcher !live adjacent to Cooper
|
||||
;
|
||||
}
|
||||
|
|
@ -0,0 +1,100 @@
|
|||
Prototype cond(Array t(1))
|
||||
|
||||
Enumeration #Null
|
||||
#Baker
|
||||
#Cooper
|
||||
#Fletcher
|
||||
#Miller
|
||||
#Smith
|
||||
EndEnumeration
|
||||
|
||||
Procedure checkTenands(Array tenants(1), Array Condions.cond(1))
|
||||
Protected i, j
|
||||
Protected.cond *f
|
||||
j=ArraySize(Condions())
|
||||
For i=0 To j
|
||||
*f=Condions(i) ; load the function pointer to the current condition
|
||||
If *f(tenants()) = #False
|
||||
ProcedureReturn #False
|
||||
EndIf
|
||||
Next
|
||||
ProcedureReturn #True
|
||||
EndProcedure
|
||||
|
||||
Procedure C1(Array t(1))
|
||||
If Int(Abs(t(#Fletcher)-t(#Cooper)))<>1
|
||||
ProcedureReturn #True
|
||||
EndIf
|
||||
EndProcedure
|
||||
|
||||
Procedure C2(Array t(1))
|
||||
If t(#Baker)<>5
|
||||
ProcedureReturn #True
|
||||
EndIf
|
||||
EndProcedure
|
||||
|
||||
Procedure C3(Array t(1))
|
||||
If t(#Cooper)<>1
|
||||
ProcedureReturn #True
|
||||
EndIf
|
||||
EndProcedure
|
||||
|
||||
Procedure C4(Array t(1))
|
||||
If t(#Miller) >= t(#Cooper)
|
||||
ProcedureReturn #True
|
||||
EndIf
|
||||
EndProcedure
|
||||
|
||||
Procedure C5(Array t(1))
|
||||
If t(#Fletcher)<>1 And t(#Fletcher)<>5
|
||||
ProcedureReturn #True
|
||||
EndIf
|
||||
EndProcedure
|
||||
|
||||
Procedure C6(Array t(1))
|
||||
If Int(Abs(t(#Smith)-t(#Fletcher)))<>1
|
||||
ProcedureReturn #True
|
||||
EndIf
|
||||
EndProcedure
|
||||
|
||||
|
||||
If OpenConsole()
|
||||
Dim People(4)
|
||||
Dim Conditions(5)
|
||||
Define a, b, c, d, e, i
|
||||
;
|
||||
;- Load all conditions
|
||||
Conditions(i)=@C1(): i+1
|
||||
Conditions(i)=@C2(): i+1
|
||||
Conditions(i)=@C3(): i+1
|
||||
Conditions(i)=@C4(): i+1
|
||||
Conditions(i)=@C5(): i+1
|
||||
Conditions(i)=@C6()
|
||||
;
|
||||
; generate and the all legal combinations
|
||||
For a=1 To 5
|
||||
For b=1 To 5
|
||||
If a=b: Continue: EndIf
|
||||
For c=1 To 5
|
||||
If a=c Or b=c: Continue: EndIf
|
||||
For d=1 To 5
|
||||
If d=a Or d=b Or d=c : Continue: EndIf
|
||||
For e=1 To 5
|
||||
If e=a Or e=b Or e=c Or e=d: Continue: EndIf
|
||||
People(#Baker)=a
|
||||
People(#Cooper)=b
|
||||
People(#Fletcher)=c
|
||||
People(#Miller)=d
|
||||
People(#Smith)=e
|
||||
If checkTenands(People(), Conditions())
|
||||
PrintN("Solution found;")
|
||||
PrintN("Baker="+Str(a)+#CRLF$+"Cooper="+Str(b)+#CRLF$+"Fletcher="+Str(c))
|
||||
PrintN("Miller="+Str(d)+#CRLF$+"Smith="+Str(e)+#CRLF$)
|
||||
EndIf
|
||||
Next
|
||||
Next
|
||||
Next
|
||||
Next
|
||||
Next
|
||||
Print("Press ENTER to exit"): Input()
|
||||
EndIf
|
||||
|
|
@ -0,0 +1,33 @@
|
|||
people$ = "Baler,Cooper,Fletcher,Miller,Smith"
|
||||
|
||||
for baler = 1 to 4 ' can not be in room 5
|
||||
for cooper = 2 to 5 ' can not be in room 1
|
||||
for fletcher = 2 to 4 ' can not be in room 1 or 5
|
||||
for miller = 1 to 5 ' can be in any room
|
||||
for smith = 1 to 5 ' can be in any room
|
||||
if miller > cooper and abs(smith - fletcher) > 1 and abs(fletcher - cooper) > 1 then
|
||||
if baler + cooper + fletcher + miller + smith = 15 then ' that is 1 + 2 + 3 + 4 + 5
|
||||
rooms$ = baler;cooper;fletcher;miller;smith
|
||||
bad = 0
|
||||
for i = 1 to 5 ' make sure each room is unique
|
||||
rm$ = chr$(i + 48)
|
||||
r1 = instr(rooms$,rm$)
|
||||
r2 = instr(rooms$,rm$,r1+1)
|
||||
if r2 <> 0 then bad = 1
|
||||
next i
|
||||
if bad = 0 then goto [roomAssgn] ' if it is not bad it is a good assignment
|
||||
end if
|
||||
end if
|
||||
next smith
|
||||
next miller
|
||||
next fletcher
|
||||
next cooper
|
||||
next baler
|
||||
print "Cam't assign rooms" ' print this if it can not find a solution
|
||||
wait
|
||||
|
||||
[roomAssgn]
|
||||
Print "Room Assignment"
|
||||
for i = 1 to 5
|
||||
print mid$(rooms$,i,1);" ";word$(people$,i,",");" "; ' print the room assignments
|
||||
next i
|
||||
|
|
@ -0,0 +1,71 @@
|
|||
#!/bin/bash
|
||||
|
||||
NAMES=(Baker Cooper Fletcher Miller Smith)
|
||||
|
||||
CRITERIA=(
|
||||
'Baker != TOP'
|
||||
'Cooper != BOTTOM'
|
||||
'Fletcher != TOP'
|
||||
'Fletcher != BOTTOM'
|
||||
'Miller > Cooper'
|
||||
'$(abs $(( Smith - Fletcher )) ) > 1'
|
||||
'$(abs $(( Fletcher - Cooper )) ) > 1'
|
||||
)
|
||||
|
||||
# Code below here shouldn't need to change to vary parameters
|
||||
let BOTTOM=0
|
||||
let TOP=${#NAMES[@]}-1
|
||||
|
||||
# Not available as a builtin
|
||||
function abs {
|
||||
let n=$1
|
||||
if (( n < 0 )); then let n=-n; fi
|
||||
echo "$n"
|
||||
}
|
||||
|
||||
# Algorithm we use to iterate over the permutations
|
||||
# requires that we start with the array sorted lexically
|
||||
NAMES=($(printf "%s\n" "${NAMES[@]}" | sort))
|
||||
while true; do
|
||||
# set each name to its position in the array
|
||||
for (( i=BOTTOM; i<=TOP; ++i )); do
|
||||
eval "${NAMES[i]}=$i"
|
||||
done
|
||||
|
||||
# check to see if we've solved the problem
|
||||
let solved=1
|
||||
for criterion in "${CRITERIA[@]}"; do
|
||||
if ! eval "(( $criterion ))"; then
|
||||
let solved=0
|
||||
break
|
||||
fi
|
||||
done
|
||||
if (( solved )); then
|
||||
echo "From bottom to top: ${NAMES[@]}"
|
||||
break
|
||||
fi
|
||||
|
||||
# Bump the names list to the next permutation
|
||||
let j=TOP-1
|
||||
while (( j >= BOTTOM )) && ! [[ "${NAMES[j]}" < "${NAMES[j+1]}" ]]; do
|
||||
let j-=1
|
||||
done
|
||||
if (( j < BOTTOM )); then break; fi
|
||||
let k=TOP
|
||||
while (( k > j )) && [[ "${NAMES[k]}" < "${NAMES[j]}" ]]; do
|
||||
let k-=1
|
||||
done
|
||||
if (( k <= j )); then break; fi
|
||||
t="${NAMES[j]}"
|
||||
NAMES[j]="${NAMES[k]}"
|
||||
NAMES[k]="$t"
|
||||
for (( k=1; k<=(TOP-j); ++k )); do
|
||||
a=BOTTOM+j+k
|
||||
b=TOP-k+1
|
||||
if (( a < b )); then
|
||||
t="${NAMES[a]}"
|
||||
NAMES[a]="${NAMES[b]}"
|
||||
NAMES[b]="$t"
|
||||
fi
|
||||
done
|
||||
done
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
include c:\cxpl\codes;
|
||||
int B, C, F, M, S;
|
||||
for B:= 1 to 4 do \Baker does not live on top (5th) floor
|
||||
for C:= 2 to 5 do \Cooper does not live on bottom floor
|
||||
if C#B then \Cooper & Baker live on different floors
|
||||
for F:= 2 to 4 do \Fletcher doesn't live on top or bottom
|
||||
if F#B & F#C & F#C-1 & F#C+1 then \ and she's not adjacent to Cooper
|
||||
for M:= 1 to 5 do
|
||||
if M#F & M#B & M>C then \Miller lives above Cooper
|
||||
for S:= 1 to 5 do \Smith is not adjacent to Fletcher
|
||||
if S#M & S#F & S#C & S#B & S#F-1 & S#F+1 then \show
|
||||
[Text(0, "Baker "); IntOut(0, B); CrLf(0); \all
|
||||
Text(0, "Cooper "); IntOut(0, C); CrLf(0); \possible
|
||||
Text(0, "Fletcher "); IntOut(0, F); CrLf(0); \solutions
|
||||
Text(0, "Miller "); IntOut(0, M); CrLf(0);
|
||||
Text(0, "Smith "); IntOut(0, S); CrLf(0);
|
||||
]
|
||||
Loading…
Add table
Add a link
Reference in a new issue