Skip to content

Instantly share code, notes, and snippets.

@andretietz
Last active April 23, 2024 19:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andretietz/8c4e1ebc4e4eeafcb87f9be89da3e1e4 to your computer and use it in GitHub Desktop.
Save andretietz/8c4e1ebc4e4eeafcb87f9be89da3e1e4 to your computer and use it in GitHub Desktop.
transforming nodes
// following data classes are available, feel free to create more if necessary
data class Node(
val id: String,
val parentId: String? = null, // nullable string , default null
val repeat: Int? = null // nullable int , default null
)
data class TargetNode(
val id: String, // this id's can be generated somehow
val parentId: String? = null,
val path: String? = null
)
// main method 1
fun main(args: Array<String>) {
val nodes = listOf(
// The list can be in random order
// for the sake of understanding the problem I left it in order.
Node("w1"),
Node("w2", "w1", 2),
Node("w3", "w1"),
Node("w4", "w3", 2),
)
nodes.forEach {
println(it)
}
transform(nodes).forEach {
println(it)
}
}
// output:
Node(id=w1, parentId=null, repeat=null)
Node(id=w2, parentId=w1, repeat=2)
Node(id=w3, parentId=w1, repeat=null)
Node(id=w4, parentId=w3, repeat=2)
TargetNode(id=1, parentId=null, path=null)
--TargetNode(id=2, parentId=1, path=w2+0)
--TargetNode(id=3, parentId=1, path=w2+1)
--TargetNode(id=4, parentId=1, path=null)
----TargetNode(id=5, parentId=4, path=w4+0)
----TargetNode(id=6, parentId=4, path=w4+1)
// different setup of nodes in main method:
fun main(args: Array<String>) {
val nodes = listOf(
Node("w1"),
Node("w2", "w1", 2),
Node("w3", "w2"), // changed
Node("w4", "w3", 2),
)
nodes.forEach {
println(it)
}
transform(nodes).forEach {
println(it)
}
}
// output:
Node(id=w1, parentId=null, repeat=null)
Node(id=w2, parentId=w1, repeat=2)
Node(id=w3, parentId=w2, repeat=null)
Node(id=w4, parentId=w3, repeat=2)
TargetNode(id=1, parentId=null, path=null)
--TargetNode(id=2, parentId=1, path=w2+0)
----TargetNode(id=4, parentId=2, path=w2+0)
------TargetNode(id=6, parentId=4, path=w2+0->w4+0)
------TargetNode(id=7, parentId=4, path=w2+0->w4+1)
--TargetNode(id=3, parentId=1, path=w2+1)
----TargetNode(id=5, parentId=3, path=w2+1)
------TargetNode(id=8, parentId=5, path=w2+1->w4+0)
------TargetNode(id=9, parentId=5, path=w2+1->w4+1)
// Task: write a transform method that's creating the target nodes, so that the output equals the given output
// requirement:
// * no recursion, if possible
// * as less iterations as possible
// Repeated Root Node:
Node(id=w2, parentId=w1, repeat=2)
Node(id=w4, parentId=w3, repeat=2)
Node(id=w1, parentId=null, repeat=3)
Node(id=w3, parentId=w1, repeat=null)
TargetNode(id=1, parentId=null, path=w1+0)
--TargetNode(id=4, parentId=1, path=w1+0->w2+0)
--TargetNode(id=5, parentId=1, path=w1+0->w2+1)
--TargetNode(id=6, parentId=1, path=w1+0)
----TargetNode(id=13, parentId=6, path=w1+0->w4+0)
----TargetNode(id=14, parentId=6, path=w1+0->w4+1)
TargetNode(id=2, parentId=null, path=w1+1)
--TargetNode(id=7, parentId=2, path=w1+1->w2+0)
--TargetNode(id=8, parentId=2, path=w1+1->w2+1)
--TargetNode(id=9, parentId=2, path=w1+1)
----TargetNode(id=15, parentId=9, path=w1+1->w4+0)
----TargetNode(id=16, parentId=9, path=w1+1->w4+1)
TargetNode(id=3, parentId=null, path=w1+2)
--TargetNode(id=10, parentId=3, path=w1+2->w2+0)
--TargetNode(id=11, parentId=3, path=w1+2->w2+1)
--TargetNode(id=12, parentId=3, path=w1+2)
----TargetNode(id=17, parentId=12, path=w1+2->w4+0)
----TargetNode(id=18, parentId=12, path=w1+2->w4+1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment