Advent of Kotlin: Week 1: JSON stringify and possible value parentheses
I am happy to announce the Advent of Kotlin 2021! What is more, it starts now. In this event, I will be announcing a challenge each week, and your task is to write the solution. If you want to have some fun together, post your solution on Twitter with the tag AdventOfKotlin. You can also send it to contact@kt.academy. We assume that if you send it to us, you agree to publish it here.
I would also like to announce a contest. Those, who will share their solutions on Twitter, can win special gifts. The biggest one is an entry to the Kotlin Coroutines workshop in March (the workshop will be conducted online and will take place in the European time zone). As part of the activity we will be counting:
- sharing solutions with others (with #AdventOfKotlin @KtDotAcademy),
- creativity, readability, efficiency, scalability and style in solutions,
- helping others and engaging in discussions.
In the end, my final verdict has to be subjective, but I hope that doesn't prevent us from having fun.
The prerequisite to participate in the competition is to solve all the tasks. But beware, the challenges will get more difficult as time goes on.
This is a warm-up week. Instead of one complex challenge, there will be two easy ones. Although they still give you plenty of room to show good style :)
During the advent, you can work on this project:
The first challenge is to stringify the JsonElement
represented with the following hierarchy of types:
sealed class JsonElement
data class JsonObject(val fields: Map<String, JsonElement>) : JsonElement() {
constructor(vararg fields: Pair<String, JsonElement>) : this(fields.toMap())
}
data class JsonArray(val elements: List<JsonElement>) : JsonElement() {
constructor(vararg elements: JsonElement) : this(elements.toList())
}
data class JsonNumber(val value: Double) : JsonElement()
data class JsonString(val value: String) : JsonElement()
data class JsonBoolean(val value: Boolean) : JsonElement()
object JsonNull : JsonElement()
This is how your function should look like and behave:
fun JsonElement.stringify(): String = TODO()
assertEquals("""{"i":10}""", JsonObject("i" to JsonNumber(10.0)).stringify())
assertEquals(
"""{"a":"AAA","b":123.45,"c":true,"d":false,"e":null}""",
JsonObject(
"a" to JsonString("AAA"),
"b" to JsonNumber(123.45),
"c" to JsonBoolean(true),
"d" to JsonBoolean(false),
"e" to JsonNull,
).stringify()
)
assertEquals(
"""{"letters":["A","B","C"]}""",
JsonObject(
"letters" to JsonArray(
JsonString("A"),
JsonString("B"),
JsonString("C"),
)
).stringify()
)
assertEquals(
"""{"users":[{"name":"Marcin"},{"name":"Maja"}]}""",
JsonObject(
"users" to JsonArray(
JsonObject("name" to JsonString("Marcin")),
JsonObject("name" to JsonString("Maja")),
)
).stringify()
)
Here you can find unit tests to this exercise:
Here are the files with setup and tests in the project template:
The next challenge is from @cassidoo newsletter. I had a lot of fun solving it, so I am happy to share it with you.
The task is to create a function, that for a given n
, generates all combinations of n
well-formed pairs of parentheses. Here are a few usage examples:
fun generateParenthesisCombinations(num: Int): List<String> = TODO()
assertEquals(listOf("()"), generateParenthesisCombinations(1))
assertEquals(listOf("(())", "()()"), generateParenthesisCombinations(2).sorted())
assertEquals(
listOf("((()))", "(()())", "(())()", "()(())", "()()()"),
generateParenthesisCombinations(3).sorted()
)
assertEquals(
listOf(
"(((())))", "((()()))", "((())())", "((()))()", "(()(()))", "(()()())", "(()())()",
"(())(())", "(())()()", "()((()))", "()(()())", "()(())()", "()()(())", "()()()()"
),
generateParenthesisCombinations(4).sorted()
)
You can find more tests here:
Here are the files with setup and tests in the project template:
I wish you the best of luck in this and future challenges and can't wait to see your solutions :)
Marcin Moskala is a highly experienced developer and Kotlin instructor as the founder of Kt. Academy, an official JetBrains partner specializing in Kotlin training, Google Developers Expert, known for his significant contributions to the Kotlin community. Moskala is the author of several widely recognized books, including "Effective Kotlin," "Kotlin Coroutines," "Functional Kotlin," "Advanced Kotlin," "Kotlin Essentials," and "Android Development with Kotlin."
Beyond his literary achievements, Moskala is the author of the largest Medium publication dedicated to Kotlin. As a respected speaker, he has been invited to share his insights at numerous programming conferences, including events such as Droidcon and the prestigious Kotlin Conf, the premier conference dedicated to the Kotlin programming language.