两个 Extra
This commit is contained in:
@@ -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",
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
@@ -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~~~")
|
||||
}
|
||||
}
|
||||
@@ -8,6 +8,8 @@ import org.junit.jupiter.api.Test
|
||||
|
||||
/**
|
||||
* 本关考察你对无符号数字的理解。
|
||||
*
|
||||
* 共两个编写区域。
|
||||
*/
|
||||
class `More strange variables` {
|
||||
// write start 1
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user