Sha256: aa6ea4dbd963b7f27054770fde1e706ac75d56028c4b8fc3e835c266ffbc07e3

Contents?: true

Size: 704 Bytes

Versions: 89

Compression:

Stored size: 704 Bytes

Contents

package romannumerals

import (
	"bytes"
	"fmt"
)

const testVersion = 3

type arabicToRoman struct {
	arabic int
	roman  string
}

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

	if input <= 0 || input >= 4000 {
		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

89 entries across 89 versions & 1 rubygems

Version Path
trackler-2.2.1.7 tracks/go/exercises/roman-numerals/example.go
trackler-2.2.1.6 tracks/go/exercises/roman-numerals/example.go
trackler-2.2.1.5 tracks/go/exercises/roman-numerals/example.go
trackler-2.2.1.4 tracks/go/exercises/roman-numerals/example.go
trackler-2.2.1.3 tracks/go/exercises/roman-numerals/example.go
trackler-2.2.1.2 tracks/go/exercises/roman-numerals/example.go
trackler-2.2.1.1 tracks/go/exercises/roman-numerals/example.go
trackler-2.2.1.0 tracks/go/exercises/roman-numerals/example.go
trackler-2.2.0.6 tracks/go/exercises/roman-numerals/example.go
trackler-2.2.0.5 tracks/go/exercises/roman-numerals/example.go
trackler-2.2.0.4 tracks/go/exercises/roman-numerals/example.go
trackler-2.2.0.3 tracks/go/exercises/roman-numerals/example.go
trackler-2.2.0.2 tracks/go/exercises/roman-numerals/example.go
trackler-2.2.0.1 tracks/go/exercises/roman-numerals/example.go
trackler-2.2.0.0 tracks/go/exercises/roman-numerals/example.go
trackler-2.1.0.55 tracks/go/exercises/roman-numerals/example.go
trackler-2.1.0.54 tracks/go/exercises/roman-numerals/example.go
trackler-2.1.0.53 tracks/go/exercises/roman-numerals/example.go
trackler-2.1.0.52 tracks/go/exercises/roman-numerals/example.go
trackler-2.1.0.51 tracks/go/exercises/roman-numerals/example.go