Add tasks for all the new languages
This commit is contained in:
parent
9dc3c2bb62
commit
bba7bfd280
13208 changed files with 134745 additions and 0 deletions
25
Task/Visualize-a-tree/Lingo/visualize-a-tree-1.lingo
Normal file
25
Task/Visualize-a-tree/Lingo/visualize-a-tree-1.lingo
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
-- parent script "TreeItem"
|
||||
-- (minimal implementation with direct property access)
|
||||
|
||||
property name
|
||||
property children
|
||||
|
||||
on new (me, itemName)
|
||||
me.name = itemName
|
||||
me.children = []
|
||||
return me
|
||||
end
|
||||
|
||||
on addChild (me, child)
|
||||
me.children.add(child)
|
||||
end
|
||||
|
||||
-- print a tree
|
||||
on printTree (me, treeItem, indent)
|
||||
if voidP(treeItem) then treeItem = me
|
||||
if voidP(indent) then indent = ""
|
||||
put indent&treeItem.name
|
||||
repeat with c in treeItem.children
|
||||
me.printTree(c, indent&" ")
|
||||
end repeat
|
||||
end
|
||||
17
Task/Visualize-a-tree/Lingo/visualize-a-tree-2.lingo
Normal file
17
Task/Visualize-a-tree/Lingo/visualize-a-tree-2.lingo
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
-- create a tree
|
||||
root = script("TreeItem").new("root")
|
||||
a = script("TreeItem").new("a")
|
||||
root.addChild(a)
|
||||
b = script("TreeItem").new("b")
|
||||
root.addChild(b)
|
||||
a1 = script("TreeItem").new("a1")
|
||||
a.addChild(a1)
|
||||
a11 = script("TreeItem").new("a11")
|
||||
a1.addChild(a11)
|
||||
a12 = script("TreeItem").new("a12")
|
||||
a1.addChild(a12)
|
||||
b1 = script("TreeItem").new("b1")
|
||||
b.addChild(b1)
|
||||
|
||||
-- print the tree
|
||||
root.printTree()
|
||||
25
Task/Visualize-a-tree/Nim/visualize-a-tree.nim
Normal file
25
Task/Visualize-a-tree/Nim/visualize-a-tree.nim
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
import strutils
|
||||
|
||||
type
|
||||
Node[T] = ref TNode[T]
|
||||
TNode[T] = object
|
||||
data: T
|
||||
left, right: Node[T]
|
||||
|
||||
proc n[T](data: T; left, right: Node[T] = nil): Node[T] =
|
||||
Node[T](data: data, left: left, right: right)
|
||||
|
||||
proc indent[T](n: Node[T]): seq[string] =
|
||||
if n == nil: return @["-- (null)"]
|
||||
|
||||
result = @["--" & $n.data]
|
||||
|
||||
for a in indent n.left: result.add " |" & a
|
||||
|
||||
let r = indent n.right
|
||||
result.add " `" & r[0]
|
||||
for a in r[1..r.high]: result.add " " & a
|
||||
|
||||
let tree = 1.n(2.n(4.n(7.n),5.n),3.n(6.n(8.n,9.n)))
|
||||
|
||||
echo tree.indent.join("\n")
|
||||
40
Task/Visualize-a-tree/Phix/visualize-a-tree-1.phix
Normal file
40
Task/Visualize-a-tree/Phix/visualize-a-tree-1.phix
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
function rand_tree(integer low, integer high)
|
||||
for i=1 to 2 do
|
||||
integer v = rand(high-low+1)-1+low
|
||||
if v!=low
|
||||
and v!=high then
|
||||
return {v,rand_tree(low,v),rand_tree(v,high)}
|
||||
end if
|
||||
end for
|
||||
return 0
|
||||
end function
|
||||
|
||||
object tree = rand_tree(0,20) -- (can be 0, ~1% chance)
|
||||
|
||||
constant Horizontal = #C4,
|
||||
Horizontals = "\#C4",
|
||||
TopLeft = #DA,
|
||||
Vertical = #B3,
|
||||
BtmLeft = #C0
|
||||
|
||||
procedure visualise_tree(object tree, string root=Horizontals)
|
||||
if atom(tree) then
|
||||
puts(1,"<empty>\n")
|
||||
else
|
||||
object {v,l,r} = tree
|
||||
integer g = root[$]
|
||||
if sequence(l) then
|
||||
root[$] = iff(g=TopLeft or g=Horizontal?' ':Vertical)
|
||||
visualise_tree(l,root&TopLeft)
|
||||
end if
|
||||
root[$] = g
|
||||
puts(1,root)
|
||||
?v
|
||||
if sequence(r) then
|
||||
root[$] = iff(g=TopLeft?Vertical:' ')
|
||||
visualise_tree(r,root&BtmLeft)
|
||||
end if
|
||||
end if
|
||||
end procedure
|
||||
|
||||
visualise_tree(tree)
|
||||
1
Task/Visualize-a-tree/Phix/visualize-a-tree-2.phix
Normal file
1
Task/Visualize-a-tree/Phix/visualize-a-tree-2.phix
Normal file
|
|
@ -0,0 +1 @@
|
|||
pp(tree,{pp_Nest,10})
|
||||
21
Task/Visualize-a-tree/Sidef/visualize-a-tree.sidef
Normal file
21
Task/Visualize-a-tree/Sidef/visualize-a-tree.sidef
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
func visualize_tree(tree, label, children,
|
||||
indent = '',
|
||||
mids = ['├─', '│ '],
|
||||
ends = ['└─', ' '],
|
||||
) {
|
||||
func visit(node, pre) {
|
||||
gather {
|
||||
take(pre[0] + label(node))
|
||||
var chldn = children(node)
|
||||
var end = chldn.end
|
||||
chldn.each_kv { |i, child|
|
||||
if (i == end) { take(visit(child, [pre[1]] ~X+ ends)) }
|
||||
else { take(visit(child, [pre[1]] ~X+ mids)) }
|
||||
}
|
||||
}
|
||||
}
|
||||
visit(tree, [indent] * 2)
|
||||
}
|
||||
|
||||
var tree = 'root':['a':['a1':['a11':[]]],'b':['b1':['b11':[]],'b2':[],'b3':[]]]
|
||||
say visualize_tree(tree, { .first }, { .second }).flatten.join("\n")
|
||||
Loading…
Add table
Add a link
Reference in a new issue