summaryrefslogtreecommitdiff
path: root/go/roman-numerals/roman_numerals.go
diff options
context:
space:
mode:
authorDimitri Sokolyuk <demon@dim13.org>2016-08-28 02:36:40 +0200
committerDimitri Sokolyuk <demon@dim13.org>2016-08-28 02:36:40 +0200
commit6e5c7cbb5923f7627abc3b720600b6f1df718132 (patch)
tree9a210bd3782fa8db78d2d47029f14e4a375d158e /go/roman-numerals/roman_numerals.go
parentf43656022928caaf5a9882d93ffdf64eb069fbab (diff)
Solve roman
Diffstat (limited to 'go/roman-numerals/roman_numerals.go')
-rw-r--r--go/roman-numerals/roman_numerals.go19
1 files changed, 19 insertions, 0 deletions
diff --git a/go/roman-numerals/roman_numerals.go b/go/roman-numerals/roman_numerals.go
new file mode 100644
index 0000000..21727e2
--- /dev/null
+++ b/go/roman-numerals/roman_numerals.go
@@ -0,0 +1,19 @@
+package romannumerals
+
+import "errors"
+
+const testVersion = 2
+
+var (
+ m0 = []string{"", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"}
+ m1 = []string{"", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"}
+ m2 = []string{"", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"}
+ m3 = []string{"", "M", "MM", "MMM"}
+)
+
+func ToRomanNumeral(n int) (string, error) {
+ if n < 1 || n >= 4000 {
+ return "", errors.New("out of range")
+ }
+ return m3[n%1e4/1e3] + m2[n%1e3/1e2] + m1[n%1e2/1e1] + m0[n%1e1], nil
+}