Sha256: 7cdc4f67615a0c8658ab81f6d41eb6bb646e16a78da74cff162fa2e0d8a7b5cd

Contents?: true

Size: 1.52 KB

Versions: 133

Compression:

Stored size: 1.52 KB

Contents

import Foundation

struct Luhn {

    var number: Int64 = 0
    var numberString: String = ""
    var addends: [Int] { return addendsFunc(numberString) }
    var checksum: Int { return addends.reduce(0, +) }
    var isValid: Bool = false

    init(_ num: String ) {
        self.numberString = num.replacingOccurrences(of: " ", with: "")
        let num = Int64(numberString)
        if self.numberString.utf16.count <= 1 {
            self.isValid = false
        } else if num == nil {
            isValid = false
        } else {
            self.number = num!
            isValid = checksum % 10 == 0
        }
    }

    func addendsFunc(_ num: String) -> [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
        }

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

}

Version data entries

133 entries across 133 versions & 1 rubygems

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