mirror of
https://github.com/rwinkhart/convertroman.git
synced 2026-09-05 16:37:22 -04:00
cleaned up code
This commit is contained in:
+36
-41
@@ -10,46 +10,41 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
|
||||||
_maxRoman = 3999
|
|
||||||
_minRoman = 1
|
|
||||||
)
|
|
||||||
|
|
||||||
var (
|
|
||||||
InvalidRomanNumeral = errors.New("invalid roman numeral")
|
|
||||||
IntegerOutOfBounds = errors.New("integer must be between 1 and 3999")
|
|
||||||
)
|
|
||||||
|
|
||||||
// numeral describes the value and symbol of a single roman numeral
|
// numeral describes the value and symbol of a single roman numeral
|
||||||
type numeral struct {
|
type numeral struct {
|
||||||
val int
|
val int
|
||||||
sym []byte
|
sym []byte
|
||||||
}
|
}
|
||||||
|
|
||||||
// _numerals are all unique numerals ordered from largest to smallest
|
|
||||||
var _numerals = []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
|
|
||||||
var (
|
var (
|
||||||
m0 = []string{"", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"}
|
// InvalidRomanNumeral - error for when a roman numeral string provided is not a valid roman numeral
|
||||||
m1 = []string{"", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"}
|
InvalidRomanNumeral = errors.New("invalid roman numeral")
|
||||||
m2 = []string{"", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"}
|
// IntegerOutOfBounds - error for when the integer provided is invalid and unable to be converted to a roman numeral
|
||||||
m3 = []string{"", "M", "MM", "MMM"}
|
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
|
// IntToString converts an integer value to a roman numeral string. An error is
|
||||||
@@ -71,13 +66,13 @@ func IntToBytes(input int) ([]byte, error) {
|
|||||||
// outOfBounds checks to ensure an input value is valid for roman numerals without the need of
|
// 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)
|
// vinculum (used for values of 4,000 and greater)
|
||||||
func outOfBounds(input int) bool {
|
func outOfBounds(input int) bool {
|
||||||
return input < _minRoman || input > _maxRoman
|
return input < 1 || input > 3999
|
||||||
}
|
}
|
||||||
|
|
||||||
func intToRoman(n int) string {
|
func intToRoman(n int) string {
|
||||||
// This is efficient in Go. The 4 operands are evaluated,
|
// This is efficient in Go. The 4 operands are evaluated,
|
||||||
// then a single allocation is made of the exact size needed for the result.
|
// then a single allocation is made of the exact size needed for the result.
|
||||||
return m3[n%1e4/1e3] + m2[n%1e3/1e2] + m1[n%100/10] + m0[n%10]
|
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
|
// StringToInt converts a roman numeral string to an integer. Roman numerals for numbers
|
||||||
@@ -102,13 +97,13 @@ func BytesToInt(input []byte) (int, error) {
|
|||||||
|
|
||||||
func romanToInt(input []byte) (int, bool) {
|
func romanToInt(input []byte) (int, bool) {
|
||||||
var output int
|
var output int
|
||||||
for _, rom := range _numerals {
|
for _, n := range nums {
|
||||||
for bytes.HasPrefix(input, rom.sym) {
|
for bytes.HasPrefix(input, n.sym) {
|
||||||
output += rom.val
|
output += n.val
|
||||||
input = input[len(rom.sym):]
|
input = input[len(n.sym):]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// If we are still left with input string values then the
|
// if we are still left with input string values then the
|
||||||
// input was invalid and the bool is returned as False
|
// input was invalid and the bool is returned as false
|
||||||
return output, len(input) == 0
|
return output, len(input) == 0
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user