# artemkorsakov's solution

## to Roman Numerals in the Go Track

Published at Feb 25 2019 · 0 comments
Instructions
Test suite
Solution

Write a function to convert from normal numbers to Roman Numerals.

The Romans were a clever bunch. They conquered most of Europe and ruled it for hundreds of years. They invented concrete and straight roads and even bikinis. One thing they never discovered though was the number zero. This made writing and dating extensive histories of their exploits slightly more challenging, but the system of numbers they came up with is still in use today. For example the BBC uses Roman numerals to date their programmes.

The Romans wrote numbers using letters - I, V, X, L, C, D, M. (notice these letters have lots of straight lines and are hence easy to hack into stone tablets).

`````` 1  => I
10  => X
7  => VII
``````

There is no need to be able to convert numbers larger than about 3000. (The Romans themselves didn't tend to go any higher)

Wikipedia says: Modern Roman numerals ... are written by expressing each digit separately starting with the left most digit and skipping any digit with a value of zero.

To see this in practice, consider the example of 1990.

In Roman numerals 1990 is MCMXC:

1000=M 900=CM 90=XC

2008 is written as MMVIII:

2000=MM 8=VIII

## Running the tests

To run the tests run the command `go test` from within the exercise directory.

If the test suite contains benchmarks, you can run these with the `--bench` and `--benchmem` flags:

``````go test -v --bench . --benchmem
``````

Keep in mind that each reviewer will run benchmarks on a different machine, with different specs, so the results from these benchmark tests may vary.

## Further information

For more detailed information about the Go track, including how to get help if you're having trouble, please visit the exercism.io Go language page.

## Source

The Roman Numeral Kata http://codingdojo.org/cgi-bin/index.pl?KataRomanNumerals

## Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

### cases_test.go

``````package romannumerals

// Source: exercism/problem-specifications
// Commit: 3c78ac4 Add test case for input value 49 to test double-normalization
// Problem Specifications Version: 1.2.0

type romanNumeralTest struct {
arabic   int
roman    string
hasError bool
}

var romanNumeralTests = []romanNumeralTest{
{1, "I", false},
{2, "II", false},
{3, "III", false},
{4, "IV", false},
{5, "V", false},
{6, "VI", false},
{9, "IX", false},
{27, "XXVII", false},
{48, "XLVIII", false},
{49, "XLIX", false},
{59, "LIX", false},
{93, "XCIII", false},
{141, "CXLI", false},
{163, "CLXIII", false},
{402, "CDII", false},
{575, "DLXXV", false},
{911, "CMXI", false},
{1024, "MXXIV", false},
{3000, "MMM", false},
}``````

### roman_numerals_test.go

``````package romannumerals

import "testing"

func TestRomanNumerals(t *testing.T) {
tc := append(romanNumeralTests, []romanNumeralTest{
{0, "", true},
{-1, "", true},
{3001, "", true},
}...)

for _, test := range tc {
actual, err := ToRomanNumeral(test.arabic)
if err == nil && test.hasError {
t.Errorf("ToRomanNumeral(%d) should return an error.", test.arabic)
continue
}
if err != nil && !test.hasError {
var _ error = err
t.Errorf("ToRomanNumeral(%d) should not return an error.", test.arabic)
continue
}
if !test.hasError && actual != test.roman {
t.Errorf("ToRomanNumeral(%d): %s, expected %s", test.arabic, actual, test.roman)
}
}
}

func BenchmarkRomanNumerals(b *testing.B) {
for i := 0; i < b.N; i++ {
for _, test := range romanNumeralTests {
ToRomanNumeral(test.arabic)
}
}
}``````
``````package romannumerals

import (
"errors"
"fmt"
"strings"
)

// RomanLetter is a letter from Roman alphabet
type RomanLetter int

// Match Roman and Arabic numerals
const (
I RomanLetter = 1
V             = 5
X             = 10
L             = 50
C             = 100
D             = 500
M             = 1000
)

func (rl RomanLetter) String() string {
switch rl {
case I:
return "I"
case V:
return "V"
case X:
return "X"
case L:
return "L"
case C:
return "C"
case D:
return "D"
case M:
return "M"
default:
return fmt.Sprintf("%d", int(rl))
}
}

var romanLetters = [7]RomanLetter{M, D, C, L, X, V, I}

// ErrRoman issued when an invalid number is passed
var ErrRoman = errors.New("the number must be greater than 0 and less than 3000")

// ToRomanNumeral translates from arabic numeral to roman numeral.
func ToRomanNumeral(number int) (string, error) {
if number < 1 || number > 3000 {
return "", ErrRoman
}
result := ""
for _, rl := range romanLetters {
str := ""
str, number = ToString(number, rl)
result += str
}
return result, nil
}

// ToString translates the given Arabic number to the specified Roman numeral and returns the remainder as an Arabic number.
func ToString(number int, romanLetter RomanLetter) (string, int) {
result := ""
count := number / int(romanLetter)
if count > 0 {
result += strings.Repeat(romanLetter.String(), count)
number = number - count*int(romanLetter)
}
diff := number - (int(romanLetter) - GetPreviousLetter(romanLetter))
if diff >= 0 {
prev := RomanLetter(GetPreviousLetter(romanLetter))
result += prev.String() + romanLetter.String()
number = diff
}
return result, number
}

// GetPreviousLetter returns previous roman letter for a given letter.
func GetPreviousLetter(romanLetter RomanLetter) int {
if int(romanLetter) >= 500 {
return 100
}
if int(romanLetter) >= 50 {
return 10
}
if int(romanLetter) >= 5 {
return 1
}
return 0
}``````