Sha256: d0e1a4a68d507e35aad3d695d3bb9ee428abe7dd1e32ca8ad31c3564e0100442

Contents?: true

Size: 1.42 KB

Versions: 396

Compression:

Stored size: 1.42 KB

Contents

enum BaseError: Error {
    case invalidInputBase
    case invalidOutputBase
    case negativeDigit
    case invalidPositiveDigit
}

struct Base {

    static func outputDigits(inputBase: Int, inputDigits: [Int], outputBase: Int) throws -> [Int] {
        guard inputBase >= 2 else {
            throw BaseError.invalidInputBase
        }

        guard outputBase >= 2 else {
            throw BaseError.invalidOutputBase
        }

        let sum = try getSum(inputDigits, base: inputBase)
        let result = getDigits(sum, base: outputBase)

        return result
    }

    private static func getSum(_ digits: [Int], base: Int) throws -> Int {
        var multiplier = 1
        var sum = 0

        for digit in digits.reversed() {
            guard digit >= 0 else {
                throw BaseError.negativeDigit
            }
            guard digit < base else {
                throw BaseError.invalidPositiveDigit
            }

            sum += digit * multiplier
            multiplier *= base
        }

        return sum
    }

    private static func getDigits(_ sum: Int, base: Int) -> [Int] {
        var sum = sum
        var digits = [Int]()
        var multiplier = 1

        while sum > 0 {
            multiplier *= base
            let value = sum % multiplier
            let digit = value / (multiplier / base)
            digits.append(digit)
            sum -= value
        }

        return digits.reversed()
    }
}

Version data entries

396 entries across 396 versions & 1 rubygems

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