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 186c7d4..15d8413 100644 --- a/src/main/kotlin/io/github/sor2171/puzzles/utils/CatalogStructure.kt +++ b/src/main/kotlin/io/github/sor2171/puzzles/utils/CatalogStructure.kt @@ -18,9 +18,11 @@ object CatalogStructure { arrayOf( "${Classification.ALPHABET}.We need a variable, oh, maybe many", "${Classification.EXTRA}.More strange variables", + "${Classification.EXTRA}.More unpredictable variables", ), arrayOf( "${Classification.ALPHABET}.You need a function, not a mathematical one", + "${Classification.EXTRA}.More expressive functions", ) ) diff --git a/src/test/kotlin/io/github/sor2171/puzzles/extra/More expressive functions.kt b/src/test/kotlin/io/github/sor2171/puzzles/extra/More expressive functions.kt new file mode 100644 index 0000000..919e8bf --- /dev/null +++ b/src/test/kotlin/io/github/sor2171/puzzles/extra/More expressive functions.kt @@ -0,0 +1,32 @@ +@file:SkipCompile(skip = true) +@file:Suppress("ClassName") + +package io.github.sor2171.puzzles.extra + +import io.github.sor2171.puzzles.annotation.SkipCompile +import org.junit.jupiter.api.Test + +/** + * 本关考察你对 `infix` 和 `operator` 函数的理解。 + */ +class `More expressive functions` { + + // write start 1 + + // write end 1 + + @Test + fun puzzle() { + val laugh = "Ha" repeatBy 3 + assert(laugh == "HaHaHa") + + val message = "42" repeatBy 2 + assert(message == "4242") + + val exclamation = "!" * 4 + assert(exclamation == "!!!!") + + val combined = ("Yo" repeatBy 2) + ("~" * 3) + assert(combined == "YoYo~~~") + } +} diff --git a/src/test/kotlin/io/github/sor2171/puzzles/extra/More strange variables.kt b/src/test/kotlin/io/github/sor2171/puzzles/extra/More strange variables.kt index 84db253..9e8e8ce 100644 --- a/src/test/kotlin/io/github/sor2171/puzzles/extra/More strange variables.kt +++ b/src/test/kotlin/io/github/sor2171/puzzles/extra/More strange variables.kt @@ -8,6 +8,8 @@ import org.junit.jupiter.api.Test /** * 本关考察你对无符号数字的理解。 + * + * 共两个编写区域。 */ class `More strange variables` { // write start 1 diff --git a/src/test/kotlin/io/github/sor2171/puzzles/extra/More unpredictable variables.kt b/src/test/kotlin/io/github/sor2171/puzzles/extra/More unpredictable variables.kt new file mode 100644 index 0000000..dc2e30e --- /dev/null +++ b/src/test/kotlin/io/github/sor2171/puzzles/extra/More unpredictable variables.kt @@ -0,0 +1,46 @@ +@file:SkipCompile(skip = true) +@file:Suppress("ClassName") + +package io.github.sor2171.puzzles.extra + +import io.github.sor2171.puzzles.annotation.SkipCompile +import org.junit.jupiter.api.Test + +/** + * 很多初学者认为 `val` 就是“不可变”,但其实它只是“只读(Read-Only)”。 + * 此外,Kotlin 还提供了属性委托,比如只有在首次访问时才初始化的 `by lazy`。 + * 本关考察你对 Kotlin 属性(Property)、自定义 Getter 与惰性委托机制的理解。 + * + * 共两个编写区域。 + */ +class `More unpredictable variables` { + private var accessCount = 0 + var evalTimes = 0 + + val dynamicValue: Int + get() { + // write start 1 + + // write end 1 + } + + val lazyGreet: String by lazy { + // write start 2 + + // write end 2 + } + + @Test + fun puzzle() { + val first = dynamicValue + val second = dynamicValue + assert(first == 1 && second == 2) + assert(dynamicValue == 3) + + assert(evalTimes == 0) + assert(lazyGreet == "Hello, Alien!") + assert(evalTimes == 1) + assert(lazyGreet == "Hello, Alien!") + assert(evalTimes == 1) + } +}