Sha256: 409a1da7d7aadb0ff25b9b707a6c6d0140b937c908989c937c4b98d5fabe9136

Contents?: true

Size: 1.16 KB

Versions: 396

Compression:

Stored size: 1.16 KB

Contents

struct NumberSeries {

    enum NumberSeriesError: Error {
        case invalidCharacter
        case spanLongerThanStringLength
        case negativeSpan
    }

    let numbers: [Int]

    init(_ numberString: String) throws {
        self.numbers = try numberString.characters.map {
            guard let intValue = Int(String($0)) else {
                throw NumberSeriesError.invalidCharacter
            }
            return intValue
        }
    }

    func largestProduct(_ numberOfDigits: Int) throws -> Int {
        guard numberOfDigits >= 0 else {
            throw NumberSeriesError.negativeSpan
        }

        let endIndex = numbers.count - numberOfDigits
        guard endIndex >= 0 else {
            throw NumberSeriesError.spanLongerThanStringLength
        }

        var result = 0

        for startIndex in 0 ... endIndex {
            let selectedNumbers = numbers[startIndex ..< startIndex + numberOfDigits]

            var total = 1

            for selectedNumber in selectedNumbers {
                total *= selectedNumber
            }

            if total > result {
                result = total
            }
        }

        return result
    }

}

Version data entries

396 entries across 396 versions & 1 rubygems

Version Path
trackler-2.0.6.40 tracks/swift/exercises/largest-series-product/Sources/LargestSeriesProductExample.swift
trackler-2.0.6.39 tracks/swift/exercises/largest-series-product/Sources/LargestSeriesProductExample.swift
trackler-2.0.6.38 tracks/swift/exercises/largest-series-product/Sources/LargestSeriesProductExample.swift
trackler-2.0.6.37 tracks/swift/exercises/largest-series-product/Sources/LargestSeriesProductExample.swift
trackler-2.0.6.36 tracks/swift/exercises/largest-series-product/Sources/LargestSeriesProductExample.swift
trackler-2.0.6.35 tracks/swift/exercises/largest-series-product/Sources/LargestSeriesProductExample.swift
trackler-2.0.6.34 tracks/swift/exercises/largest-series-product/Sources/LargestSeriesProductExample.swift
trackler-2.0.6.33 tracks/swift/exercises/largest-series-product/Sources/LargestSeriesProductExample.swift
trackler-2.0.6.32 tracks/swift/exercises/largest-series-product/Sources/LargestSeriesProductExample.swift
trackler-2.0.6.31 tracks/swift/exercises/largest-series-product/Sources/LargestSeriesProductExample.swift
trackler-2.0.6.30 tracks/swift/exercises/largest-series-product/Sources/LargestSeriesProductExample.swift
trackler-2.0.6.29 tracks/swift/exercises/largest-series-product/Sources/LargestSeriesProductExample.swift
trackler-2.0.6.28 tracks/swift/exercises/largest-series-product/Sources/LargestSeriesProductExample.swift
trackler-2.0.6.27 tracks/swift/exercises/largest-series-product/Sources/LargestSeriesProductExample.swift
trackler-2.0.6.26 tracks/swift/exercises/largest-series-product/Sources/LargestSeriesProductExample.swift
trackler-2.0.6.25 tracks/swift/exercises/largest-series-product/Sources/LargestSeriesProductExample.swift
trackler-2.0.6.24 tracks/swift/exercises/largest-series-product/Sources/LargestSeriesProductExample.swift
trackler-2.0.6.23 tracks/swift/exercises/largest-series-product/Sources/LargestSeriesProductExample.swift
trackler-2.0.6.22 tracks/swift/exercises/largest-series-product/Sources/LargestSeriesProductExample.swift
trackler-2.0.6.21 tracks/swift/exercises/largest-series-product/Sources/LargestSeriesProductExample.swift