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.1.0.50 tracks/go/exercises/roman-numerals/example.go
trackler-2.1.0.49 tracks/go/exercises/roman-numerals/example.go
trackler-2.1.0.48 tracks/go/exercises/roman-numerals/example.go
trackler-2.1.0.47 tracks/go/exercises/roman-numerals/example.go
trackler-2.1.0.46 tracks/go/exercises/roman-numerals/example.go
trackler-2.1.0.45 tracks/go/exercises/roman-numerals/example.go
trackler-2.1.0.44 tracks/go/exercises/roman-numerals/example.go
trackler-2.1.0.43 tracks/go/exercises/roman-numerals/example.go
trackler-2.1.0.42 tracks/go/exercises/roman-numerals/example.go
trackler-2.1.0.41 tracks/go/exercises/roman-numerals/example.go
trackler-2.1.0.40 tracks/go/exercises/roman-numerals/example.go
trackler-2.1.0.39 tracks/go/exercises/roman-numerals/example.go
trackler-2.1.0.38 tracks/go/exercises/roman-numerals/example.go
trackler-2.1.0.37 tracks/go/exercises/roman-numerals/example.go
trackler-2.1.0.36 tracks/go/exercises/roman-numerals/example.go
trackler-2.1.0.34 tracks/go/exercises/roman-numerals/example.go
trackler-2.1.0.33 tracks/go/exercises/roman-numerals/example.go
trackler-2.1.0.32 tracks/go/exercises/roman-numerals/example.go
trackler-2.1.0.31 tracks/go/exercises/roman-numerals/example.go
trackler-2.1.0.30 tracks/go/exercises/roman-numerals/example.go