-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbwt.kt
45 lines (32 loc) · 1.34 KB
/
bwt.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
fun main(args: Array<String>) {
when(args.get(0)) {
"-d", "--direct" -> println(args.get(1).bwTransform())
"-i", "--inverse" -> println(BwtResult(args.get(1), args.get(2).toInt()).bwTransformInv())
else -> println("-d --direct \t [text] - do BWT on text \n-i --inverse \t [text] [index] - get back text")
}
}
data class BwtResult(val string: String, val index: Int) {
fun bwTransformInv(): String{
val sorted = string.toList()
.mapIndexed { index, char -> Pair(char, index) }
.sortedBy { it.first }
fun compose(prefix: String, i: Int): String {
val res = sorted.get(i)
return if (res.second == index) prefix + res.first
else compose(prefix + res.first, res.second)
}
return compose("", index)
}
override fun toString(): String {
return "\'${string}\' ${index}"
}
}
fun String.bwTransform(): BwtResult {
val rotations = (0 .. this.length - 1)
.map { Pair(this.rotate(it), it) }
.sortedWith(compareBy { it.first })
val string = rotations.map { it.first.takeLast(1) }.joinToString(separator = "")
val index = rotations.indexOfFirst { it.second == 0 }
return BwtResult(string, index)
}
fun String.rotate(n: Int) = drop(n % length) + take(n % length)