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.159 tracks/swift/exercises/luhn/Sources/LuhnExample.swift
trackler-2.2.1.158 tracks/swift/exercises/luhn/Sources/LuhnExample.swift
trackler-2.2.1.157 tracks/swift/exercises/luhn/Sources/LuhnExample.swift
trackler-2.2.1.156 tracks/swift/exercises/luhn/Sources/LuhnExample.swift
trackler-2.2.1.155 tracks/swift/exercises/luhn/Sources/LuhnExample.swift
trackler-2.2.1.154 tracks/swift/exercises/luhn/Sources/LuhnExample.swift
trackler-2.2.1.153 tracks/swift/exercises/luhn/Sources/LuhnExample.swift
trackler-2.2.1.152 tracks/swift/exercises/luhn/Sources/LuhnExample.swift
trackler-2.2.1.151 tracks/swift/exercises/luhn/Sources/LuhnExample.swift
trackler-2.2.1.150 tracks/swift/exercises/luhn/Sources/LuhnExample.swift
trackler-2.2.1.149 tracks/swift/exercises/luhn/Sources/LuhnExample.swift
trackler-2.2.1.148 tracks/swift/exercises/luhn/Sources/LuhnExample.swift
trackler-2.2.1.147 tracks/swift/exercises/luhn/Sources/LuhnExample.swift
trackler-2.2.1.146 tracks/swift/exercises/luhn/Sources/LuhnExample.swift
trackler-2.2.1.145 tracks/swift/exercises/luhn/Sources/LuhnExample.swift
trackler-2.2.1.144 tracks/swift/exercises/luhn/Sources/LuhnExample.swift
trackler-2.2.1.143 tracks/swift/exercises/luhn/Sources/LuhnExample.swift
trackler-2.2.1.142 tracks/swift/exercises/luhn/Sources/LuhnExample.swift
trackler-2.2.1.141 tracks/swift/exercises/luhn/Sources/LuhnExample.swift
trackler-2.2.1.140 tracks/swift/exercises/luhn/Sources/LuhnExample.swift