Idiomatically group String (count consecutively repeated characters) - kotlin

How with what idioms do I achieve the desired effect?
val input = "aaaabbbcca"
val result = input.(here do the transformations)
val output = listOf("a" to 4, "b" to 3, "c" to 2, "a" to 1)
assert(result == output)

Here's a fun way to do it immutably using fold
fun main() {
val result = "aaaabbbcca"
.chunked(1)
.fold(emptyList<Pair<String, Int>>()) { list, current ->
val (prev, count) = list.lastOrNull() ?: Pair(current, 0)
if (prev == current) list.dropLast(1) + Pair(current, count + 1)
else list + Pair(current, 1)
}
val output = listOf("a" to 4, "b" to 3, "c" to 2, "a" to 1)
check(result == output)
println(result)
}
Output:
[(a, 4), (b, 3), (c, 2), (a, 1)]

This is a tricky little problem, and I can't find a particular idiomatic solution.
However, here's one that's quite concise:
val result = input.replace(Regex("(.)(?!\\1)(.)"), "$1§$2")
.split("§")
.map{ Pair(it[0], it.length) }
It uses a complicated little regex to insert a marker character (§ here, though of course it would work with any character that can't be in the input) between every pair of different characters.  ((?…) is a zero-width look-ahead assertion, so (?!\1) asserts that the next character is different from the previous one.  We need to include the next character in the match, otherwise it'll append a marker after the last character too.)
That gives aaaa§bbb§cc§a in this case.
We then split the string at the marker, giving a list of character groups (in this case "aaaa", "bbb", "cc", "a"), which it's easy to convert into (character,length) pairs.
Using a regex is not always a good solution, especially when it's complicated and unintuitive like this one.  So this might not be a good choice in production code.  On the other hand, a solution using fold() or reduce() probably wouldn't be that much easier to read, either.  In fact, the most maintainable solution might be the old-fashioned one of looping over the characters…

I believe there are no good (efficient, readable) idiomatic ways to solve this. We can use a good, old and boring loop approach. To make it at least a little more funny, we can do it with a lazy computing, utilizing coroutines:
fun String.countConsecutive() = sequence {
if (isEmpty()) return#sequence
val it = iterator()
var curr = it.next()
var count = 1
it.forEach {
if (curr == it) {
count++
} else {
yield(curr.toString() to count)
curr = it
count = 1
}
}
yield(curr.toString() to count)
}
This is good if our string is very long and we only need to iterate over consecutive groups. Even better if we don't need to iterate over all of them.

Related

Removing a loop to make code run faster (Kotlin) (Big O)

I'm trying a leetcode challenge and am struggling to pass the challenge due to the speed of my code:
class Solution {
fun longestPalindrome(s: String): String {
var longestPal = ""
var substring = ""
for (i in 0..s.length) {
for (j in i + 1..s.length) {
substring = s.substring(i, j)
if (substring == substring.reversed() && substring.length > longestPal.length) {
longestPal = substring
}
}
}
return longestPal
}
}
I'm a newb and not familiar with Big O notation.
I imagine if I could use just one loop I would be able to speed this code up significantly but am not sure how I would go about this.
(Not saying this is the best approach, but that is a start)
Palindromes can only be found between two same letters. So one idea is to go through the string once, and keep track of letter indexes. When you encounter a letter you already saw before, and the difference in indexes is longer than the current longest palindrome, you check for a palindrome:
fun longestPal(s: String): String {
val letters = mutableMapOf<Char, MutableList<Int>>()
var longest = ""
s.indices.forEach { index ->
val indicesForCurrentChar = letters[s[index]] ?: mutableListOf()
for (i in indicesForCurrentChar) {
if ((index - i) < longest.length) break // (1) won't be the longest anyway
val sub = s.substring(i, index + 1)
if (sub == sub.reversed()) longest = sub
}
indicesForCurrentChar.add(index)
letters[s[index]] = indicesForCurrentChar
}
return longest
}
What is costly here is the palindrome check itself (sub == sub.reversed). But the check in (1) should contain it (think of a string which is the repetition of the same letter).
I would be curious to know what other suggest online.
Your code runs in O(n^3) time, a loop within a loop within a loop, because that reversed() call iterates up to the size of the input string. You can look up Manacher's algorithm for an explanation of how to do it in linear time (O(n)), no nested iteration at all.

