June 2018 Update

This commit is contained in:
Ingy döt Net 2018-06-22 20:57:24 +00:00
parent ba8067c3b7
commit 22f33d4004
5278 changed files with 84726 additions and 14379 deletions

View file

@ -0,0 +1,50 @@
using Lazy
"""
Input a tree for display as a fringed structure.
"""
function fringe(tree)
fringey(node::Pair) = [fringey(i) for i in node]
fringey(leaf::Int) = leaf
fringey(tree)
end
"""
equalsfringe() uses a reduction to a lazy 1D list via
getleaflist() for its "equality" of fringes
"""
getleaflist(tree::Int) = [tree]
getleaflist(tree::Pair) = vcat(getleaflist(seq(tree[1])), getleaflist(seq(tree[2])))
getleaflist(tree::Lazy.LazyList) = vcat(getleaflist(tree[1]), getleaflist(tree[2]))
getleaflist(tree::Void) = []
equalsfringe(t1, t2) = (getleaflist(t1) == getleaflist(t2))
a = 1 => 2 => 3 => 4 => 5 => 6 => 7 => 8
b = 1 => (( 2 => 3 ) => (4 => (5 => ((6 => 7) => 8))))
c = (((1 => 2) => 3) => 4) => 5 => 6 => 7 => 8
x = 1 => 2 => 3 => 4 => 5 => 6 => 7 => 8 => 9
y = 0 => 2 => 3 => 4 => 5 => 6 => 7 => 8
z = 1 => 2 => (4 => 3) => 5 => 6 => 7 => 8
prettyprint(s) = println(replace("$s", r"\{Any,1\}|Any|Array\{T,1\}\swhere\sT|Array|", ""))
prettyprint(fringe(a))
prettyprint(fringe(b))
prettyprint(fringe(c))
prettyprint(fringe(x))
prettyprint(fringe(y))
prettyprint(fringe(z))
prettyprint(getleaflist(a))
prettyprint(getleaflist(b))
prettyprint(getleaflist(c))
println(equalsfringe(a, a))
println(equalsfringe(a, b))
println(equalsfringe(a, c))
println(equalsfringe(b, c))
println(equalsfringe(a, x) == false)
println(equalsfringe(a, y) == false)
println(equalsfringe(a, z) == false)

View file

@ -0,0 +1,26 @@
sub fringe ($tree) {
multi sub fringey (Pair $node) { fringey $_ for $node.kv; }
multi sub fringey ( Any $leaf) { take $leaf; }
gather fringey $tree;
}
sub samefringe ($a, $b) { fringe($a) eqv fringe($b) }
# Testing:
my $a = 1 => 2 => 3 => 4 => 5 => 6 => 7 => 8;
my $b = 1 => (( 2 => 3 ) => (4 => (5 => ((6 => 7) => 8))));
my $c = (((1 => 2) => 3) => 4) => 5 => 6 => 7 => 8;
my $x = 1 => 2 => 3 => 4 => 5 => 6 => 7 => 8 => 9;
my $y = 0 => 2 => 3 => 4 => 5 => 6 => 7 => 8;
my $z = 1 => 2 => (4 => 3) => 5 => 6 => 7 => 8;
say so samefringe $a, $a;
say so samefringe $a, $b;
say so samefringe $a, $c;
say not samefringe $a, $x;
say not samefringe $a, $y;
say not samefringe $a, $z;

View file

