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.119 tracks/kotlin/exercises/largest-series-product/.meta/src/reference/kotlin/Series.kt
trackler-2.2.1.118 tracks/kotlin/exercises/largest-series-product/.meta/src/reference/kotlin/Series.kt
trackler-2.2.1.117 tracks/kotlin/exercises/largest-series-product/.meta/src/reference/kotlin/Series.kt
trackler-2.2.1.116 tracks/kotlin/exercises/largest-series-product/.meta/src/reference/kotlin/Series.kt
trackler-2.2.1.115 tracks/kotlin/exercises/largest-series-product/.meta/src/reference/kotlin/Series.kt
trackler-2.2.1.114 tracks/kotlin/exercises/largest-series-product/.meta/src/reference/kotlin/Series.kt
trackler-2.2.1.113 tracks/kotlin/exercises/largest-series-product/.meta/src/reference/kotlin/Series.kt
trackler-2.2.1.111 tracks/kotlin/exercises/largest-series-product/.meta/src/reference/kotlin/Series.kt
trackler-2.2.1.110 tracks/kotlin/exercises/largest-series-product/.meta/src/reference/kotlin/Series.kt
trackler-2.2.1.109 tracks/kotlin/exercises/largest-series-product/.meta/src/reference/kotlin/Series.kt
trackler-2.2.1.108 tracks/kotlin/exercises/largest-series-product/.meta/src/reference/kotlin/Series.kt
trackler-2.2.1.107 tracks/kotlin/exercises/largest-series-product/.meta/src/reference/kotlin/Series.kt
trackler-2.2.1.106 tracks/kotlin/exercises/largest-series-product/.meta/src/reference/kotlin/Series.kt
trackler-2.2.1.105 tracks/kotlin/exercises/largest-series-product/.meta/src/reference/kotlin/Series.kt
trackler-2.2.1.104 tracks/kotlin/exercises/largest-series-product/.meta/src/reference/kotlin/Series.kt
trackler-2.2.1.103 tracks/kotlin/exercises/largest-series-product/.meta/src/reference/kotlin/Series.kt
trackler-2.2.1.102 tracks/kotlin/exercises/largest-series-product/.meta/src/reference/kotlin/Series.kt
trackler-2.2.1.101 tracks/kotlin/exercises/largest-series-product/.meta/src/reference/kotlin/Series.kt
trackler-2.2.1.100 tracks/kotlin/exercises/largest-series-product/.meta/src/reference/kotlin/Series.kt
trackler-2.2.1.99 tracks/kotlin/exercises/largest-series-product/.meta/src/reference/kotlin/Series.kt