Sha256: 7616480908f3fb964702bfda9b9ed5b1fc53a3bab177aa52e5016b082a812401

Contents?: true

Size: 1.19 KB

Versions: 126

Compression:

Stored size: 1.19 KB

Contents

import java.lang.Math.max

class Series(private val digits: String) {

    init {
        require(digits.all { it.isDigit() })
    }

    fun getLargestProduct(span: Int): Long {
        require(span >= 0)
        require(digits.length >= span)

        if(digits.isEmpty()) {
            return 1
        }

        return calculateRec(digits.map { it.intValue() }, span, 0)
    }

    private tailrec fun calculateRec(digits: List<Int>, span: Int, maxSoFar: Long): Long {
        val currentProduct = digits.subList(0, span).prod()
        val newMax = max(currentProduct, maxSoFar)
        if(digits.size == span) { //since we won't be checking 'remainders' of the list, the end condition is simply when the size is the same as the span
            return newMax
        }

        return calculateRec(digits.tail(), span, newMax)
    }

}

fun Char.intValue() : Int  {
    if(!this.isDigit()) {
        throw IllegalArgumentException("Char ${this} is not a legal DEC character")
    }

    return this.intDiff('0')
}

fun Char.intDiff(other: Char) = this.toInt() - other.toInt()

fun List<Int>.prod() : Long = this.fold(1L) {productSoFar, item -> item*productSoFar}

fun <T> List<T>.tail() = this.subList(1, this.size)

Version data entries

126 entries across 126 versions & 1 rubygems

Version Path
trackler-2.2.1.180 tracks/kotlin/exercises/largest-series-product/.meta/src/reference/kotlin/Series.kt
trackler-2.2.1.179 tracks/kotlin/exercises/largest-series-product/.meta/src/reference/kotlin/Series.kt
trackler-2.2.1.178 tracks/kotlin/exercises/largest-series-product/.meta/src/reference/kotlin/Series.kt
trackler-2.2.1.177 tracks/kotlin/exercises/largest-series-product/.meta/src/reference/kotlin/Series.kt
trackler-2.2.1.176 tracks/kotlin/exercises/largest-series-product/.meta/src/reference/kotlin/Series.kt
trackler-2.2.1.175 tracks/kotlin/exercises/largest-series-product/.meta/src/reference/kotlin/Series.kt
trackler-2.2.1.174 tracks/kotlin/exercises/largest-series-product/.meta/src/reference/kotlin/Series.kt
trackler-2.2.1.173 tracks/kotlin/exercises/largest-series-product/.meta/src/reference/kotlin/Series.kt
trackler-2.2.1.172 tracks/kotlin/exercises/largest-series-product/.meta/src/reference/kotlin/Series.kt
trackler-2.2.1.171 tracks/kotlin/exercises/largest-series-product/.meta/src/reference/kotlin/Series.kt
trackler-2.2.1.170 tracks/kotlin/exercises/largest-series-product/.meta/src/reference/kotlin/Series.kt
trackler-2.2.1.169 tracks/kotlin/exercises/largest-series-product/.meta/src/reference/kotlin/Series.kt
trackler-2.2.1.167 tracks/kotlin/exercises/largest-series-product/.meta/src/reference/kotlin/Series.kt
trackler-2.2.1.166 tracks/kotlin/exercises/largest-series-product/.meta/src/reference/kotlin/Series.kt
trackler-2.2.1.165 tracks/kotlin/exercises/largest-series-product/.meta/src/reference/kotlin/Series.kt
trackler-2.2.1.164 tracks/kotlin/exercises/largest-series-product/.meta/src/reference/kotlin/Series.kt
trackler-2.2.1.163 tracks/kotlin/exercises/largest-series-product/.meta/src/reference/kotlin/Series.kt
trackler-2.2.1.162 tracks/kotlin/exercises/largest-series-product/.meta/src/reference/kotlin/Series.kt
trackler-2.2.1.161 tracks/kotlin/exercises/largest-series-product/.meta/src/reference/kotlin/Series.kt
trackler-2.2.1.160 tracks/kotlin/exercises/largest-series-product/.meta/src/reference/kotlin/Series.kt