Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
54
Task/Department-numbers/Tcl/department-numbers-1.tcl
Normal file
54
Task/Department-numbers/Tcl/department-numbers-1.tcl
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
# Procedure named ".." returns list of integers from 1 to max.
|
||||
proc .. max {
|
||||
for {set i 1} {$i <= $max} {incr i} {
|
||||
lappend l $i
|
||||
}
|
||||
return $l
|
||||
}
|
||||
|
||||
# Procedure named "anyEqual" returns true if any elements are equal,
|
||||
# false otherwise.
|
||||
proc anyEqual l {
|
||||
if {[llength [lsort -unique $l]] != [llength $l]} {
|
||||
return 1
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
# Procedure named "odd" tells whether a value is odd or not.
|
||||
proc odd n {
|
||||
expr $n %2 != 0
|
||||
}
|
||||
|
||||
|
||||
# Procedure named "sum" sums its parameters.
|
||||
proc sum args {
|
||||
expr [join $args +]
|
||||
}
|
||||
|
||||
|
||||
# Create lists of candidate numbers using proc ".."
|
||||
set sanitation [.. 7]
|
||||
set fire $sanitation
|
||||
# Filter even numbers for police stations (remove odd ones).
|
||||
set police [lmap e $sanitation {
|
||||
if [odd $e] continue
|
||||
set e
|
||||
}]
|
||||
|
||||
|
||||
# Try all combinations and display acceptable ones.
|
||||
set valid 0
|
||||
foreach p $police {
|
||||
foreach s $sanitation {
|
||||
foreach f $fire {
|
||||
# Check for equal elements in list.
|
||||
if [anyEqual [list $p $s $f]] continue
|
||||
# Check for sum of list elements.
|
||||
if {[sum $p $s $f] != 12} continue
|
||||
puts "$p $s $f"
|
||||
incr valid
|
||||
}
|
||||
}
|
||||
}
|
||||
puts "$valid valid combinations found."
|
||||
13
Task/Department-numbers/Tcl/department-numbers-2.tcl
Normal file
13
Task/Department-numbers/Tcl/department-numbers-2.tcl
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
set valid 0
|
||||
for {set police 2} {$police <= 6} {incr police 2} {
|
||||
for {set sanitation 1} {$sanitation <= 7} {incr sanitation} {
|
||||
if {$police == $sanitation} continue
|
||||
for {set fire 1} {$fire <= 7} {incr fire} {
|
||||
if {$police == $fire || $sanitation == $fire} continue
|
||||
if {[expr $police + $sanitation + $fire] != 12} continue
|
||||
puts "$police $sanitation $fire"
|
||||
incr valid
|
||||
}
|
||||
}
|
||||
}
|
||||
puts "$valid valid combinations found."
|
||||
17
Task/Department-numbers/Tcl/department-numbers-3.tcl
Normal file
17
Task/Department-numbers/Tcl/department-numbers-3.tcl
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
set min 1
|
||||
set max 7
|
||||
set valid 0
|
||||
for {set police $min} {$police <= $max} {incr police} {
|
||||
if {[expr $police % 2] == 1} continue ;# filter even numbers for police
|
||||
for {set sanitation $min} {$sanitation <= $max} {incr sanitation} {
|
||||
if {$police == $sanitation} continue
|
||||
for {set fire $min} {$fire <= $max} {incr fire} {
|
||||
if {$police == $fire || $sanitation == $fire} continue
|
||||
if {[expr $police + $sanitation + $fire] != 12} continue
|
||||
puts "$police $sanitation $fire"
|
||||
incr valid
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
puts "$valid valid combinations found."
|
||||
52
Task/Department-numbers/Tcl/department-numbers-4.tcl
Normal file
52
Task/Department-numbers/Tcl/department-numbers-4.tcl
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
# Procedure named ".." returns list of integers from 1 to max.
|
||||
proc .. max {
|
||||
for {set i 1} {$i <= $max} {incr i} {
|
||||
lappend l $i
|
||||
}
|
||||
return $l
|
||||
}
|
||||
|
||||
|
||||
# Procedure named "..." returns list of n lists of integers from 1 to max.
|
||||
proc ... {max n} {
|
||||
foreach i [.. $n] {
|
||||
lappend result [.. $max]
|
||||
}
|
||||
return $result
|
||||
}
|
||||
|
||||
# Procedure named "crossProduct" returns cross product of lists
|
||||
proc crossProduct {listOfLists} {
|
||||
set result [list [list]]
|
||||
foreach factor $listOfLists {
|
||||
set newResult {}
|
||||
foreach combination $result {
|
||||
foreach elt $factor {
|
||||
lappend newResult [linsert $combination end $elt]
|
||||
}
|
||||
}
|
||||
set result $newResult
|
||||
}
|
||||
return $result
|
||||
}
|
||||
|
||||
# Procedure named "filter" filters list elements by using a
|
||||
# condition λ (lambda) expression
|
||||
proc filter {l condition} {
|
||||
return [lmap el $l {
|
||||
if {![apply $condition $el]} continue
|
||||
set el
|
||||
}]
|
||||
}
|
||||
|
||||
# Here the fun using lambda expressions begins. The following is the main program.
|
||||
|
||||
# Set λ expressions
|
||||
set λPoliceEven {_ {expr [lindex $_ 0] % 2 == 0}}
|
||||
set λNoEquals {_ {expr [llength [lsort -unique $_]] == [llength $_]}}
|
||||
set λSumIs12 {_ {expr [join $_ +] == 12}}
|
||||
|
||||
# Create all combinations and filter acceptable ones
|
||||
set numbersOk [filter [filter [filter [crossProduct [... 7 3]] ${λPoliceEven}] ${λSumIs12}] ${λNoEquals}]
|
||||
puts [join $numbersOk \n]
|
||||
puts "[llength $numbersOk] valid combinations found."
|
||||
Loading…
Add table
Add a link
Reference in a new issue