@ -1,81 +1,73 @@
/*REXX program examines the leaves of two binary trees (as shown below).*/
_=left('',28); say _ " A A "
say _ " / \ ◄════1st tree / \ "
say _ " / \ / \ "
say _ " / \ / \ "
say _ " B C B C "
say _ " / \ / 2nd tree════► / \ / "
say _ " D E F D E F "
say _ " / / \ / / \ "
say _ "G H I G δ I "
/*REXX pgm examines the leaves of 2 binary trees (as shown below), and finds inequities.*/
_=left('', 28); say _ " A A "
say _ " / \ ◄════1st tree / \ "
say _ " / \ / \ "
say _ " / \ / \ "
say _ " B C B C "
say _ " / \ / 2nd tree════► / \ / "
say _ " D E F D E F "
say _ " / / \ / / \ "
say _ "G H I G δ I "
say
#=0; done.=0; node.=0 /*initialize # leaves,DONE.,NODE.*/
call make_tree 1 /*define tree number 1 (1st tree)*/
call make_tree 2 /* " " " 2 (2nd " )*/
z1=root.1; L1=node.1.z1; done.1.z1=1 /*L1 is a leaf on tree number 1. */
z2=z1; L2=node.2.z2; done.2.z2=1 /*L2 " " " " " " 2. */
#=0; done.=0; @.=0 /*initialize: #, leaves, DONE., nodes*/
call make_tree 1 /*define tree number 1 (the 1st tree).*/
call make_tree 2 /* " " " 2 (the 2nd " ).*/
z1=root.1; L1=@.1.z1; done.1.z1=1 /*L1: is a leaf on tree number 1. */
z2=z1; L2=@.2.z2; done.2.z2=1 /*L2: " " " " " " 2. */
do # % 2 /*loop for the number of leaves. */
do # % 2 /*loop for the number of (tree) leaves.*/
if L1==L2 then do
if L1==0 then call sayX 'The trees are equal.'
say ' The ' L1 " leaf is identical in both trees."
do until \done.1.z1
z1=go_next(z1,1); L1=node.1.z1
end
say ' The ' L1 " leaf is identical in both trees."
do until \done.1.z1; z1=go_next(z1, 1); L1=@.1.z1; end
done.1.z1=1
do until \done.2.z2
z2=go_next(z2,2); L2=node.2.z2
end
do until \done.2.z2; z2=go_next(z2, 2); L2=@.2.z2; end
done.2.z2=1
end
else select
when L1==0 then call sayX L2 'exceeds leaves in 1st tree'
when L2==0 then call sayX L1 'exceeds leaves in 2nd tree'
otherwise call sayX 'A difference is: ' L1 '¬=' L2
when L1==0 then call sayX L2 'exceeds leaves in 1st tree'
when L2==0 then call sayX L1 'exceeds leaves in 2nd tree'
otherwise call sayX 'A difference is: ' L1 '¬=' L2
end /*select*/
end /*# % 2*/
exit
/*──────────────────────────────────GO_NEXT subroutine──────────────────*/
go_next: procedure expose node.; arg q,t /*find next node.*/
next=.
if node.t.q._Lson\==0 &, /*is there a left branch in tree?*/
node.t.q._Lson.vis==0 then do /*has this node been visited yet?*/
next=node.t.q._Lson /*──► next node. */
node.t.q._Lson.vis=1 /*leftside done. */
end
if next==. &,
node.t.q._Rson\==0 &, /*is there a right tree branch ? */
node.t.q._Rson.vis==0 then do /*has this node been VISited yet?*/
next=node.t.q._Rson /*──► next node. */
node.t.q._Rson.vis=1 /*rightside done.*/
end
if next==. then next=node.t.q._dad /*process the father node. */
return next /*the next node (or 0, if done).*/
/*──────────────────────────────────MAKE_NODE subroutine────────────────*/
make_node: parse arg name,t; # = #+1 /*make a new node/branch on tree.*/
q = node.t.0 + 1 ; node.t.q = name ; node.t.q._dad = 0
node.t.q._Lson = 0 ; node.t.q._Rson = 0 ; node.t.0 = q
return q /*number of the node just created*/
/*──────────────────────────────────MAKE_TREE subroutine────────────────*/
make_tree: procedure expose node. root. #; parse arg tree /*build trees*/
if tree==1 then hhh='H' /* [↓] must be a wood duck*/
else hhh='δ' /*the odd duck in the whole tree.*/
a=make_node('A', tree); root.tree=a
b=make_node('B', tree); call son 'L', b,a,tree
c=make_node('C', tree); call son 'R', c,a,tree
d=make_node('D', tree); call son 'L', d,b,tree
e=make_node('E', tree); call son 'R', e,b,tree
f=make_node('F', tree); call son 'L', f,c,tree
g=make_node('G', tree); call son 'L', g,d,tree
/*quacks like a duck?*/ h=make_node(hhh, tree); call son 'L', h,f,tree
i=make_node('I', tree); call son 'R', i,f,tree
return
/*──────────────────────────────────SAYX subroutine─────────────────────*/
sayX: say; say arg(1); say; exit /*tell msg and exit.*/
/*──────────────────────────────────SON subroutine──────────────────────*/
son: procedure expose node.; parse arg ?,son,dad,t; LR = '_'?"SON"
node.t.son._dad=dad; q=node.t.dad.LR /*define which son [↑] */
if q\==0 then do; node.t.q._dad=son; node.t.son.LR=q; end
node.t.dad.LR=son
if ?=='R' & node.t.dad.LR>0 then node.t.le._brother=node.t.dad.LR
return
/*──────────────────────────────────────────────────────────────────────────────────────*/
go_next: procedure expose @.; arg q,t; next=. /*find next node in the tree. */
if @.t.q._Lson\==0 &, /*there a left branch in tree?*/
@.t.q._Lson.vis==0 then do /*has this node been visited? */
next= @.t.q._Lson /*point to the ──► next node.*/
@.t.q._Lson.vis= 1 /*the leftside is completed. */
end
if next==. &,
@.t.q._Rson\==0 &, /*there a right tree branch ? */
@.t.q._Rson.vis==0 then do /*has this node been visited? */
next= @.t.q._Rson /*──► next node. */
@.t.q._Rson.vis= 1 /*the rightside is completed. */
end
if next==. then next= @.t.q._dad /*process the father node. */
return next /*next node (or 0, if done).*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
make_node: parse arg name,t; #=# +1; q= @.t.0 +1 /*make new node/branch on tree*/
@.t.q= name; @.t.q._Lson= 0; @.t.0= q
@.t.q._dad=0; @.t.q._Rson= 0; return q /*number of node just created.*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
make_tree: procedure expose @. root. #; parse arg tree /*construct a couple of trees.*/
if tree==1 then hhh='H' /* [↓] must be a wood duck*/
else hhh='δ' /*the odd duck in the tree. */
a=make_node('A', tree); root.tree=a
b=make_node('B', tree); call son 'L', b, a, tree
c=make_node('C', tree); call son 'R', c, a, tree
d=make_node('D', tree); call son 'L', d, b, tree
e=make_node('E', tree); call son 'R', e, b, tree
f=make_node('F', tree); call son 'L', f, c, tree
g=make_node('G', tree); call son 'L', g, d, tree
/*quacks like a duck?*/ h=make_node(hhh, tree); call son 'L', h, f, tree
i=make_node('I', tree); call son 'R', i, f, tree
return
/*──────────────────────────────────────────────────────────────────────────────────────*/
sayX: say; say arg(1); say; exit /*display a message and exit. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
son: procedure expose @.; parse arg ?,son,dad,t; LR= '_'?"SON"
@.t.son._dad= dad; q= @.t.dad.LR /* [↓] define which son. */
if q\==0 then do; @.t.q._dad= son; @.t.son.LR= q; end
@.t.dad.LR= son; return