Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
39
Task/Visualize-a-tree/Wren/visualize-a-tree.wren
Normal file
39
Task/Visualize-a-tree/Wren/visualize-a-tree.wren
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
import "/dynamic" for Struct
|
||||
import "random" for Random
|
||||
|
||||
var Stem = Struct.create("Stem", ["str", "next"])
|
||||
|
||||
var SDOWN = " |"
|
||||
var SLAST = " `"
|
||||
var SNONE = " "
|
||||
|
||||
var rand = Random.new()
|
||||
|
||||
var tree // recursive
|
||||
tree = Fn.new { |root, head|
|
||||
var col = Stem.new(null, null)
|
||||
var tail = head
|
||||
while (tail) {
|
||||
System.write(tail.str)
|
||||
if (!tail.next) break
|
||||
tail = tail.next
|
||||
}
|
||||
System.print("--%(root)")
|
||||
if (root <= 1) return
|
||||
if (tail && tail.str == SLAST) tail.str = SNONE
|
||||
if (!tail) {
|
||||
tail = head = col
|
||||
} else {
|
||||
tail.next = col
|
||||
}
|
||||
while (root != 0) { // make a tree by doing something random
|
||||
var r = 1 + rand.int(root)
|
||||
root = root - r
|
||||
col.str = (root != 0) ? SDOWN : SLAST
|
||||
tree.call(r, head)
|
||||
}
|
||||
tail.next = null
|
||||
}
|
||||
|
||||
var n = 8
|
||||
tree.call(n, null)
|
||||
Loading…
Add table
Add a link
Reference in a new issue