Sha256: 2e4fd4811b9e015bea6b1d2fcfecae34c56d6d248057525c168b699b2c3c9e17

Contents?: true

Size: 919 Bytes

Versions: 62

Compression:

Stored size: 919 Bytes

Contents

abstract class Error() of InvalidWindowSize | InvalidDigit {}
class InvalidWindowSize() extends Error() {
    shared actual default Boolean equals(Object that) => that is InvalidWindowSize;
}
class InvalidDigit(Character c) extends Error() {
    shared actual default Boolean equals(Object that) {
        if (is InvalidDigit that) {
            return that.c == c;
        }
        return false;
    }
}

Integer|Error largestProduct(String digits, Integer window) {
    if (window<0 || window>digits.size) {
        return InvalidWindowSize();
    }
    value invalids = [for (d in digits) if (!('0'..'9').contains(d)) d];
    if (nonempty invalids) {
        return InvalidDigit(invalids[0]);
    }
    value parsed = [for (d in digits) d.integer - '0'.integer];
    value ranges = { for (i in 0 .. (digits.size - window)) parsed[i:window] };
    return max({ for (r in ranges) r.fold(1)((acc, e) => acc * e) });
}

Version data entries

62 entries across 62 versions & 1 rubygems

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