-
Notifications
You must be signed in to change notification settings - Fork 1
/
romannumeral.go
109 lines (97 loc) · 3.51 KB
/
romannumeral.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// Converts between integers and Roman Numeral strings.
//
// Currently only supports Roman Numerals without viniculum (1-3999) and will throw an error for
// numbers outside of that range. See here for details on viniculum:
// https://en.wikipedia.org/wiki/Roman_numerals#Large_numbers
package romannumeral
import (
"bytes"
"errors"
)
// numeral describes the value and symbol of a single roman numeral
type numeral struct {
val int
sym []byte
}
var (
// InvalidRomanNumeral - error for when a roman numeral string provided is not a valid roman numeral
InvalidRomanNumeral = errors.New("invalid roman numeral")
// IntegerOutOfBounds - error for when the integer provided is invalid and unable to be converted to a roman numeral
IntegerOutOfBounds = errors.New("integer must be between 1 and 3999")
// all unique numerals ordered from largest to smallest
nums = []numeral{
{1000, []byte("M")},
{900, []byte("CM")},
{500, []byte("D")},
{400, []byte("CD")},
{100, []byte("C")},
{90, []byte("XC")},
{50, []byte("L")},
{40, []byte("XL")},
{10, []byte("X")},
{9, []byte("IX")},
{5, []byte("V")},
{4, []byte("IV")},
{1, []byte("I")},
}
// lookup arrays used for converting from an int to a roman numeral extremely quickly.
// method from here: https://rosettacode.org/wiki/Roman_numerals/Encode#Go
r0 = []string{"", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"}
r1 = []string{"", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"}
r2 = []string{"", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"}
r3 = []string{"", "M", "MM", "MMM"}
)
// IntToString converts an integer value to a roman numeral string. An error is
// returned if the integer is not between 1 and 3999.
func IntToString(input int) (string, error) {
if outOfBounds(input) {
return "", IntegerOutOfBounds
}
return intToRoman(input), nil
}
// IntToBytes converts an integer value to a roman numeral byte array. An error is
// returned if the integer is not between 1 and 3999.
func IntToBytes(input int) ([]byte, error) {
str, err := IntToString(input)
return []byte(str), err
}
// outOfBounds checks to ensure an input value is valid for roman numerals without the need of
// vinculum (used for values of 4,000 and greater)
func outOfBounds(input int) bool {
return input < 1 || input > 3999
}
func intToRoman(n int) string {
// This is efficient in Go. The 4 operands are evaluated,
// then a single allocation is made of the exact size needed for the result.
return r3[n%1e4/1e3] + r2[n%1e3/1e2] + r1[n%100/10] + r0[n%10]
}
// StringToInt converts a roman numeral string to an integer. Roman numerals for numbers
// outside of the range 1 to 3,999 will return an error. Empty strings will return 0
// with no error thrown.
func StringToInt(input string) (int, error) {
return BytesToInt([]byte(input))
}
// BytesToInt converts a roman numeral byte array to an integer. Roman numerals for numbers
// outside of the range 1 to 3,999 will return an error. Nil or empty []byte will return 0
// with no error thrown.
func BytesToInt(input []byte) (int, error) {
if input == nil || len(input) == 0 {
return 0, nil
}
if output, ok := romanToInt(input); ok {
return output, nil
}
return 0, InvalidRomanNumeral
}
func romanToInt(input []byte) (int, bool) {
var output int
for _, n := range nums {
for bytes.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 the bool is returned as false
return output, len(input) == 0
}