Sha256: 01767f0b0ab8a95067d92c4de641ba1410bf93914e7dc101b38e71bcec1446d3

Contents?: true

Size: 681 Bytes

Versions: 122

Compression:

Stored size: 681 Bytes

Contents

package romannumerals

import (
	"bytes"
	"fmt"
)

type arabicToRoman struct {
	arabic int
	roman  string
}

func ToRomanNumeral(input int) (string, error) {
	buffer := bytes.NewBufferString("")

	if input <= 0 || input >= 3001 {
		return "", fmt.Errorf("the number %d is undefined in the roman numeral system", input)
	}

	mappings := []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"},
	}

	for _, m := range mappings {
		for input >= m.arabic {
			buffer.WriteString(m.roman)
			input -= m.arabic
		}
	}

	return buffer.String(), nil
}

Version data entries

122 entries across 122 versions & 1 rubygems

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