Sha256: 717477071d7771d5b89208aeddf269216f9ce58e9189d67b862336cb0fb933ce

Contents?: true

Size: 1.19 KB

Versions: 270

Compression:

Stored size: 1.19 KB

Contents

import java.lang.Math.max

class Series(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

270 entries across 270 versions & 1 rubygems

Version Path
trackler-2.1.0.34 tracks/kotlin/exercises/largest-series-product/src/example/kotlin/Series.kt
trackler-2.1.0.33 tracks/kotlin/exercises/largest-series-product/src/example/kotlin/Series.kt
trackler-2.1.0.32 tracks/kotlin/exercises/largest-series-product/src/example/kotlin/Series.kt
trackler-2.1.0.31 tracks/kotlin/exercises/largest-series-product/src/example/kotlin/Series.kt
trackler-2.1.0.30 tracks/kotlin/exercises/largest-series-product/src/example/kotlin/Series.kt
trackler-2.1.0.29 tracks/kotlin/exercises/largest-series-product/src/example/kotlin/Series.kt
trackler-2.1.0.28 tracks/kotlin/exercises/largest-series-product/src/example/kotlin/Series.kt
trackler-2.1.0.27 tracks/kotlin/exercises/largest-series-product/src/example/kotlin/Series.kt
trackler-2.1.0.26 tracks/kotlin/exercises/largest-series-product/src/example/kotlin/Series.kt
trackler-2.1.0.25 tracks/kotlin/exercises/largest-series-product/src/example/kotlin/Series.kt
trackler-2.1.0.24 tracks/kotlin/exercises/largest-series-product/src/example/kotlin/Series.kt
trackler-2.1.0.23 tracks/kotlin/exercises/largest-series-product/src/example/kotlin/Series.kt
trackler-2.1.0.22 tracks/kotlin/exercises/largest-series-product/src/example/kotlin/Series.kt
trackler-2.1.0.21 tracks/kotlin/exercises/largest-series-product/src/example/kotlin/Series.kt
trackler-2.1.0.20 tracks/kotlin/exercises/largest-series-product/src/example/kotlin/Series.kt
trackler-2.1.0.19 tracks/kotlin/exercises/largest-series-product/src/example/kotlin/Series.kt
trackler-2.1.0.18 tracks/kotlin/exercises/largest-series-product/src/example/kotlin/Series.kt
trackler-2.1.0.17 tracks/kotlin/exercises/largest-series-product/src/example/kotlin/Series.kt
trackler-2.1.0.16 tracks/kotlin/exercises/largest-series-product/src/example/kotlin/Series.kt
trackler-2.1.0.15 tracks/kotlin/exercises/largest-series-product/src/example/kotlin/Series.kt