Sums of Parts in Kotlin

The challenge

Let us consider this example (array written in general format):

ls = [0, 1, 3, 6, 10]

Its following parts:

ls = [0, 1, 3, 6, 10]
ls = [1, 3, 6, 10]
ls = [3, 6, 10]
ls = [6, 10]
ls = [10]
ls = []

The corresponding sums are (put together in a list): [20, 20, 19, 16, 10, 0]

The function parts_sums (or its variants in other languages) will take as parameter a list ls and return a list of the sums of its parts as defined above.

Other Examples:

ls = [1, 2, 3, 4, 5, 6]

// [21, 20, 18, 15, 11, 6, 0]
parts_sums(ls)

ls = [744125, 935, 407, 454, 430, 90, 144, 6710213, 889, 810, 2579358]

// [10037855, 9293730, 9292795, 9292388, 9291934, 9291504, 9291414, 9291270, 2581057, 2580168, 2579358, 0]
parts_sums(ls)

The solution in Kotlin

Option 1:

package sumofparts

fun sumParts(ls: IntArray): IntArray {
    val result = IntArray(ls.size + 1)
    for (i in ls.indices.reversed()) {
        result[i] = result[i + 1] + ls[i]
    }
    return result
}

Option 2:

package sumofparts

fun sumParts(ls: IntArray): IntArray {
    val result = IntArray(ls.size + 1) {0}
    ls.reverse()
    for (i in 1..ls.size) {
        result[i] = result[i - 1] + ls[i - 1]
    }
    result.reverse()
    return result
}

Option 3:

package sumofparts
import java.util.*

fun sumParts(ls: IntArray): IntArray {
    val totalSum = ls.sum()
    var currentSum = totalSum
    val link = LinkedList<Int>()
    ls.forEach {
        link.add(currentSum)
        currentSum -= it
    }
    link.addLast(0)
    return link.toIntArray()
}

Test cases to validate our solution

package sumofparts

import org.junit.Assert.*
import org.junit.Test
//import kotlin.test.assertEquals

class sumofpartsTest {
  fun dotest(ls: IntArray, expected: IntArray) {
    val actual: IntArray = sumParts(ls)
    assertArrayEquals(expected, actual)
  }
  @Test
  fun test() {
    dotest(intArrayOf(), intArrayOf(0));
    dotest(intArrayOf(0, 1, 3, 6, 10), intArrayOf(20, 20, 19, 16, 10, 0))
    dotest(intArrayOf(1, 2, 3, 4, 5, 6), intArrayOf(21, 20, 18, 15, 11, 6, 0))
    dotest(intArrayOf(744125, 935, 407, 454, 430, 90, 144, 6710213, 889, 810, 2579358),
        intArrayOf(10037855, 9293730, 9292795, 9292388, 9291934, 9291504, 9291414, 9291270, 2581057, 2580168, 2579358, 0))
    
  }
}
Tags:
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments