Sync
This commit is contained in:
parent
6f050a029e
commit
776bba907c
3887 changed files with 59894 additions and 7280 deletions
|
|
@ -1,20 +1,20 @@
|
|||
import std.stdio, std.conv, std.algorithm, std.array;
|
||||
|
||||
struct BTNode(T) { T value; BTNode* left, right; }
|
||||
struct Node(T) { T value; Node* left, right; }
|
||||
|
||||
string[] treeIndent(T)(in BTNode!T* t) {
|
||||
if (t is null) return ["-- (null)"];
|
||||
const tr = treeIndent(t.right);
|
||||
return text("--", t.value) ~
|
||||
map!q{" |" ~ a}(treeIndent(t.left)).array() ~
|
||||
(" `" ~ tr[0]) ~ map!q{" " ~ a}(tr[1..$]).array();
|
||||
string[] treeIndent(T)(in Node!T* t) {
|
||||
if (!t) return ["-- (null)"];
|
||||
const tr = t.right.treeIndent;
|
||||
return "--" ~ t.value.text ~
|
||||
t.left.treeIndent.map!q{" |" ~ a}.array ~
|
||||
(" `" ~ tr[0]) ~ tr[1 .. $].map!q{" " ~ a}.array;
|
||||
}
|
||||
|
||||
void main () {
|
||||
static N(T)(T v, BTNode!T* l=null, BTNode!T* r=null) {
|
||||
return new BTNode!T(v, l, r);
|
||||
static N(T)(T v, Node!T* l=null, Node!T* r=null) {
|
||||
return new Node!T(v, l, r);
|
||||
}
|
||||
|
||||
const tree = N(1, N(2, N(4, N(7)), N(5)), N(3, N(6, N(8), N(9))));
|
||||
writefln("%-(%s\n%)", tree.treeIndent());
|
||||
writefln("%-(%s\n%)", tree.treeIndent);
|
||||
}
|
||||
|
|
|
|||
51
Task/Visualize-a-tree/Perl/visualize-a-tree.pl
Normal file
51
Task/Visualize-a-tree/Perl/visualize-a-tree.pl
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
#!/usr/bin/perl
|
||||
use warnings;
|
||||
use strict;
|
||||
use utf8;
|
||||
use open OUT => ':utf8', ':std';
|
||||
|
||||
sub parse {
|
||||
my ($tree) = shift;
|
||||
if (my ($root, $children) = $tree =~ /^(.+?)\((.*)\)$/) {
|
||||
|
||||
my $depth = 0;
|
||||
for my $pos (0 .. length($children) - 1) {
|
||||
my $char = \substr $children, $pos, 1;
|
||||
if (0 == $depth and ',' eq $$char) {
|
||||
$$char = "\x0";
|
||||
} elsif ('(' eq $$char) {
|
||||
$depth++;
|
||||
} elsif (')' eq $$char) {
|
||||
$depth--;
|
||||
}
|
||||
}
|
||||
return($root, [map parse($_), split /\x0/, $children]);
|
||||
|
||||
} else { # Leaf.
|
||||
return $tree;
|
||||
}
|
||||
}
|
||||
|
||||
sub output {
|
||||
my ($parsed, $prefix) = @_;
|
||||
my $is_root = not defined $prefix;
|
||||
$prefix //= ' ';
|
||||
while (my $member = shift @$parsed) {
|
||||
my $last = !@$parsed || (1 == @$parsed and ref $parsed->[0]);
|
||||
unless ($is_root) {
|
||||
substr $prefix, -3, 1, ' ';
|
||||
substr($prefix, -4, 1) =~ s/├/│/;
|
||||
substr $prefix, -2, 1, ref $member ? ' ' : '└' if $last;
|
||||
}
|
||||
|
||||
if (ref $member) {
|
||||
output($member, $prefix . '├─');
|
||||
} else {
|
||||
print $prefix, $member, "\n";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
my $tree = 'a(b0(c1,c2(d(ef,gh)),c3(i1,i2,i3(jj),i4(kk,m))),b1(C1,C2(D1(E),D2,D3),C3))';
|
||||
my $parsed = [parse($tree)];
|
||||
output($parsed);
|
||||
1
Task/Visualize-a-tree/PicoLisp/visualize-a-tree.l
Normal file
1
Task/Visualize-a-tree/PicoLisp/visualize-a-tree.l
Normal file
|
|
@ -0,0 +1 @@
|
|||
(view '(1 (2 (3 (4) (5) (6 (7))) (8 (9)) (10)) (11 (12) (13))))
|
||||
48
Task/Visualize-a-tree/Python/visualize-a-tree-1.py
Normal file
48
Task/Visualize-a-tree/Python/visualize-a-tree-1.py
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
Python 3.2.3 (default, May 3 2012, 15:54:42)
|
||||
[GCC 4.6.3] on linux2
|
||||
Type "copyright", "credits" or "license()" for more information.
|
||||
>>> help('pprint.pprint')
|
||||
Help on function pprint in pprint:
|
||||
|
||||
pprint.pprint = pprint(object, stream=None, indent=1, width=80, depth=None)
|
||||
Pretty-print a Python object to a stream [default is sys.stdout].
|
||||
|
||||
>>> from pprint import pprint
|
||||
>>> for tree in [ (1, 2, 3, 4, 5, 6, 7, 8),
|
||||
(1, (( 2, 3 ), (4, (5, ((6, 7), 8))))),
|
||||
((((1, 2), 3), 4), 5, 6, 7, 8) ]:
|
||||
print("\nTree %r can be pprint'd as:" % (tree, ))
|
||||
pprint(tree, indent=1, width=1)
|
||||
|
||||
|
||||
|
||||
Tree (1, 2, 3, 4, 5, 6, 7, 8) can be pprint'd as:
|
||||
(1,
|
||||
2,
|
||||
3,
|
||||
4,
|
||||
5,
|
||||
6,
|
||||
7,
|
||||
8)
|
||||
|
||||
Tree (1, ((2, 3), (4, (5, ((6, 7), 8))))) can be pprint'd as:
|
||||
(1,
|
||||
((2,
|
||||
3),
|
||||
(4,
|
||||
(5,
|
||||
((6,
|
||||
7),
|
||||
8)))))
|
||||
|
||||
Tree ((((1, 2), 3), 4), 5, 6, 7, 8) can be pprint'd as:
|
||||
((((1,
|
||||
2),
|
||||
3),
|
||||
4),
|
||||
5,
|
||||
6,
|
||||
7,
|
||||
8)
|
||||
>>>
|
||||
51
Task/Visualize-a-tree/Python/visualize-a-tree-2.py
Normal file
51
Task/Visualize-a-tree/Python/visualize-a-tree-2.py
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
>>> tree = "a",("b0",("c1","c2",("d",("ef","gh")),"c3",("i1","i2","i3",("jj"),"i4",("kk","m"))),"b1",("C1","C2",("D1",("E"),"D2","D3"),"C3"))
|
||||
>>> pprint(tree, width=1)
|
||||
('a',
|
||||
('b0',
|
||||
('c1',
|
||||
'c2',
|
||||
('d',
|
||||
('ef',
|
||||
'gh')),
|
||||
'c3',
|
||||
('i1',
|
||||
'i2',
|
||||
'i3',
|
||||
'jj',
|
||||
'i4',
|
||||
('kk',
|
||||
'm'))),
|
||||
'b1',
|
||||
('C1',
|
||||
'C2',
|
||||
('D1',
|
||||
'E',
|
||||
'D2',
|
||||
'D3'),
|
||||
'C3')))
|
||||
>>> copypasteoutput = ('a',
|
||||
... ('b0',
|
||||
... ('c1',
|
||||
... 'c2',
|
||||
... ('d',
|
||||
... ('ef',
|
||||
... 'gh')),
|
||||
... 'c3',
|
||||
... ('i1',
|
||||
... 'i2',
|
||||
... 'i3',
|
||||
... 'jj',
|
||||
... 'i4',
|
||||
... ('kk',
|
||||
... 'm'))),
|
||||
... 'b1',
|
||||
... ('C1',
|
||||
... 'C2',
|
||||
... ('D1',
|
||||
... 'E',
|
||||
... 'D2',
|
||||
... 'D3'),
|
||||
... 'C3')))
|
||||
>>> tree == copypasteoutput
|
||||
True
|
||||
>>>
|
||||
11
Task/Visualize-a-tree/Python/visualize-a-tree-3.py
Normal file
11
Task/Visualize-a-tree/Python/visualize-a-tree-3.py
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
>>> pprint(tree, width=60)
|
||||
('a',
|
||||
('b0',
|
||||
('c1',
|
||||
'c2',
|
||||
('d', ('ef', 'gh')),
|
||||
'c3',
|
||||
('i1', 'i2', 'i3', 'jj', 'i4', ('kk', 'm'))),
|
||||
'b1',
|
||||
('C1', 'C2', ('D1', 'E', 'D2', 'D3'), 'C3')))
|
||||
>>>
|
||||
38
Task/Visualize-a-tree/Python/visualize-a-tree-4.py
Normal file
38
Task/Visualize-a-tree/Python/visualize-a-tree-4.py
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
>>> mixedtree = ['a', ('b0', ('c1', 'c2', ['d', ('ef', 'gh')], 'c3', ('i1', 'i2',
|
||||
... 'i3', 'jj', 'i4', ['kk', 'm'])), 'b1', ('C1', 'C2', ('D1', 'E',
|
||||
... 'D2', 'D3'), 'C3'))]
|
||||
>>> pprint(mixedtree, width=1)
|
||||
['a',
|
||||
('b0',
|
||||
('c1',
|
||||
'c2',
|
||||
['d',
|
||||
('ef',
|
||||
'gh')],
|
||||
'c3',
|
||||
('i1',
|
||||
'i2',
|
||||
'i3',
|
||||
'jj',
|
||||
'i4',
|
||||
['kk',
|
||||
'm'])),
|
||||
'b1',
|
||||
('C1',
|
||||
'C2',
|
||||
('D1',
|
||||
'E',
|
||||
'D2',
|
||||
'D3'),
|
||||
'C3'))]
|
||||
>>> pprint(mixedtree, width=60)
|
||||
['a',
|
||||
('b0',
|
||||
('c1',
|
||||
'c2',
|
||||
['d', ('ef', 'gh')],
|
||||
'c3',
|
||||
('i1', 'i2', 'i3', 'jj', 'i4', ['kk', 'm'])),
|
||||
'b1',
|
||||
('C1', 'C2', ('D1', 'E', 'D2', 'D3'), 'C3'))]
|
||||
>>>
|
||||
13
Task/Visualize-a-tree/Racket/visualize-a-tree.rkt
Normal file
13
Task/Visualize-a-tree/Racket/visualize-a-tree.rkt
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
#lang racket/base
|
||||
|
||||
(define (visualize t0)
|
||||
(let loop ([t t0] [last? #t] [indent '()])
|
||||
(define (I mid last) (cond [(eq? t t0) ""] [last? mid] [else last]))
|
||||
(for-each display (reverse indent))
|
||||
(unless (eq? t t0) (printf "|\n"))
|
||||
(for-each display (reverse indent))
|
||||
(printf "~a~a\n" (I "\\-" "+-") (car t))
|
||||
(for ([s (cdr t)] [n (in-range (- (length t) 2) -1 -1)])
|
||||
(loop s (zero? n) (cons (I " " "| ") indent)))))
|
||||
|
||||
(visualize '(1 (2 (3 (4) (5) (6 (7))) (8 (9)) (10)) (11 (12) (13))))
|
||||
Loading…
Add table
Add a link
Reference in a new issue