Update all new Tasks
This commit is contained in:
parent
00a190b0a6
commit
91df62d461
5697 changed files with 93386 additions and 804 deletions
49
Task/Fractran/Tcl/fractran-1.tcl
Normal file
49
Task/Fractran/Tcl/fractran-1.tcl
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
package require Tcl 8.6
|
||||
|
||||
oo::class create Fractran {
|
||||
variable fracs nco
|
||||
constructor {fractions} {
|
||||
set fracs {}
|
||||
foreach frac $fractions {
|
||||
if {[regexp {^(\d+)/(\d+),?$} $frac -> num denom]} {
|
||||
lappend fracs $num $denom
|
||||
} else {
|
||||
return -code error "$frac is not a supported fraction"
|
||||
}
|
||||
}
|
||||
if {![llength $fracs]} {
|
||||
return -code error "need at least one fraction"
|
||||
}
|
||||
}
|
||||
|
||||
method execute {n {steps 15}} {
|
||||
set co [coroutine [incr nco] my Generate $n]
|
||||
for {set i 0} {$i < $steps} {incr i} {
|
||||
lappend result [$co]
|
||||
}
|
||||
catch {rename $co ""}
|
||||
return $result
|
||||
}
|
||||
|
||||
method Step {n} {
|
||||
foreach {num den} $fracs {
|
||||
if {$n % $den} continue
|
||||
return [expr {$n * $num / $den}]
|
||||
}
|
||||
return -code break
|
||||
}
|
||||
method Generate {n} {
|
||||
yield [info coroutine]
|
||||
while 1 {
|
||||
yield $n
|
||||
set n [my Step $n]
|
||||
}
|
||||
return -code break
|
||||
}
|
||||
}
|
||||
|
||||
set ft [Fractran new {
|
||||
17/91 78/85 19/51 23/38 29/33 77/29 95/23
|
||||
77/19 1/17 11/13 13/11 15/14 15/2 55/1
|
||||
}]
|
||||
puts [$ft execute 2]
|
||||
12
Task/Fractran/Tcl/fractran-2.tcl
Normal file
12
Task/Fractran/Tcl/fractran-2.tcl
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
oo::objdefine $ft method pow2 {n} {
|
||||
set co [coroutine [incr nco] my Generate 2]
|
||||
set pows {}
|
||||
while {[llength $pows] < $n} {
|
||||
set item [$co]
|
||||
if {($item & ($item-1)) == 0} {
|
||||
lappend pows $item
|
||||
}
|
||||
}
|
||||
return $pows
|
||||
}
|
||||
puts [$ft pow2 10]
|
||||
Loading…
Add table
Add a link
Reference in a new issue