-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathMixed References.kt
48 lines (38 loc) · 892 Bytes
/
Mixed References.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package chapter2.solutions
/**
* This program is intended to help solving the exercise
*
* We mainly have the program and print the state after every step
*/
fun main() {
val x = arrayOf(0, 1, 2, 3, 4)
x.printCurrentState()
x[3] = x[2]
x.printCurrentState()
x[4] = x[0]
x.printCurrentState()
x[2] = x[1]
x.printCurrentState()
x[1] = x[0]
x.printCurrentState()
x[0] = x[1]
x.printCurrentState()
x[4] = x[3]
x.printCurrentState()
x[3] = x[2]
x.printCurrentState()
x[2] = x[4]
x.printCurrentState("Solution: ")
}
private fun <T> Array<T>.printCurrentState(prefix: String? = null) {
if (prefix != null) {
println(prefix)
}
this.forEachIndexed { index, value ->
print("x[$index] = $value")
if (index < this.size - 1) {
print(" | ")
}
}
println()
}