Sha256: 165c0683c170d00e53f1c68df4c2edc3e5fbfb6a7f6b385a11617a91d59a9dde

Contents?: true

Size: 709 Bytes

Versions: 396

Compression:

Stored size: 709 Bytes

Contents

extension String {

    init(_ value: RomanNumeral) {
        self = value.romanNumeral
    }
}

struct RomanNumeral {

    var romanNumeral: String = ""

    private func toRomanNumerals(_ input: Int) -> String {
        var arabicToRoman = [1000: "M", 900: "CM", 500: "D", 400: "CD", 100: "C", 90: "XC", 50: "L", 40: "XL", 10: "X", 9: "IX", 5: "V", 4: "IV", 1: "I"]
        var i = input
        var s = ""
        for arabic in arabicToRoman.keys.sorted(by: > ) {
            while i >= arabic {
                s += arabicToRoman[arabic] ?? ""
                i -= arabic
            }
        }
        return s
    }

    init(_ value: Int) {

        self.romanNumeral = toRomanNumerals(value)
    }
}

Version data entries

396 entries across 396 versions & 1 rubygems

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