From 60834482b2ae93f566b1f73bb8c4d4278ff582d6 Mon Sep 17 00:00:00 2001 From: Branden Date: Sun, 7 Feb 2021 15:09:13 -0600 Subject: [PATCH] initial commit --- .gitignore | 16 ++++++++++ README.md | 1 + go.mod | 3 ++ go.sum | 0 romannumeral.go | 73 +++++++++++++++++++++++++++++++++++++++++++ romannumeral_test.go | 74 ++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 167 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100644 go.mod create mode 100644 go.sum create mode 100644 romannumeral.go create mode 100644 romannumeral_test.go diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..8683efe --- /dev/null +++ b/.gitignore @@ -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* diff --git a/README.md b/README.md new file mode 100644 index 0000000..0f78b37 --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +# Go Roman Numerals diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..78a7687 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module github.com/brandenc40/romannumeral + +go 1.15 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..e69de29 diff --git a/romannumeral.go b/romannumeral.go new file mode 100644 index 0000000..b4e9557 --- /dev/null +++ b/romannumeral.go @@ -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 +} diff --git a/romannumeral_test.go b/romannumeral_test.go new file mode 100644 index 0000000..9e31b8d --- /dev/null +++ b/romannumeral_test.go @@ -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") + } +}