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.2.1.52 tracks/kotlin/exercises/largest-series-product/.meta/src/reference/kotlin/Series.kt
trackler-2.2.1.51 tracks/kotlin/exercises/largest-series-product/.meta/src/reference/kotlin/Series.kt
trackler-2.2.1.50 tracks/kotlin/exercises/largest-series-product/.meta/src/reference/kotlin/Series.kt
trackler-2.2.1.49 tracks/kotlin/exercises/largest-series-product/.meta/src/reference/kotlin/Series.kt
trackler-2.2.1.48 tracks/kotlin/exercises/largest-series-product/.meta/src/reference/kotlin/Series.kt
trackler-2.2.1.47 tracks/kotlin/exercises/largest-series-product/.meta/src/reference/kotlin/Series.kt
trackler-2.2.1.46 tracks/kotlin/exercises/largest-series-product/.meta/src/reference/kotlin/Series.kt
trackler-2.2.1.45 tracks/kotlin/exercises/largest-series-product/.meta/src/reference/kotlin/Series.kt
trackler-2.2.1.44 tracks/kotlin/exercises/largest-series-product/.meta/src/reference/kotlin/Series.kt
trackler-2.2.1.43 tracks/kotlin/exercises/largest-series-product/.meta/src/reference/kotlin/Series.kt
trackler-2.2.1.42 tracks/kotlin/exercises/largest-series-product/.meta/src/reference/kotlin/Series.kt
trackler-2.2.1.41 tracks/kotlin/exercises/largest-series-product/.meta/src/reference/kotlin/Series.kt
trackler-2.2.1.40 tracks/kotlin/exercises/largest-series-product/.meta/src/reference/kotlin/Series.kt
trackler-2.2.1.39 tracks/kotlin/exercises/largest-series-product/.meta/src/reference/kotlin/Series.kt
trackler-2.2.1.38 tracks/kotlin/exercises/largest-series-product/.meta/src/reference/kotlin/Series.kt
trackler-2.2.1.37 tracks/kotlin/exercises/largest-series-product/.meta/src/reference/kotlin/Series.kt
trackler-2.2.1.36 tracks/kotlin/exercises/largest-series-product/.meta/src/reference/kotlin/Series.kt
trackler-2.2.1.35 tracks/kotlin/exercises/largest-series-product/.meta/src/reference/kotlin/Series.kt
trackler-2.2.1.34 tracks/kotlin/exercises/largest-series-product/.meta/src/reference/kotlin/Series.kt
trackler-2.2.1.33 tracks/kotlin/exercises/largest-series-product/.meta/src/reference/kotlin/Series.kt