Write a kotlin program that prints the number that is repeated the most in a consecutive way

I'm kind of stuck, I don't know how to make the second loop to start 1 position above the first loop in Kotlin.
I have an array (named myArray) with 10 elements, I need to Write a Kotlin program that prints the number that has the most consecutive repeated number in the array and also prints the number of times it appears in the sequence.
The program must parse the array from left to right so that if two numbers meet the condition, the one that appears first from left to right will be printed.
Longest: 3
Number: 8
fun main() {
val myArray: IntArray = intArrayOf(1,2,2,4,5,6,7,8,8,8)
for((index , value) in myArray.withIndex()){
var inx = index + 1
var count = 0
var longest = 0
var number = 0
for((inx,element) in myArray.withIndex()) {
if(value == element ){
count+=
}
}
if(longest < count){
longest = count
number = value
}
}
}
I'm against just dropping answers, but it is quite late for me, so I'll leave this answer here and edit it tomorrow with more info on how each part works. I hope that maybe in the meanwhile it will help you to gain some idea to where you might be going wrong.
val results = mutableMapOf<Int, Int>()
(0..myArray.size - 2).forEach { index ->
val current = myArray[index]
if (current == myArray[index + 1]) {
results[current] = (results[current] ?: 1) + 1
}
}
val (max, occurrences) = results.maxByOrNull { it.value } ?: run { println("No multiple occurrences"); return }
println("Most common consecutive number $max, with $occurrences occurrences")
Alternatively if the intArray would be a list, or if we allowed to change it to a list myArray.toList(), you could replace the whole forEach loop with a zipWithNext. But I'm pretty sure that this is a HW question, so I doubt this is the expected way of solving it.
myList.zipWithNext { a, b ->
if (a == b) results[a] = (results[a] ?: 1) + 1
}

In Kotlin, can I have two random values with the second one omitting the first random number?

