initial commit

This commit is contained in:
Branden
2021-02-07 15:09:13 -06:00
commit 60834482b2
6 changed files with 167 additions and 0 deletions
+16
View File
@@ -0,0 +1,16 @@
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib
# Test binary, built with `go test -c`
*.test
# Output of the go coverage tool, specifically when used with LiteIDE
*.out
# Dependency directories (remove the comment below to include it)
vendor/
.idea*
+1
View File
@@ -0,0 +1 @@
# Go Roman Numerals
+3
View File
@@ -0,0 +1,3 @@
module github.com/brandenc40/romannumeral
go 1.15
View File
+73
View File
@@ -0,0 +1,73 @@
package romannumeral
import (
"errors"
"strings"
)
type numeral struct {
val int
sym string
}
var (
InvalidRomanNumeral = errors.New("invalid roman numeral")
IntegerOutOfBounds = errors.New("integer must be between 1 and 3999")
)
var _numerals = []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"},
}
// FromInt converts an integer value to a roman numeral string. An error is
// returned if the integer is not between 1 and 3999.
func FromInt(input int) (string, error) {
if outOfBounds(input) {
return "", IntegerOutOfBounds
}
return intToRoman(input), nil
}
func outOfBounds(input int) bool {
return input < 1 || input > 3999
}
func intToRoman(input int) string {
var output string
for _, rom := range _numerals {
for input >= rom.val {
output += rom.sym
input -= rom.val
}
}
return output
}
// ToInt converts a roman numeral string to an integer. Roman numerals for numbers
// outside of the range 1 to 3999 will return an error.
func ToInt(input string) (int, error) {
if input == "" {
return 0, nil
}
if output, ok := romanToInt(input); ok {
return output, nil
}
return 0, InvalidRomanNumeral
}
func romanToInt(input string) (int, bool) {
var output int
for _, rom := range _numerals {
for strings.HasPrefix(input, rom.sym) {
output += rom.val
input = input[len(rom.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
}
+74
View File
@@ -0,0 +1,74 @@
package romannumeral
import (
"testing"
)
var testCases = map[string]int{
"I": 1, "II": 2,
"III": 3, "IV": 4,
"V": 5, "VI": 6,
"VII": 7, "VIII": 8,
"IX": 9, "X": 10,
"XIV": 14, "XIX": 19,
"XX": 20, "XXXIII": 33,
"XXXIV": 34, "XXXIX": 39,
"XLIX": 49, "L": 50,
"LXXXIX": 89, "XCIX": 99,
"CXLIX": 149, "CCCXLIX": 349,
"CDLVI": 456, "D": 500,
"DCIV": 604, "DCCLXXXIX": 789,
"DCCCXLIX": 849, "CMIV": 904,
"MVII": 1007, "MLXVI": 1066,
"MDCCLXXVI": 1776, "MMDCCCVI": 2806,
"MMCMXCIX": 2999, "MMXXI": 2021,
"MMMCMLXXIX": 3979, "MMMCMXCIX": 3999,
}
func TestFromInt(t *testing.T) {
for expected, input := range testCases {
out, err := FromInt(input)
if err != nil {
t.Errorf("FromInt(%d) returned an error %s", input, err.Error())
}
if out != expected {
t.Errorf("FromInt(%d) = %s; want %s", input, out, expected)
}
}
_, err := FromInt(100000)
if err == nil {
t.Errorf("FromInt(%d) expected an error", 100000)
}
_, err = FromInt(0)
if err == nil {
t.Errorf("FromInt(%d) expected an error", 0)
}
}
func TestToInteger(t *testing.T) {
for input, expected := range testCases {
out, err := ToInt(input)
if err != nil {
t.Errorf("ToInt(%s) returned an error %s", input, err.Error())
}
if out != expected {
t.Errorf("ToInt(%s) = %d; want %d", input, out, expected)
}
}
_, err := ToInt("IVCMXCIX")
if err == nil {
t.Errorf("ToInt(%d) expected an error", 0)
}
}
func BenchmarkToRomanNumeral(b *testing.B) {
for i := 0; i < b.N; i++ {
_, _ = FromInt(2999)
}
}
func BenchmarkToInteger(b *testing.B) {
for i := 0; i < b.N; i++ {
_, _ = ToInt("DCCCXLIX")
}
}