tasks a-s
This commit is contained in:
parent
47bf37c096
commit
b83f433714
12433 changed files with 156208 additions and 123 deletions
33
Task/Same-Fringe/Tcl/same-fringe-1.tcl
Normal file
33
Task/Same-Fringe/Tcl/same-fringe-1.tcl
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
package require Tcl 8.6
|
||||
package require struct::tree
|
||||
|
||||
# A wrapper round a coroutine for iterating over the leaves of a tree in order
|
||||
proc leafiterator {tree} {
|
||||
coroutine coro[incr ::coroutines] apply {tree {
|
||||
yield [info coroutine]
|
||||
$tree walk [$tree rootname] node {
|
||||
if {[$tree isleaf $node]} {
|
||||
yield $node
|
||||
}
|
||||
}
|
||||
yieldto break
|
||||
}} $tree
|
||||
}
|
||||
|
||||
# Compare two trees for equality of their leaf node names
|
||||
proc samefringe {tree1 tree2} {
|
||||
set c1 [leafiterator $tree1]
|
||||
set c2 [leafiterator $tree2]
|
||||
try {
|
||||
while 1 {
|
||||
if {[set l1 [$c1]] ne [set l2 [$c2]]} {
|
||||
puts "$l1 != $l2"; # Just so we can see where we failed
|
||||
return 0
|
||||
}
|
||||
}
|
||||
return 1
|
||||
} finally {
|
||||
rename $c1 {}
|
||||
rename $c2 {}
|
||||
}
|
||||
}
|
||||
20
Task/Same-Fringe/Tcl/same-fringe-2.tcl
Normal file
20
Task/Same-Fringe/Tcl/same-fringe-2.tcl
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
# Make some trees to compare...
|
||||
struct::tree t1 deserialize {
|
||||
root {} {}
|
||||
a 0 {}
|
||||
d 3 {}
|
||||
e 3 {}
|
||||
b 0 {}
|
||||
c 0 {}
|
||||
}
|
||||
struct::tree t2 deserialize {
|
||||
root {} {}
|
||||
a 0 {}
|
||||
d 3 {}
|
||||
e 3 {}
|
||||
b 0 {}
|
||||
cc 0 {}
|
||||
}
|
||||
|
||||
# Print the boolean result of doing the comparison
|
||||
puts [samefringe t1 t2]
|
||||
Loading…
Add table
Add a link
Reference in a new issue