Here is what I am trying to say:
val firstNumbers = (1..69).random()
val secondNumbers = (1..69).random()
I would like the secondNumbers to omit the random number picked in firstNumbers
If you're just generating two numbers, what you could do is lower the upper bound for secondNumbers down to 68, then add 1 if it's greater than or equal to the first number. This will ensure an even distribution:
val firstNumber = (1..69).random()
var secondNumber = (1..68).random()
if (secondNumber >= firstNumber) {
secondNumber += 1
}
For generating more than 2 numbers, the following code should work:
fun randoms(bound: Int, n: Int): List<Int> {
val mappings = mutableMapOf<Int, Int>()
val ret = mutableListOf<Int>()
for (i in 0 until n) {
val num = (1..(bound - i)).random()
ret.add(mappings.getOrDefault(num, num))
mappings.put(num, mappings.getOrDefault(bound - i, bound - i))
}
return ret
}
It tries to emulate Fisher-Yates shuffling while only keeping track of swaps that happened, thus greatly reducing memory usage when n is much less than bound. If n is very close to bound, then the answer by #lukas.j is much cleaner to use and probably also faster.
It can be used like so:
randoms(69, 6) // might return [17, 36, 60, 48, 69, 21]
(I'd encourage people to double-check the uniformity and correctness of the algorithm, but it seems good to me)
random() is the wrong approach, rather use shuffled() and then take the first two elements from the list with take(). And it is a oneliner:
val (firstNumber, secondNumber) = (1..69).shuffled().take(2)
println(firstNumber)
println(secondNumber)
Another approach could be to find one number in range 1..69, remove that number from the range and find the second one.
val first = (1..69).random()
val second = ((1..69) - first).random()
Edit: As per your comment, you want 6 different numbers within this range. You can do that like this.
val values = (1..69).toMutableList()
val newList = List(6) {
values.random().also { values.remove(it) }
}

How I can return IntArray with reduce or fold?

I have the following data for my task:
Input: nums = [1,2,3,4]
Output: [1,3,6,10]
Explanation: Running sum is obtained as follows: [1, 1+2, 1+2+3, 1+2+3+4].
As u see, I need to return IntArray, the first thing I used was runningReduce() , but this function is used in the version of Kotlin 1.4.30.
fun runningSum(nums: IntArray): IntArray {
return nums.runningReduce { sum, element -> sum + element }.toIntArray()
}
Yes, this solution works, but how can I solve the same problem using reduce() or fold()?
Try the following:
nums.fold(listOf(0)) { acc, i -> acc + (acc.last() + i) }.drop(1).toIntArray()
The solution is sub-optimal though: it copies the list in each iteration. But looks fancy.
To avoid copying, you could write it as follows:
nums.fold(mutableListOf(0)) { acc, i -> acc += (acc.last() + i); acc }.drop(1).toIntArray()
I think I prefer the first version, the second one is not pure from functional perspective.
Mafor is right, fold() is not a good choice when producing a collection because it copies the collection every time so you need to workaround with mutable collections, which defeats the point of the functional style.
If you really want to work with arrays, which are mutable, doing it the old fashioned procedural way may be best:
val array = intArrayOf(1, 2, 3, 4)
for (i in array.indices.drop(1)) {
array[i] += array[i - 1]
}
println(array.joinToString(", "))
Here's a slightly modified version of Mafor's answer that gives you an IntArray and avoids the use of multiple statements - it still uses the mutable list though:
val input = intArrayOf(1, 2, 3, 4)
val output = input.fold(mutableListOf(0)) { acc, cur ->
acc.apply { add(last() + cur) }
}.drop(1).toIntArray()
println(output.joinToString(", "))
Both of these print 1, 3, 6, 10.

How can I get the coefficients from a polynomial expression?

At the input I get a polynomial as a string, for example "7x^4+3x^3-6x^2+x-8". I want to get its coefficients in variables but I have no idea on how to do this. Maximum degree is not known, coefficients are integers. Also terms of some degree can be absent. I will be very grateful for any help.
I tried to split by "+" and "-" and then by "x^" but I have trouble with x, the term with (unwritten) degree 1.
Also I have tried firstly split by "x" then by "^" and handled exception with "-" but I don't know how to handle exception with missing degrees.
private fun koef(text: String) : List<Int> {
val vars = text.split("x")
val koefList = mutableListOf<Int>()
var count = 1
vars.forEach {
if (it == "-") koefList.add(-1)
else {
if (it[0] == '^')
}
}
return koefList
}
Here's one implementation.
It's slightly more general, allowing the terms to be in any order, and to have surrounding whitespace.  But it still assumes that the polynomial is valid, that the powers are non-negative and all different, and that there's at least one term.
You didn't specify the order of coefficients, so this returns them in increasing power (starting with that of x^0, then x^1, &c).
private fun coeffs(polynomial: String): List<Int> {
val terms = polynomial.split(Regex("(?=[+-])")).associate{ term ->
val s = term.split(Regex("x\\^?"))
val coeff = s[0].replace(" ", "")
.let{ when (it){ "", "+" -> 1; "-" -> -1; else -> it.toInt() }}
val power = s.getOrNull(1)?.trim()
.let{ when (it){ null -> 0; "" -> 1; else -> it.toInt() }}
power to coeff
}
val highestPower = terms.keys.max()!!
return (0..highestPower).map{ terms[it] ?: 0 }
}
Sample results:
coeffs("x^2+2x-1") = [-1, 2, 1]
coeffs("2x^3 - 3x^4 - x + 4") = [4, -1, 0, 2, -3]
coeffs("x") = [0, 1]
coeffs("-2") = [-2]
It starts by splitting the string into terms.  ((?=[+-]) is a lookahead, which matches an empty string if it's followed by + or -.  Full documentation on Java/Kotlin regular expressions is here.)
It then splits each term into a coefficient and power, converts them to numbers, and creates a Map from term to coefficient.  (That's quite awkward, as it has to handle several special cases where the numbers and/or signs are missing.)  Using a map handles missing powers (and also powers that aren't in order).
Finally, it finds the largest power, and converts the map to a list of coefficients in increasing powers, filling in 0 for missing powers.
I've kept the code short, to show the principle.  If it were to be used in production, you should probably make it safer and more efficient, e.g. by checking for invalid input such as empty string, invalid characters, or duplicate powers; and by putting the Regexs in properties so that they don't have to be recreated each time.  Some unit tests wouldn't be a bad thing, either!