Sha256: 220d43341f8800f6ecfffd36ab34701f3efd71420d18f0c3f3f23561a1551395

Contents?: true

Size: 942 Bytes

Versions: 59

Compression:

Stored size: 942 Bytes

Contents

#if os(Linux)
    import Glibc
#elseif os(OSX)
    import Darwin
#endif

extension Int {

    init(_ value: Octal) {
        self = value.toDecimal
    }

}

struct Octal {
    private var stringValue = ""
    fileprivate var toDecimal: Int = 0
    private func isValidOctal() -> Bool {
        return (Int(stringValue) ?? -1) > -1 ? true : false
    }

    private func oct2int(_ input: String) -> Int {
        let orderedInput = Array(input.characters.reversed())
        var tempInt: Int = 0
        for (inx, each) in orderedInput.enumerated() {
            let tempCharInt = Int("\(each)") ?? 0
            if tempCharInt > 7 {return 0}
            let tempOctPower = Int(pow(Double(8), Double(inx)))
            tempInt += tempOctPower * tempCharInt
        }
        return tempInt
    }
    init( _ sv: String) {
        self.stringValue = sv

        if isValidOctal() {
            self.toDecimal = oct2int(sv)
        }

    }

}

Version data entries

59 entries across 59 versions & 1 rubygems

Version Path
trackler-2.0.8.30 tracks/swift/exercises/octal/Sources/OctalExample.swift
trackler-2.0.8.29 tracks/swift/exercises/octal/Sources/OctalExample.swift
trackler-2.0.8.28 tracks/swift/exercises/octal/Sources/OctalExample.swift
trackler-2.0.8.27 tracks/swift/exercises/octal/Sources/OctalExample.swift
trackler-2.0.8.26 tracks/swift/exercises/octal/Sources/OctalExample.swift
trackler-2.0.8.24 tracks/swift/exercises/octal/Sources/OctalExample.swift
trackler-2.0.8.23 tracks/swift/exercises/octal/Sources/OctalExample.swift
trackler-2.0.8.22 tracks/swift/exercises/octal/Sources/OctalExample.swift
trackler-2.0.8.21 tracks/swift/exercises/octal/Sources/OctalExample.swift
trackler-2.0.8.20 tracks/swift/exercises/octal/Sources/OctalExample.swift
trackler-2.0.8.19 tracks/swift/exercises/octal/Sources/OctalExample.swift
trackler-2.0.8.18 tracks/swift/exercises/octal/Sources/OctalExample.swift
trackler-2.0.8.17 tracks/swift/exercises/octal/Sources/OctalExample.swift
trackler-2.0.8.16 tracks/swift/exercises/octal/Sources/OctalExample.swift
trackler-2.0.8.15 tracks/swift/exercises/octal/Sources/OctalExample.swift
trackler-2.0.8.14 tracks/swift/exercises/octal/Sources/OctalExample.swift
trackler-2.0.8.13 tracks/swift/exercises/octal/Sources/OctalExample.swift
trackler-2.0.8.12 tracks/swift/exercises/octal/Sources/OctalExample.swift
trackler-2.0.8.11 tracks/swift/exercises/octal/Sources/OctalExample.swift
trackler-2.0.8.10 tracks/swift/exercises/octal/Sources/OctalExample.swift