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.139 tracks/swift/exercises/luhn/Sources/LuhnExample.swift
trackler-2.2.1.138 tracks/swift/exercises/luhn/Sources/LuhnExample.swift
trackler-2.2.1.137 tracks/swift/exercises/luhn/Sources/LuhnExample.swift
trackler-2.2.1.136 tracks/swift/exercises/luhn/Sources/LuhnExample.swift
trackler-2.2.1.135 tracks/swift/exercises/luhn/Sources/LuhnExample.swift
trackler-2.2.1.134 tracks/swift/exercises/luhn/Sources/LuhnExample.swift
trackler-2.2.1.133 tracks/swift/exercises/luhn/Sources/LuhnExample.swift
trackler-2.2.1.132 tracks/swift/exercises/luhn/Sources/LuhnExample.swift
trackler-2.2.1.131 tracks/swift/exercises/luhn/Sources/LuhnExample.swift
trackler-2.2.1.130 tracks/swift/exercises/luhn/Sources/LuhnExample.swift
trackler-2.2.1.129 tracks/swift/exercises/luhn/Sources/LuhnExample.swift
trackler-2.2.1.128 tracks/swift/exercises/luhn/Sources/LuhnExample.swift
trackler-2.2.1.127 tracks/swift/exercises/luhn/Sources/LuhnExample.swift
trackler-2.2.1.126 tracks/swift/exercises/luhn/Sources/LuhnExample.swift
trackler-2.2.1.125 tracks/swift/exercises/luhn/Sources/LuhnExample.swift
trackler-2.2.1.124 tracks/swift/exercises/luhn/Sources/LuhnExample.swift
trackler-2.2.1.123 tracks/swift/exercises/luhn/Sources/LuhnExample.swift
trackler-2.2.1.122 tracks/swift/exercises/luhn/Sources/LuhnExample.swift
trackler-2.2.1.121 tracks/swift/exercises/luhn/Sources/LuhnExample.swift
trackler-2.2.1.120 tracks/swift/exercises/luhn/Sources/LuhnExample.swift