forked from TheAlgorithms/Go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
romantoint.go
57 lines (52 loc) · 1.26 KB
/
romantoint.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// This algorithm will convert a standard roman number to an integer
// https://en.wikipedia.org/wiki/Roman_numerals
// Function receives a string as a roman number and outputs an integer
// Maximum output will be 3999
// Only standard form is supported
package conversion
import (
"errors"
"strings"
)
// numeral describes the value and symbol of a single roman numeral
type numeral struct {
val int
sym string
}
// lookup array for numeral values sorted by largest to smallest
var nums = []numeral{
{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"},
}
// RomanToInt converts a roman numeral string to an integer. Roman numerals for numbers
// outside the range 1 to 3,999 will return an error. Nil or empty string return 0
// with no error thrown.
func RomanToInt(input string) (int, error) {
if input == "" {
return 0, nil
}
var output int
for _, n := range nums {
for strings.HasPrefix(input, n.sym) {
output += n.val
input = input[len(n.sym):]
}
}
// if we are still left with input string values then the
// input was invalid and an error is returned.
if len(input) > 0 {
return 0, errors.New("invalid roman numeral")
}
return output, nil
}