Sha256: 7dec704183c3e48a9f1a4a940542bfee9b08510ced3ca09822d342628deb3545

Contents?: true

Size: 903 Bytes

Versions: 59

Compression:

Stored size: 903 Bytes

Contents

import Foundation

struct Grains {

    enum GrainsError: Error {
        case inputTooLow(String)
        case inputTooHigh(String)
    }

    static func square(_ num: Int) throws -> UInt64 {
        guard num >= 1 else {
            let message = "Input[\(num)] invalid. Input should be between 1 and 64 (inclusive)"
            throw GrainsError.inputTooLow(message)
        }

        guard num <= 64 else {
            let message = "Input[\(num)] invalid. Input should be between 1 and 64 (inclusive)"
            throw GrainsError.inputTooHigh(message)
        }

        let one: UInt64 = 1
        let shift = UInt64(num - 1)
        return one << shift
    }

    static var total: UInt64 {
        let numbers = (1...64).map { $0 }

        return numbers.reduce(UInt64(0)) {
            guard let squared = try? square($1) else {return $0}
            return $0 + squared
        }
    }
}

Version data entries

59 entries across 59 versions & 1 rubygems

Version Path
trackler-2.0.8.30 tracks/swift/exercises/grains/Sources/GrainsExample.swift
trackler-2.0.8.29 tracks/swift/exercises/grains/Sources/GrainsExample.swift
trackler-2.0.8.28 tracks/swift/exercises/grains/Sources/GrainsExample.swift
trackler-2.0.8.27 tracks/swift/exercises/grains/Sources/GrainsExample.swift
trackler-2.0.8.26 tracks/swift/exercises/grains/Sources/GrainsExample.swift
trackler-2.0.8.24 tracks/swift/exercises/grains/Sources/GrainsExample.swift
trackler-2.0.8.23 tracks/swift/exercises/grains/Sources/GrainsExample.swift
trackler-2.0.8.22 tracks/swift/exercises/grains/Sources/GrainsExample.swift
trackler-2.0.8.21 tracks/swift/exercises/grains/Sources/GrainsExample.swift
trackler-2.0.8.20 tracks/swift/exercises/grains/Sources/GrainsExample.swift
trackler-2.0.8.19 tracks/swift/exercises/grains/Sources/GrainsExample.swift
trackler-2.0.8.18 tracks/swift/exercises/grains/Sources/GrainsExample.swift
trackler-2.0.8.17 tracks/swift/exercises/grains/Sources/GrainsExample.swift
trackler-2.0.8.16 tracks/swift/exercises/grains/Sources/GrainsExample.swift
trackler-2.0.8.15 tracks/swift/exercises/grains/Sources/GrainsExample.swift
trackler-2.0.8.14 tracks/swift/exercises/grains/Sources/GrainsExample.swift
trackler-2.0.8.13 tracks/swift/exercises/grains/Sources/GrainsExample.swift
trackler-2.0.8.12 tracks/swift/exercises/grains/Sources/GrainsExample.swift
trackler-2.0.8.11 tracks/swift/exercises/grains/Sources/GrainsExample.swift
trackler-2.0.8.10 tracks/swift/exercises/grains/Sources/GrainsExample.swift