"🔒"
interface IPrintable {
    fun printWithIndent(indent: Int) {
        val indent = (1 .. indent).map {"| "}.joinToString("")
        println(indent + this.toString())
    }
}
#result
#

"✍️"
// @workUnit
class Printable<T>(val value: T): IPrintable {
    override fun printWithIndent(indent: Int){
        val indent = (1 .. indent).map {"| "}.joinToString("")
        println(indent + value.toString()) 
    }
}
#result
#

"🔒"
// @check
// @title: check patchString
val x = Printable(3.1415)
x.printWithIndent(0)
x.printWithIndent(1)
x.printWithIndent(2)
#result
#3.1415
#| 3.1415
#| | 3.1415

"✍️"
// @workUnit

data class TreeNode<T> (
    val value: T,
    val children: List<TreeNode<T>>,
) {
    // create a secondary constructor
    constructor(value: T): this(value, listOf())
    // override toString
    override fun toString(): String {
        var sizee = children.size
        if (sizee == 0) return "$value"
        else return "$value ($sizee)"
    }
}
#result
#

"🔒"
// @check
// @title: constructors

println(TreeNode("Hello"))
#result
#Hello

"🔒"
// @check
// @title: constructors with chidren

TreeNode(1234f, listOf(TreeNode(1), TreeNode(0)))
#result
#1234.0 (2)

"🔒"
// @check
// @title: nested construction
// @grade: 2

val tree = TreeNode(
    "C:\\",
    listOf(
        TreeNode("Photos",
                listOf(TreeNode("happy.jpg"),
                      TreeNode("sad.jpg"))),
        TreeNode("Documents",
                listOf(TreeNode("assignment-3", listOf(TreeNode(1), TreeNode(2), TreeNode(3)))))
    ))

println(tree)
println(tree.children[0])
println(tree.children[1])
#result
#C:\ (2)
#Photos (2)
#Documents (1)

"✍️"
// @workUnit

fun <T> printTree(node: TreeNode<T>, level: Int = 0) {
    // complete the code
    if (node.children.size == 0)
        level-1
    println(node.value)
    for (child in node.children){
        for(i in 0..level) print("| ")
        printTree(child, level+1)
    }
}
#result
#

"🔒"
// @check
// @title: printTree
// @grade: 5

printTree(tree)
#result
#C:\
#| Photos
#| | happy.jpg
#| | sad.jpg
#| Documents
#| | assignment-3
#| | | 1
#| | | 2
#| | | 3

Embed on website

To embed this program on your website, copy the following code and paste it into your website's HTML: