Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
dkandalov committed Nov 27, 2018
1 parent 72b3b97 commit 3ebb97f
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 21 deletions.
12 changes: 6 additions & 6 deletions src/test/kotlin/org/kotlin99/binarytrees/P56.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ import org.kotlin99.binarytrees.Tree.Node

fun Tree<*>.isSymmetric(): Boolean = this == End || (this is Node<*> && left.isMirrorOf(right))

fun Tree<*>.isMirrorOf(that: Tree<*>): Boolean = when {
this is Node<*> && that is Node<*> ->
left.isMirrorOf(that.right) && right.isMirrorOf(that.left)
this == End -> that == End
else -> false
}
fun Tree<*>.isMirrorOf(that: Tree<*>): Boolean =
when {
this is Node<*> && that is Node<*> -> left.isMirrorOf(that.right) && right.isMirrorOf(that.left)
this == End -> that == End
else -> false
}

class P56Test {
@Test fun `tree is mirror of another tree`() {
Expand Down
11 changes: 6 additions & 5 deletions src/test/kotlin/org/kotlin99/binarytrees/P60.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@ import org.kotlin99.common.containsAll
fun maxNodeAmountInHBTree(height: Int): Int =
Math.pow(2.0, height.toDouble()).toInt() - 1

fun minNodeAmountInHBTree(height: Int): Int = when {
height <= 0 -> 0
height == 1 -> 1
else -> 1 + minNodeAmountInHBTree(height - 1) + minNodeAmountInHBTree(height - 2)
}
fun minNodeAmountInHBTree(height: Int): Int =
when {
height <= 0 -> 0
height == 1 -> 1
else -> 1 + minNodeAmountInHBTree(height - 1) + minNodeAmountInHBTree(height - 2)
}

fun maxHeightOfHBTree(nodeAmount: Int): Int =
(1..Int.MAX_VALUE).first { minNodeAmountInHBTree(it) > nodeAmount } - 1
Expand Down
21 changes: 11 additions & 10 deletions src/test/kotlin/org/kotlin99/multiwaytrees/P73.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,17 @@ private object SExprParser: TokenParser {
}

private fun Token.toMTree(): MTree<String> {
fun Token.toList(): List<Token> = when {
this is Atom -> listOf(this)
this is Seq -> this.tokens
else -> throw IllegalStateException(this.toString())
}
fun Token.toList(): List<Token> =
when (this) {
is Atom -> listOf(this)
is Seq -> this.tokens
else -> throw IllegalStateException(this.toString())
}

return when {
this is Seq -> MTree((tokens[0] as Atom).value, tokens[1].toList().map { it.toMTree() })
this is Atom -> MTree(value)
else -> throw IllegalStateException(this.toString())
return when (this) {
is Seq -> MTree((tokens[0] as Atom).value, tokens[1].toList().map { it.toMTree() })
is Atom -> MTree(value)
else -> throw IllegalStateException(this.toString())
}
}

Expand All @@ -45,7 +46,7 @@ private interface Token {
}

private data class Text(val value: String): Token {
override fun trim() = null
override fun trim(): Token? = null
override fun length() = value.length
override fun toString() = value
}
Expand Down

0 comments on commit 3ebb97f

Please sign in to comment.