Another update from ingydotnet^djgoku
This commit is contained in:
parent
91df62d461
commit
948b86eafa
7604 changed files with 108452 additions and 22726 deletions
3
Task/Visualize-a-tree/Clojure/visualize-a-tree.clj
Normal file
3
Task/Visualize-a-tree/Clojure/visualize-a-tree.clj
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
(use 'vijual)
|
||||
|
||||
(draw-tree [[:A] [:B] [:C [:D [:E] [:F]] [:G]]])
|
||||
32
Task/Visualize-a-tree/Common-Lisp/visualize-a-tree-1.lisp
Normal file
32
Task/Visualize-a-tree/Common-Lisp/visualize-a-tree-1.lisp
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
(defun visualize (tree)
|
||||
(labels
|
||||
((rprint (list)
|
||||
(mapc #'princ (reverse list)))
|
||||
(vis-h (tree branches)
|
||||
(let ((len (length tree)))
|
||||
(loop
|
||||
for item in tree
|
||||
for idx from 1 to len do
|
||||
(cond
|
||||
((listp item)
|
||||
(rprint (cdr branches))
|
||||
(princ "+---+")
|
||||
(let ((next (cons "| "
|
||||
(if (= idx len)
|
||||
(cons " " (cdr branches))
|
||||
branches))))
|
||||
(terpri)
|
||||
(rprint (if (null item)
|
||||
(cdr next)
|
||||
next))
|
||||
(terpri)
|
||||
(vis-h item next)))
|
||||
(t
|
||||
(rprint (cdr branches))
|
||||
(princ item)
|
||||
(terpri)
|
||||
(rprint (if (= idx len)
|
||||
(cdr branches)
|
||||
branches))
|
||||
(terpri)))))))
|
||||
(vis-h tree '("| "))))
|
||||
32
Task/Visualize-a-tree/Common-Lisp/visualize-a-tree-2.lisp
Normal file
32
Task/Visualize-a-tree/Common-Lisp/visualize-a-tree-2.lisp
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
CL-USER> (visualize '(a b c ((d (e ((() ()))) f)) (g)))
|
||||
A
|
||||
|
|
||||
B
|
||||
|
|
||||
C
|
||||
|
|
||||
+---+
|
||||
| |
|
||||
| +---+
|
||||
| |
|
||||
| D
|
||||
| |
|
||||
| +---+
|
||||
| | |
|
||||
| | E
|
||||
| | |
|
||||
| | +---+
|
||||
| | |
|
||||
| | +---+
|
||||
| | |
|
||||
| | +---+
|
||||
| | |
|
||||
| | +---+
|
||||
| |
|
||||
| F
|
||||
|
|
||||
+---+
|
||||
|
|
||||
G
|
||||
|
||||
NIL
|
||||
46
Task/Visualize-a-tree/Go/visualize-a-tree-3.go
Normal file
46
Task/Visualize-a-tree/Go/visualize-a-tree-3.go
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
type tree []node
|
||||
|
||||
type node struct {
|
||||
label string
|
||||
children []int // indexes into tree
|
||||
}
|
||||
|
||||
func main() {
|
||||
vis(tree{
|
||||
0: node{"root", []int{1, 2, 3}},
|
||||
1: node{"ei", []int{4, 5}},
|
||||
2: node{"bee", nil},
|
||||
3: node{"si", nil},
|
||||
4: node{"dee", nil},
|
||||
5: node{"y", []int{6}},
|
||||
6: node{"eff", nil},
|
||||
})
|
||||
}
|
||||
|
||||
func vis(t tree) {
|
||||
if len(t) == 0 {
|
||||
fmt.Println("<empty>")
|
||||
return
|
||||
}
|
||||
var f func(int, string)
|
||||
f = func(n int, pre string) {
|
||||
ch := t[n].children
|
||||
if len(ch) == 0 {
|
||||
fmt.Println("╴", t[n].label)
|
||||
return
|
||||
}
|
||||
fmt.Println("┐", t[n].label)
|
||||
last := len(ch) - 1
|
||||
for _, ch := range ch[:last] {
|
||||
fmt.Print(pre, "├─")
|
||||
f(ch, pre+"│ ")
|
||||
}
|
||||
fmt.Print(pre, "└─")
|
||||
f(ch[last], pre+" ")
|
||||
}
|
||||
f(0, "")
|
||||
}
|
||||
43
Task/Visualize-a-tree/J/visualize-a-tree-1.j
Normal file
43
Task/Visualize-a-tree/J/visualize-a-tree-1.j
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
BOXC=: 9!:6 '' NB. box drawing characters
|
||||
EW =: {: BOXC NB. east-west
|
||||
|
||||
showtree=: 4 : 0
|
||||
NB. y is parent index for each node (non-indices for root nodes)
|
||||
NB. x is label for each node
|
||||
t=. (<EW,' ') ,@<@,:@,&":&.> x NB. tree fragments
|
||||
c=. |:(#~ e./@|:);(~.,"0&.>(</. i.@#)) y
|
||||
while. +./ b=. ({.c)*.//.-.e.~/c do.
|
||||
i=. b#~.{.c NB. parents whose children are leaves
|
||||
j=. </./(({.c)e.i)#"1 c NB. leaves grouped by parents
|
||||
t=. a: (;j)}t i}~ (i{t) subtree&.> j{&.><t
|
||||
c=. (-.({.c)e.i)#"1 c NB. prune edges to leaves
|
||||
end.
|
||||
;([: ,.&.>/ extend&.>)&> t -. a:
|
||||
)
|
||||
|
||||
subtree=: 4 : 0
|
||||
p=. EW={."1 s=. >{.t=. graft y
|
||||
(<(>{.x) root p),(<(connect p),.s),}.t
|
||||
)
|
||||
|
||||
graft=: 3 : 0
|
||||
n=. (-~ >./) #&> y
|
||||
f=. i.@(,&0)@#&.>@{.&.> y
|
||||
,&.>/ y ,&> n$&.>f
|
||||
)
|
||||
|
||||
connect=: 3 : 0
|
||||
b=. (+./\ *. +./\.) y
|
||||
c=. (b+2*y){' ',9 3 3{BOXC NB. │ NS ├ E
|
||||
c=. (0{BOXC) (b i. 1)}c NB. ┌ NW
|
||||
c=. (6{BOXC) (b i: 1)}c NB. └ SW
|
||||
j=. (b i. 1)+<.-:+/b
|
||||
EW&(j})^:(1=+/b) c j}~ ((0 3 6 9{BOXC)i.j{c){1 4 7 5{BOXC
|
||||
)
|
||||
|
||||
root=: 4 : 0
|
||||
j=. k+<.-:1+(y i: 1)-k=. y i. 1
|
||||
(-j)|.(#y){.x,.,:' ',EW
|
||||
)
|
||||
|
||||
extend=: 3 : '(+./\"1 (y=EW) *. *./\."1 y e.'' '',EW)}y,:EW'
|
||||
6
Task/Visualize-a-tree/J/visualize-a-tree-2.j
Normal file
6
Task/Visualize-a-tree/J/visualize-a-tree-2.j
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
(i.10) showtree _,}.p:inv i.10
|
||||
┌─ 6
|
||||
┌─ 1 ─── 3 ─┴─ 7
|
||||
│ ┌─ 8
|
||||
─ 0 ─┤ ┌─ 4 ─┴─ 9
|
||||
└─ 2 ─┴─ 5
|
||||
|
|
@ -19,7 +19,7 @@ tt: Procedure Expose node.
|
|||
c=node.k.i
|
||||
If st<>'' Then
|
||||
st=left(st,length(st)-2)' '
|
||||
st=repl(st,' ','` ')
|
||||
st=changestr('` ',st,' ')
|
||||
Say st||s||node.c.0name
|
||||
Call tt c,st||s
|
||||
End
|
||||
|
|
|
|||
12
Task/Visualize-a-tree/Ruby/visualize-a-tree-3.rb
Normal file
12
Task/Visualize-a-tree/Ruby/visualize-a-tree-3.rb
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
def ptree(tree,indent=" ")
|
||||
case tree
|
||||
when Array
|
||||
head,*tail=tree
|
||||
ptree(head,indent)
|
||||
s=tail.size-1
|
||||
tail.each_with_index { |tree1,i| ptree(tree1,"#{indent}#{((i==s) ? ' ':'|')} ") }
|
||||
else
|
||||
puts(indent.gsub(/\s\s$/,"--").gsub(/ --$/,"\\--")+tree.to_s)
|
||||
end
|
||||
end
|
||||
ptree [1,2,3,[4,5,6,[7,8,9]],3,[22,33]]
|
||||
Loading…
Add table
Add a link
Reference in a new issue