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.159 tracks/kotlin/exercises/largest-series-product/.meta/src/reference/kotlin/Series.kt
trackler-2.2.1.158 tracks/kotlin/exercises/largest-series-product/.meta/src/reference/kotlin/Series.kt
trackler-2.2.1.157 tracks/kotlin/exercises/largest-series-product/.meta/src/reference/kotlin/Series.kt
trackler-2.2.1.156 tracks/kotlin/exercises/largest-series-product/.meta/src/reference/kotlin/Series.kt
trackler-2.2.1.155 tracks/kotlin/exercises/largest-series-product/.meta/src/reference/kotlin/Series.kt
trackler-2.2.1.154 tracks/kotlin/exercises/largest-series-product/.meta/src/reference/kotlin/Series.kt
trackler-2.2.1.153 tracks/kotlin/exercises/largest-series-product/.meta/src/reference/kotlin/Series.kt
trackler-2.2.1.152 tracks/kotlin/exercises/largest-series-product/.meta/src/reference/kotlin/Series.kt
trackler-2.2.1.151 tracks/kotlin/exercises/largest-series-product/.meta/src/reference/kotlin/Series.kt
trackler-2.2.1.150 tracks/kotlin/exercises/largest-series-product/.meta/src/reference/kotlin/Series.kt
trackler-2.2.1.149 tracks/kotlin/exercises/largest-series-product/.meta/src/reference/kotlin/Series.kt
trackler-2.2.1.148 tracks/kotlin/exercises/largest-series-product/.meta/src/reference/kotlin/Series.kt
trackler-2.2.1.147 tracks/kotlin/exercises/largest-series-product/.meta/src/reference/kotlin/Series.kt
trackler-2.2.1.146 tracks/kotlin/exercises/largest-series-product/.meta/src/reference/kotlin/Series.kt
trackler-2.2.1.145 tracks/kotlin/exercises/largest-series-product/.meta/src/reference/kotlin/Series.kt
trackler-2.2.1.144 tracks/kotlin/exercises/largest-series-product/.meta/src/reference/kotlin/Series.kt
trackler-2.2.1.143 tracks/kotlin/exercises/largest-series-product/.meta/src/reference/kotlin/Series.kt
trackler-2.2.1.142 tracks/kotlin/exercises/largest-series-product/.meta/src/reference/kotlin/Series.kt
trackler-2.2.1.141 tracks/kotlin/exercises/largest-series-product/.meta/src/reference/kotlin/Series.kt
trackler-2.2.1.140 tracks/kotlin/exercises/largest-series-product/.meta/src/reference/kotlin/Series.kt