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.139 tracks/ceylon/exercises/largest-series-product/example/Series.ceylon
trackler-2.2.1.138 tracks/ceylon/exercises/largest-series-product/example/Series.ceylon
trackler-2.2.1.137 tracks/ceylon/exercises/largest-series-product/example/Series.ceylon
trackler-2.2.1.136 tracks/ceylon/exercises/largest-series-product/example/Series.ceylon
trackler-2.2.1.135 tracks/ceylon/exercises/largest-series-product/example/Series.ceylon
trackler-2.2.1.134 tracks/ceylon/exercises/largest-series-product/example/Series.ceylon
trackler-2.2.1.133 tracks/ceylon/exercises/largest-series-product/example/Series.ceylon
trackler-2.2.1.132 tracks/ceylon/exercises/largest-series-product/example/Series.ceylon
trackler-2.2.1.131 tracks/ceylon/exercises/largest-series-product/example/Series.ceylon
trackler-2.2.1.130 tracks/ceylon/exercises/largest-series-product/example/Series.ceylon
trackler-2.2.1.129 tracks/ceylon/exercises/largest-series-product/example/Series.ceylon
trackler-2.2.1.128 tracks/ceylon/exercises/largest-series-product/example/Series.ceylon
trackler-2.2.1.127 tracks/ceylon/exercises/largest-series-product/example/Series.ceylon
trackler-2.2.1.126 tracks/ceylon/exercises/largest-series-product/example/Series.ceylon
trackler-2.2.1.125 tracks/ceylon/exercises/largest-series-product/example/Series.ceylon
trackler-2.2.1.124 tracks/ceylon/exercises/largest-series-product/example/Series.ceylon
trackler-2.2.1.123 tracks/ceylon/exercises/largest-series-product/example/Series.ceylon
trackler-2.2.1.122 tracks/ceylon/exercises/largest-series-product/example/Series.ceylon
trackler-2.2.1.121 tracks/ceylon/exercises/largest-series-product/example/Series.ceylon
trackler-2.2.1.120 tracks/ceylon/exercises/largest-series-product/example/Series.ceylon