Divide list into two list - kotlin

Is there a simple way to divide list of Double into two lists of pairs in Kotlin?
In such way:
[x1, y1, x2, y2, x3, y3] => [(x1, x2), (x2, x3), (x3, x1)], [(y1, y2), (y2, y3), (y3, y1)]
I tried to use filterIndexed and zipWithNext
val x = filterIndexed { index, _ -> index % 2 == 0 }.zipWithNext()
val y = filterIndexed { index, _ -> index % 2 == 1 }.zipWithNext()
But the result is:
[x1, y1, x2, y2, x3, y3] => [(x1, x2), (x2, x3)], [(y1, y2), (y2, y3)]

If I understand correctly, the problem with the zipWithNext that you are using is that it doesn't "wrap around", i.e. output the final (x3, x1) or (y3, y1) pair, containing the last and first elements of the list.
You can fix this by simply declaring your own version of zipWithNext that does do this.
You can either do something like this:
fun <T> Iterable<T>.zipWithNextAndWrapAround(): List<Pair<T, T>> {
val zippedWithNext = zipWithNext()
if (zippedWithNext.isEmpty()) return zippedWithNext
return zippedWithNext + (zippedWithNext.last().second to zippedWithNext.first().first)
}
Or copy and paste over the original source code of zipWithNext and slightly modify it:
fun <T> Iterable<T>.zipWithNextAndWrapAround(): List<Pair<T, T>> {
val iterator = iterator()
if (!iterator.hasNext()) return emptyList()
val result = mutableListOf<Pair<T, T>>()
var current = iterator.next()
// remember what the first element was
val first = current
while (iterator.hasNext()) {
val next = iterator.next()
result.add(current to next)
current = next
}
// at last, add this pair
result.add(current to first)
return result
}
Usage:
val x = list.filterIndexed { index, _ -> index % 2 == 0 }.zipWithNextAndWrapAround()
val y = list.filterIndexed { index, _ -> index % 2 == 1 }.zipWithNextAndWrapAround()
Note that this is looping through the list twice. You can avoid that by writing your own version of partition called partitionIndexed.
The code could be something like:
inline fun <T> Iterable<T>.partitionIndexed(predicate: (Int, T) -> Boolean): Pair<List<T>, List<T>> {
val first = ArrayList<T>()
val second = ArrayList<T>()
forEachIndexed { index, element ->
if (predicate(index, element)) {
first.add(element)
} else {
second.add(element)
}
}
return Pair(first, second)
}
// usage:
val (x, y) = list.partitionIndexed { index, _ ->
index % 2 == 0
}.let { (a, b) ->
a.zipWithNextAndWrapAround() to b.zipWithNextAndWrapAround()
}

You could do something like this:
val lst = listOf(1, 2, 3, 4, 5, 6, 7, 8)
val intermediate = lst.chunked(2).map { it[0] to it[1] }.let { it + it[0] }
val x = intermediate.map { it.first }.zipWithNext()
val y = intermediate.map { it.second }.zipWithNext()
println(x) //[(1, 3), (3, 5), (5, 7), (7, 1)]
println(y) //[(2, 4), (4, 6), (6, 8), (8, 2)]

val input = listOf("x1", "y1", "x2", "y2", "x3", "y3")
val result = list
.withIndex()
.groupBy { it.index % 2 }
.map { entry -> entry.value.map { it.value } }
.map { (it + it[0]).zipWithNext() }
println(result)
Output:
[[(x1, x2), (x2, x3), (x3, x1)], [(y1, y2), (y2, y3), (y3, y1)]]

Related

Two sum in kotlin

Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.
Example 1:
Input: nums = [2,7,11,15], target = 9
Output: [0,1]
Explanation: Because nums[0] + nums[1] == 9, we return [0, 1].
Example 2:
Input: nums = [3,2,4], target = 6
Output: [1,2]
Example 3:
Input: nums = [3,3], target = 6
Output: [0,1]
I am adding piece of code.
fun main() {
// val nums = intArrayOf(2, 7, 11, 15)
// val target = 9
val nums = intArrayOf(3, 2, 4)
val target = 6
// val nums = intArrayOf(3, 3)
// val target = 6
twoSum(nums, target).forEach {
print(" $it")
}
}
fun twoSum(nums: IntArray, target: Int): IntArray {
val map = mutableMapOf<Int, Int>()
nums.forEachIndexed { index, i ->
map[i]?.let {
return intArrayOf(it, index)
}
map[target - i] = index
}
return intArrayOf()
}
My youtube link is described that I am debugging the code. My question is how
map[i]?.let {
return intArrayOf(it, index)
}
is going inside the 1st and 2nd iteration of return statment and it not going in 3rd iteration. Can anyone help me on this. Thanks

create objects based on elements of a list

