Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
52
Task/Ordered-partitions/Tcl/ordered-partitions-1.tcl
Normal file
52
Task/Ordered-partitions/Tcl/ordered-partitions-1.tcl
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
package require Tcl 8.5
|
||||
package require struct::set
|
||||
|
||||
# Selects all k-sized combinations from a list.
|
||||
# "Borrowed" from elsewhere on RC
|
||||
proc selectCombinationsFrom {k l} {
|
||||
if {$k == 0} {return {}} elseif {$k == [llength $l]} {return [list $l]}
|
||||
set all {}
|
||||
set n [expr {[llength $l] - [incr k -1]}]
|
||||
for {set i 0} {$i < $n} {} {
|
||||
set first [lindex $l $i]
|
||||
incr i
|
||||
if {$k == 0} {
|
||||
lappend all $first
|
||||
} else {
|
||||
foreach s [selectCombinationsFrom $k [lrange $l $i end]] {
|
||||
lappend all [list $first {*}$s]
|
||||
}
|
||||
}
|
||||
}
|
||||
return $all
|
||||
}
|
||||
|
||||
# Construct the partitioning of a given list
|
||||
proc buildPartitions {lst n args} {
|
||||
# Base case when we have no further partitions to process
|
||||
if {[llength $args] == 0} {
|
||||
return [list [list $lst]]
|
||||
}
|
||||
set result {}
|
||||
set c [selectCombinationsFrom $n $lst]
|
||||
if {[llength $c] == 0} {set c [list $c]}
|
||||
foreach comb $c {
|
||||
# Sort necessary for "nice" order
|
||||
set rest [lsort -integer [struct::set difference $lst $comb]]
|
||||
foreach p [buildPartitions $rest {*}$args] {
|
||||
lappend result [list $comb {*}$p]
|
||||
}
|
||||
}
|
||||
return $result
|
||||
}
|
||||
|
||||
# Wrapper that assembles the initial list and calls the partitioner
|
||||
proc partitions args {
|
||||
set sum [tcl::mathop::+ {*}$args]
|
||||
set startingSet {}
|
||||
for {set i 1} {$i <= $sum} {incr i} {
|
||||
lappend startingSet $i
|
||||
}
|
||||
|
||||
return [buildPartitions $startingSet {*}$args]
|
||||
}
|
||||
4
Task/Ordered-partitions/Tcl/ordered-partitions-2.tcl
Normal file
4
Task/Ordered-partitions/Tcl/ordered-partitions-2.tcl
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
puts [partitions 1 1 1]
|
||||
puts [partitions 2 2]
|
||||
puts [partitions 2 0 2]
|
||||
puts [partitions 2 2 0]
|
||||
Loading…
Add table
Add a link
Reference in a new issue