Sha256: 6f0282f5ca8f7ca7886a6115473cbe6fe88056732b3befad0dbc01c3dae6bf00

Contents?: true

Size: 1.68 KB

Versions: 59

Compression:

Stored size: 1.68 KB

Contents

import Foundation

struct Luhn {

    var number: Int64 = 0
    var addends: [Int] { return addendsFunc(number)}
    var checksum: Int {return addends.reduce(0, +) }
    var isValid: Bool { return checksum % 10 == 0}

    init(_ num: Int64) {
        self.number = num
    }

    static func create (_ num: Int64) -> Double {

        func createCheckDigit(_ value: Int) -> Int {
            let nearestTen = Int(ceil((Double(value) / 10.00)) * 10)
            return nearestTen - value
        }

        let zeroCheckDigitNumber = num * 10
        let luhn = Luhn(zeroCheckDigitNumber)

        if luhn.isValid {
            return Double(zeroCheckDigitNumber)}

        return Double((zeroCheckDigitNumber) + createCheckDigit(luhn.checksum))

    }

    func addendsFunc(_ num: Int64) -> [Int] {
        func oddIndexInt64Minus9( _ input: [Int]) -> [Int] {
            var input = input
            input = Array(input.reversed())
            var tempArray: [Int] = []
            for (inx, each) in input.enumerated() {
                var tempEach: Int = each
                if (inx+1) % 2 == 0 {
                    tempEach *= 2
                    if tempEach > 10 {
                        tempEach -= 9
                    }
                    tempArray.insert(tempEach, at: 0)
                } else {
                    tempArray.insert(tempEach, at: 0)
                }
            }
            return tempArray
        }
        func char2Int(_ input: Character) -> Int {
            let tempInt = Int(String(input)) ?? -1 // -1 = error
            return tempInt
        }

        let tempString = "\(num)"

        return oddIndexInt64Minus9(Array(tempString.characters).map {char2Int($0)})
    }

}

Version data entries

59 entries across 59 versions & 1 rubygems

Version Path
trackler-2.0.8.9 tracks/swift/exercises/luhn/Sources/LuhnExample.swift
trackler-2.0.8.8 tracks/swift/exercises/luhn/Sources/LuhnExample.swift
trackler-2.0.8.7 tracks/swift/exercises/luhn/Sources/LuhnExample.swift
trackler-2.0.8.6 tracks/swift/exercises/luhn/Sources/LuhnExample.swift
trackler-2.0.8.5 tracks/swift/exercises/luhn/Sources/LuhnExample.swift
trackler-2.0.8.4 tracks/swift/exercises/luhn/Sources/LuhnExample.swift
trackler-2.0.8.3 tracks/swift/exercises/luhn/Sources/LuhnExample.swift
trackler-2.0.8.2 tracks/swift/exercises/luhn/Sources/LuhnExample.swift
trackler-2.0.8.1 tracks/swift/exercises/luhn/Sources/LuhnExample.swift
trackler-2.0.7.0 tracks/swift/exercises/luhn/Sources/LuhnExample.swift
trackler-2.0.6.44 tracks/swift/exercises/luhn/Sources/LuhnExample.swift
trackler-2.0.6.43 tracks/swift/exercises/luhn/Sources/LuhnExample.swift
trackler-2.0.6.42 tracks/swift/exercises/luhn/Sources/LuhnExample.swift
trackler-2.0.6.41 tracks/swift/exercises/luhn/Sources/LuhnExample.swift
trackler-2.0.6.40 tracks/swift/exercises/luhn/Sources/LuhnExample.swift
trackler-2.0.6.39 tracks/swift/exercises/luhn/Sources/LuhnExample.swift
trackler-2.0.6.38 tracks/swift/exercises/luhn/Sources/LuhnExample.swift
trackler-2.0.6.37 tracks/swift/exercises/luhn/Sources/LuhnExample.swift
trackler-2.0.6.36 tracks/swift/exercises/luhn/Sources/LuhnExample.swift
trackler-2.0.6.35 tracks/swift/exercises/luhn/Sources/LuhnExample.swift