Suppose I have the below lists and data classes in Kotlin:
val qrtr = listOf("Q1", "Q2", "Q3", "Q4")
data class X(val name: String, val location: String)
val x1 = X("John Doe", "USA")
val x2 = X("Jane Doe", "Singapore")
val allX = listOf(x1, x2)
data class Y(val title: String, val rating: Double)
val y1 = Y("Senior", 4.8)
val y2 = Y("Junior", 4.5)
val allY = listOf(y1, y2)
Is there an easy way to create another list using a 3rd data class using the above lists of allX, allY and qrtr.
data class Z(val x: X, val y: Y, quarter: String)
For the above data class (Z), I would like to merge details of allX and allY by its index position..and have this data repeated for each element in qrtr.
If it were to be done manually, it would look like this: But I'd like it done programmatically.
val z1 = Z(x1, y1, "Q1")
val z2 = Z(x1, y1, "Q2")
val z3 = Z(x1, y1, "Q3")
val z4 = Z(x1, y1, "Q4")
val z5 = Z(x2, y2, "Q1")
val z6 = Z(x2, y2, "Q2")
val z7 = Z(x2, y2, "Q3")
val z8 = Z(x2, y2, "Q4")
val allZ = listOf(z1, z2, z3, z4, z5, z6, z7, z8)
val result = allX.zip(allY).flatMap { (x, y) -> qrtr.map { q -> Z(x, y, q) } }
You can do this:
val allZ = allX.flatMapIndexed { index, x -> qrtr.map { q -> Z(x, allY[index], q) } }
allZ.forEach {
println(it)
}
Output:
Z(x=X(name=John Doe, location=USA), y=Y(title=Senior, rating=4.8), quarter=Q1)
Z(x=X(name=John Doe, location=USA), y=Y(title=Senior, rating=4.8), quarter=Q2)
Z(x=X(name=John Doe, location=USA), y=Y(title=Senior, rating=4.8), quarter=Q3)
Z(x=X(name=John Doe, location=USA), y=Y(title=Senior, rating=4.8), quarter=Q4)
Z(x=X(name=Jane Doe, location=Singapore), y=Y(title=Junior, rating=4.5), quarter=Q1)
Z(x=X(name=Jane Doe, location=Singapore), y=Y(title=Junior, rating=4.5), quarter=Q2)
Z(x=X(name=Jane Doe, location=Singapore), y=Y(title=Junior, rating=4.5), quarter=Q3)
Z(x=X(name=Jane Doe, location=Singapore), y=Y(title=Junior, rating=4.5), quarter=Q4)
Note that it will crash though if allX has more elements than allY

Kotlin filter String by List of indices

How do I filter a List of indices out of a String? Thanks.
val s = "abcdef"
val indices = listOf(1, 2, 3, 4)
val filtered = s.filterIndexed {i, _ -> i != /* ??? */ }
// outcome: filtered = "af"

Kotlin: How to convert list to map with list?

I have a list as below
{("a", 1), ("b", 2), ("c", 3), ("a", 4)}
I want to convert it to a map of list as below
{("a" (1, 4)), ("b", (2)), ("c", (3)))}
i.e. for a, we have a list of 1 and 4, since the key is the same.
The answer in
How to convert List to Map in Kotlin? only show unique value (instead of duplicate one like mine).
I tried associateBy in Kotlin
data class Combine(val alpha: String, val num: Int)
val list = arrayListOf(Combine("a", 1), Combine("b", 2), Combine("c", 3), Combine("a", 4))
val mapOfList = list.associateBy ( {it.alpha}, {it.num} )
println(mapOfList)
But doesn't seems to work. How could I do it in Kotlin?
Code
fun main(args: Array<String>) {
data class Combine(val alpha: String, val num: Int)
val list = arrayListOf(Combine("a", 1), Combine("b", 2), Combine("c", 3), Combine("a", 4))
val mapOfList = list.associateBy ( {it.alpha}, {it.num} )
println(mapOfList)
val changed = list
.groupBy ({ it.alpha }, {it.num})
println(changed)
}
Output
{a=4, b=2, c=3}
{a=[1, 4], b=[2], c=[3]}
How it works
First it takes the list
It groups the Combines by their alpha value to their num values
You may group the list by alpha first and then map the value to List<Int>:
data class Combine(val alpha: String, val num: Int)
val list = arrayListOf(Combine("a", 1), Combine("b", 2), Combine("c", 3), Combine("a", 4))
val mapOfList = list
.groupBy { it.alpha }
.mapValues { it.value.map { it.num } }
println(mapOfList)
Here's a slightly more concise version of Jacky Choi's solution.
It combines the grouping and the transforming into one call to groupBy().
val mapOfList = list
.groupBy (
keySelector = { it.name },
valueTransform = { it.num },
)

Product of a List or Array in Kotlin

I'm trying to find a way to get the product of a List or Array without using "repeat" or any loop on Kotlin but after some research I couldn't find anything similar.
Something like this in Python would be:
>>> reduce(lambda x, y: x*y, [1,2,3,4,5,6])
output: 720
You can use reduce in Kotlin.
From the doc:
Accumulates value starting with the first element and applying
operation from left to right to current accumulator value and each
element.
val list = listOf<Int>(1, 2, 3, 4, 5, 6)
val array = intArrayOf(1, 2, 3, 4, 5, 6)
list.reduce { acc, i -> acc * i } // returns 720
array.reduce { acc, i -> acc * i } // returns 720
An even simpler solution might be: (1..6).reduce(Int::times)
Use the fold function
val total = listOf(1, 2, 3, 4, 5).fold(1, { total, next -> total * next })
Hope this helps:
fun main(args: Array<String>){
val array = intArrayOf(1, 2, 3, 4, 5, 6)
val product = array.fold(1){acc, i -> acc * i}
println("The result is: $product")
}
This will output the product of the array.
Use the fold or reduce function. Both will work.
val array = arrayOf(1, 2, 3, 4, 5, 6)
println("Product of list: ${listOfMultiplication(array)}")
fun listOfMultiplication(array: Array<Int>): Int {
return array.reduce { total, next -> total * next }
}
val array = arrayOf(1, 2, 3, 4, 5, 6)
println("Product of list: ${listOfMultiplication(array)}")
fun listOfMultiplication(array: Array<Int>): Int {
return array.fold(1) { total, next -> total * next }
}