Skip to content

Commit bf324ab

Browse files
committed
Added the current version of koans in a new format
0 parents  commit bf324ab

File tree

266 files changed

+3616
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

266 files changed

+3616
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.idea
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import Answer.*
2+
3+
enum class Answer { a, b, c }
4+
5+
val answers = mapOf<Int, Answer?>(
6+
1 to c, 2 to b, 3 to b, 4 to c
7+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
type: edu
2+
files:
3+
- name: src/Task.kt
4+
visible: true
5+
placeholders:
6+
- offset: 91
7+
length: 30
8+
placeholder_text: 1 to null, 2 to null, 3 to null, 4 to null
9+
- name: test/tests.kt
10+
visible: false
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
id: 234760
2+
update_date: Thu, 15 Feb 2018 10:40:54 UTC
+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<h2>Builders: how it works</h2>
2+
<p>Look at the questions below and give your answers</p>
3+
<p><strong>1. In the Kotlin code</strong></p><pre><code data-lang="text/x-kotlin">tr {
4+
td {
5+
text(&quot;Product&quot;)
6+
}
7+
td {
8+
text(&quot;Popularity&quot;)
9+
}
10+
}
11+
</code></pre>
12+
<p><strong>'td' is:</strong></p>
13+
<p>a. special built-in syntactic construct</p>
14+
<p>b. function declaration</p>
15+
<p>c. function invocation</p>
16+
<hr/>
17+
<p><strong>2. In the Kotlin code</strong></p><pre><code data-lang="text/x-kotlin">tr (color = &quot;yellow&quot;) {
18+
td {
19+
text(&quot;Product&quot;)
20+
}
21+
td {
22+
text(&quot;Popularity&quot;)
23+
}
24+
}
25+
</code></pre>
26+
<p><strong>'color' is:</strong></p>
27+
<p>a. new variable declaration</p>
28+
<p>b. argument name</p>
29+
<p>c. argument value</p>
30+
<hr/>
31+
<p><strong>3. The block</strong></p><pre><code data-lang="text/x-kotlin">{
32+
text(&quot;Product&quot;)
33+
}
34+
</code></pre>
35+
<p><strong>from the previous question is:</strong></p>
36+
<p>a. block inside built-in syntax construction <code>td</code></p>
37+
<p>b. function literal (or "lambda")</p>
38+
<p>c. something mysterious</p>
39+
<hr/>
40+
<p><strong>4. For the code</strong></p><pre><code data-lang="text/x-kotlin">tr (color = &quot;yellow&quot;) {
41+
this.td {
42+
text(&quot;Product&quot;)
43+
}
44+
td {
45+
text(&quot;Popularity&quot;)
46+
}
47+
}
48+
</code></pre>
49+
<p><strong>which of the following is true:</strong></p>
50+
<p>a. this code doesn't compile</p>
51+
<p>b. <code>this</code> refers to an instance of an outer class</p>
52+
<p>c. <code>this</code> refers to a receiver parameter TR of the function literal:</p><pre><code data-lang="text/x-kotlin">tr (color = &quot;yellow&quot;) { TR.(): Unit -&gt;
53+
this.td {
54+
text(&quot;Product&quot;)
55+
}
56+
}
57+
</code></pre>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import org.junit.Test
2+
import org.junit.Assert
3+
import Answer.*
4+
5+
class TestBuildersHowItWorks {
6+
@Test fun testBuildersQuiz() {
7+
if (answers.values.toSet() == setOf(null)) {
8+
Assert.fail("Please specify your answers!")
9+
}
10+
val correctAnswers = mapOf(22 - 20 to b, 1 + 3 to c, 11 - 8 to b, 79 - 78 to c)
11+
if (correctAnswers != answers) {
12+
val incorrect = (1..4).filter { answers[it] != correctAnswers[it] }
13+
Assert.fail("Your answers are incorrect! $incorrect")
14+
}
15+
}
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
fun task(): List<Boolean> {
2+
val isEven: Int.() -> Boolean = { this % 2 == 0 }
3+
val isOdd: Int.() -> Boolean = { this % 2 != 0 }
4+
5+
return listOf(42.isOdd(), 239.isOdd(), 294823098.isEven())
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
type: edu
2+
files:
3+
- name: src/Task.kt
4+
visible: true
5+
placeholders:
6+
- offset: 66
7+
length: 13
8+
placeholder_text: TODO()
9+
- offset: 119
10+
length: 13
11+
placeholder_text: TODO()
12+
- name: test/tests.kt
13+
visible: false
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
id: 234756
2+
update_date: Thu, 15 Feb 2018 10:40:49 UTC
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<h2>Extension function literals</h2>
2+
<p>Read about <a href="https://kotlinlang.org/docs/reference/lambdas.html#function-literals-with-receiver">function literals with receiver</a>.</p>
3+
<p>You can declare <code>isEven</code> and <code>isOdd</code> as values, that can be called as extension functions. Complete the declarations below.</p>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import org.junit.Assert
2+
import org.junit.Test
3+
import koans.util.inEquals
4+
5+
class TestExtensionFunctionLiterals {
6+
@Test fun testIsOddAndIsEven() {
7+
Assert.assertEquals("The functions 'isOdd' and 'isEven' should be implemented correctly".inEquals(),
8+
listOf(false, true, true), task())
9+
10+
}
11+
}

Builders/Html builders/src/Task.kt

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
fun renderProductTable(): String {
2+
return html {
3+
table {
4+
tr (color = getTitleColor()){
5+
td {
6+
text("Product")
7+
}
8+
td {
9+
text("Price")
10+
}
11+
td {
12+
text("Popularity")
13+
}
14+
}
15+
val products = getProducts()
16+
for ((index, product) in products.withIndex()) {
17+
tr {
18+
td (color = getCellColor(index, 0)) {
19+
text(product.description)
20+
}
21+
td (color = getCellColor(index, 1)) {
22+
text(product.price)
23+
}
24+
td (color = getCellColor(index, 2)) {
25+
text(product.popularity)
26+
}
27+
}
28+
}
29+
}
30+
}.toString()
31+
}
32+
33+
fun getTitleColor() = "#b9c9fe"
34+
fun getCellColor(index: Int, row: Int) = if ((index + row) %2 == 0) "#dce4ff" else "#eff2ff"

Builders/Html builders/src/data.kt

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
data class Product(val description: String, val price: Double, val popularity: Int)
2+
3+
val cactus = Product("cactus", 11.2, 13)
4+
val cake = Product("cake", 3.2, 111)
5+
val camera = Product("camera", 134.5, 2)
6+
val car = Product("car", 30000.0, 0)
7+
val carrot = Product("carrot", 1.34, 5)
8+
val cellPhone = Product("cell phone", 129.9, 99)
9+
val chimney = Product("chimney", 190.0, 2)
10+
val certificate = Product("certificate", 99.9, 1)
11+
val cigar = Product("cigar", 8.0, 51)
12+
val coffee = Product("coffee", 8.0, 67)
13+
val coffeeMaker = Product("coffee maker", 201.2, 1)
14+
val cola = Product("cola", 4.0, 67)
15+
val cranberry = Product("cranberry", 4.1, 39)
16+
val crocs = Product("crocs", 18.7, 10)
17+
val crocodile = Product("crocodile", 20000.2, 1)
18+
val cushion = Product("cushion", 131.0, 0)
19+
20+
fun getProducts() = listOf(cactus, cake, camera, car, carrot, cellPhone, chimney, certificate, cigar, coffee, coffeeMaker,
21+
cola, cranberry, crocs, crocodile, cushion)

Builders/Html builders/src/demo.kt

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import javax.swing.JFrame
2+
import javax.swing.JLabel
3+
import javax.swing.JScrollPane
4+
import javax.swing.SwingConstants.CENTER
5+
6+
fun main(args: Array<String>) {
7+
with (JFrame("Product popularity")) {
8+
setSize(600, 600)
9+
defaultCloseOperation = JFrame.EXIT_ON_CLOSE
10+
add(JScrollPane(JLabel(renderProductTable(), CENTER)))
11+
isVisible = true
12+
}
13+
}

Builders/Html builders/src/html.kt

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import java.util.ArrayList
2+
3+
open class Tag(val name: String) {
4+
val children: MutableList<Tag> = ArrayList()
5+
val attributes: MutableList<Attribute> = ArrayList()
6+
7+
override fun toString(): String {
8+
return "<$name" +
9+
(if (attributes.isEmpty()) "" else attributes.joinToString(separator = "", prefix = " ")) + ">" +
10+
(if (children.isEmpty()) "" else children.joinToString(separator = "")) +
11+
"</$name>"
12+
}
13+
}
14+
15+
class Attribute(val name : String, val value : String) {
16+
override fun toString() = """$name="$value" """
17+
}
18+
19+
fun <T: Tag> T.set(name: String, value: String?): T {
20+
if (value != null) {
21+
attributes.add(Attribute(name, value))
22+
}
23+
return this
24+
}
25+
26+
fun <T: Tag> Tag.doInit(tag: T, init: T.() -> Unit): T {
27+
tag.init()
28+
children.add(tag)
29+
return tag
30+
}
31+
32+
class Html: Tag("html")
33+
class Table: Tag("table")
34+
class Center: Tag("center")
35+
class TR: Tag("tr")
36+
class TD: Tag("td")
37+
class Text(val text: String): Tag("b") {
38+
override fun toString() = text
39+
}
40+
41+
fun html(init: Html.() -> Unit): Html = Html().apply(init)
42+
43+
fun Html.table(init : Table.() -> Unit) = doInit(Table(), init)
44+
fun Html.center(init : Center.() -> Unit) = doInit(Center(), init)
45+
46+
fun Table.tr(color: String? = null, init : TR.() -> Unit) = doInit(TR(), init).set("bgcolor", color)
47+
48+
fun TR.td(color: String? = null, align : String = "left", init : TD.() -> Unit) = doInit(TD(), init).set("align", align).set("bgcolor", color)
49+
50+
fun Tag.text(s : Any?) = doInit(Text(s.toString()), {})
51+

Builders/Html builders/task-info.yaml

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
type: edu
2+
files:
3+
- name: src/html.kt
4+
visible: true
5+
- name: src/Task.kt
6+
visible: true
7+
placeholders:
8+
- offset: 84
9+
length: 25
10+
placeholder_text: /* TODO */
11+
- offset: 404
12+
length: 484
13+
placeholder_text: TODO()
14+
- name: src/data.kt
15+
visible: true
16+
- name: src/demo.kt
17+
visible: true
18+
- name: test/tests.kt
19+
visible: false
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
id: 234759
2+
update_date: Thu, 15 Feb 2018 10:40:52 UTC

Builders/Html builders/task.html

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<h2>Html builder</h2>
2+
<p><em>1.</em> Fill the table with the proper values from the product list. The products are declared in <code>data.kt</code>.</p>
3+
<p><em>2.</em> Color the table like a chess board (using getTitleColor() and getCellColor() functions above). Pass a color as an argument to the functions <code>tr</code>, <code>td</code>.</p>
4+
<p>Open the file <code>demo.kt</code> and run the main function there to see the rendered table.</p>

Builders/Html builders/test/tests.kt

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import org.junit.Assert
2+
import org.junit.Test
3+
4+
class TestHtmlBuilders {
5+
@Test fun productTableIsFilled() {
6+
val result = renderProductTable()
7+
Assert.assertTrue("Product table should contain the corresponding data", result.contains("cactus"))
8+
}
9+
10+
@Test fun productTableIsColored() {
11+
val result = renderProductTable()
12+
Assert.assertTrue("Product table should be colored", result.contains("bgcolor"))
13+
}
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import java.util.HashMap
2+
3+
fun <K, V> buildMap(build: HashMap<K, V>.() -> Unit): Map<K, V> {
4+
val map = HashMap<K, V>()
5+
map.build()
6+
return map
7+
}
8+
9+
fun usage(): Map<Int, String> {
10+
return buildMap {
11+
put(0, "0")
12+
for (i in 1..10) {
13+
put(i, "$i")
14+
}
15+
}
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
type: edu
2+
files:
3+
- name: src/Task.kt
4+
visible: true
5+
placeholders:
6+
- offset: 26
7+
length: 128
8+
placeholder_text: /* TODO */
9+
- name: test/tests.kt
10+
visible: false
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
id: 234757
2+
update_date: Thu, 15 Feb 2018 10:40:50 UTC
+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<h2>String and map builders</h2>
2+
<p>Extension function literals are very useful for creating builders, e.g.:</p><pre><code data-lang="text/x-kotlin">fun buildString(build: StringBuilder.() -&gt; Unit): String {
3+
val stringBuilder = StringBuilder()
4+
stringBuilder.build()
5+
return stringBuilder.toString()
6+
}
7+
8+
val s = buildString {
9+
this.append(&quot;Numbers: &quot;)
10+
for (i in 1..3) {
11+
// &#39;this&#39; can be omitted
12+
append(i)
13+
}
14+
}
15+
16+
s == &quot;Numbers: 123&quot;
17+
</code></pre>
18+
<p>Add and implement the function 'buildMap' with one parameter (of type extension function) creating a new HashMap, building it and returning it as a result. The usage of this function is shown below.</p>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import org.junit.Assert
2+
import org.junit.Test
3+
import koans.util.inEquals
4+
5+
class TestStringAndMapBuilders {
6+
@Test fun testBuildMap() {
7+
val map: Map<Int, String> = buildMap {
8+
put(0, "0")
9+
for (i in 1..10) {
10+
put(i, "$i")
11+
}
12+
}
13+
val expected = hashMapOf<Int, String>()
14+
for (i in 0..10) {
15+
expected[i] = "$i"
16+
}
17+
Assert.assertEquals("Map should be filled with the right values".inEquals(), expected, map)
18+
}
19+
}

0 commit comments

Comments
 (0)