diff --git a/README.md b/README.md index 5d67ebd..97e7d13 100644 --- a/README.md +++ b/README.md @@ -41,5 +41,5 @@ ![start.png](assets/start.png) -测试通过会会给出接下来的迷题位置,也可以在 [目录测试类](src/test/kotlin/io/github/sor2171/puzzles/Catalog.kt) 中查看所有迷题。 +测试通过会给出接下来的迷题位置,也可以在 [目录测试类](src/test/kotlin/io/github/sor2171/puzzles/Catalog.kt) 中查看所有迷题。 diff --git a/src/main/kotlin/io/github/sor2171/puzzles/service/PuzzleNote.kt b/src/main/kotlin/io/github/sor2171/puzzles/service/PuzzleNote.kt index 820f232..afec00c 100644 --- a/src/main/kotlin/io/github/sor2171/puzzles/service/PuzzleNote.kt +++ b/src/main/kotlin/io/github/sor2171/puzzles/service/PuzzleNote.kt @@ -7,17 +7,27 @@ class PuzzleNote( vararg notes: PuzzleNote? = arrayOf(null) ) { val className = CompileDetector.getColoredName(className) - val notes = notes.filterNotNull() + val notes = notes override fun toString(): String { - val validNotes = notes - if (validNotes.isEmpty()) return className + val sb = StringBuilder() - val builder = StringBuilder(className) - builder.append("\n │") - for (note in validNotes) { - builder.append("\n ├ ").append(note) + sb.append(className) + + if (notes.size > 1) { + for (i in 1 until notes.size) { + sb.append("\n │") + notes[i]?.let { note -> + sb.append("\n ├── ").append(note.className) + } + } } - return builder.toString() + + val nextNote = notes.firstOrNull() + if (nextNote != null) { + sb.append("\n │\n").append(nextNote.toString()) + } + + return sb.toString() } } \ No newline at end of file diff --git a/src/main/kotlin/io/github/sor2171/puzzles/utils/CatalogStructure.kt b/src/main/kotlin/io/github/sor2171/puzzles/utils/CatalogStructure.kt index b107729..186c7d4 100644 --- a/src/main/kotlin/io/github/sor2171/puzzles/utils/CatalogStructure.kt +++ b/src/main/kotlin/io/github/sor2171/puzzles/utils/CatalogStructure.kt @@ -26,18 +26,22 @@ object CatalogStructure { val firstPuzzleNote: PuzzleNote get() { - var lastNote: PuzzleNote? = null + var nextMainNote: PuzzleNote? = null + for (i in puzzlesArray.indices.reversed()) { val array = puzzlesArray[i] - val noteArray = Array(array.size - 1) { i -> - PuzzleNote(array[i + 1]) + + val children = Array(array.size) { index -> + if (index == 0) { + nextMainNote + } else { + PuzzleNote(array[index]) + } } - lastNote = PuzzleNote( - array[0], - *noteArray - ) + + nextMainNote = PuzzleNote(array[0], *children) } - return lastNote!! + return nextMainNote!! } fun printAllMessage() = println(firstPuzzleNote) diff --git a/src/test/kotlin/io/github/sor2171/puzzles/alphabet/You need a function, not a mathematical one.kt b/src/test/kotlin/io/github/sor2171/puzzles/alphabet/You need a function, not a mathematical one.kt new file mode 100644 index 0000000..7009257 --- /dev/null +++ b/src/test/kotlin/io/github/sor2171/puzzles/alphabet/You need a function, not a mathematical one.kt @@ -0,0 +1,26 @@ +@file:SkipCompile(skip = true) +@file:Suppress("ClassName") + +package io.github.sor2171.puzzles.alphabet + +import io.github.sor2171.puzzles.annotation.SkipCompile +import org.junit.jupiter.api.Test + +/** + * 你需要写一个函数,目标是把一个 [Pair] 的两个值相交换 + */ +class `You need a function, not a mathematical one` { + + // write start 1 + fun + // write end 1 + + @Test + fun puzzle() { + val pair = "abc" to 123 + val new = exchange(pair) + println(pair) + println(new) + assert(new.first == pair.second && new.second == pair.first) + } +} \ No newline at end of file