Sha256: d2f1a2d9972838209f440c437c3f82c42f5135b576e6f57fbe1cba48e3c86b5b

Contents?: true

Size: 905 Bytes

Versions: 266

Compression:

Stored size: 905 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

266 entries across 266 versions & 1 rubygems

Version Path
trackler-2.2.1.180 tracks/swift/exercises/grains/Sources/GrainsExample.swift
trackler-2.2.1.179 tracks/swift/exercises/grains/Sources/GrainsExample.swift
trackler-2.2.1.178 tracks/swift/exercises/grains/Sources/GrainsExample.swift
trackler-2.2.1.177 tracks/swift/exercises/grains/Sources/GrainsExample.swift
trackler-2.2.1.176 tracks/swift/exercises/grains/Sources/GrainsExample.swift
trackler-2.2.1.175 tracks/swift/exercises/grains/Sources/GrainsExample.swift
trackler-2.2.1.174 tracks/swift/exercises/grains/Sources/GrainsExample.swift
trackler-2.2.1.173 tracks/swift/exercises/grains/Sources/GrainsExample.swift
trackler-2.2.1.172 tracks/swift/exercises/grains/Sources/GrainsExample.swift
trackler-2.2.1.171 tracks/swift/exercises/grains/Sources/GrainsExample.swift
trackler-2.2.1.170 tracks/swift/exercises/grains/Sources/GrainsExample.swift
trackler-2.2.1.169 tracks/swift/exercises/grains/Sources/GrainsExample.swift
trackler-2.2.1.167 tracks/swift/exercises/grains/Sources/GrainsExample.swift
trackler-2.2.1.166 tracks/swift/exercises/grains/Sources/GrainsExample.swift
trackler-2.2.1.165 tracks/swift/exercises/grains/Sources/GrainsExample.swift
trackler-2.2.1.164 tracks/swift/exercises/grains/Sources/GrainsExample.swift
trackler-2.2.1.163 tracks/swift/exercises/grains/Sources/GrainsExample.swift
trackler-2.2.1.162 tracks/swift/exercises/grains/Sources/GrainsExample.swift
trackler-2.2.1.161 tracks/swift/exercises/grains/Sources/GrainsExample.swift
trackler-2.2.1.160 tracks/swift/exercises/grains/Sources/GrainsExample.swift