From 1d3e4f0944ea1a116da9290ea913de71d54a9778 Mon Sep 17 00:00:00 2001 From: Dimitri Sokolyuk Date: Tue, 13 Mar 2018 00:55:06 +0100 Subject: shrink --- roman.go | 24 ++++-------------------- roman_test.go | 19 +++---------------- 2 files changed, 7 insertions(+), 36 deletions(-) diff --git a/roman.go b/roman.go index f57e655..5759992 100644 --- a/roman.go +++ b/roman.go @@ -1,35 +1,19 @@ // Package roman numerals -// -// For fluff, the unicode overbar is recognized as a factor of 1000. -// -// If you see boxes in the code below, those are supposed to be the -// Unicode combining overline (U+0305) and look like I̅V̅X̅L̅C̅D̅M̅. Or, if -// you see overstruck combinations of letters, that's a different font -// rendering problem. (If you need roman numerals > 3999 reliably, it -// might best to stick to chiseling them in stone...) -// -// Source: https://www.rosettacode.org/wiki/Roman_numerals/Encode#Go package roman 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", "I̅V̅", "V̅", "V̅I̅", "V̅I̅I̅", "V̅I̅I̅I̅", "I̅X̅"} - m4 = []string{"", "X̅", "X̅X̅", "X̅X̅X̅", "X̅L̅", "L̅", "L̅X̅", "L̅X̅X̅", "L̅X̅X̅X̅", "X̅C̅"} - m5 = []string{"", "C̅", "C̅C̅", "C̅C̅C̅", "C̅D̅", "D̅", "D̅C̅", "D̅C̅C̅", "D̅C̅C̅C̅", "C̅M̅"} - m6 = []string{"", "M̅", "M̅M̅", "M̅M̅M̅"} + m3 = []string{"", "M", "MM", "MMM"} ) -// Roman represents a roman numeral in interval 0 < x < 4e6 +// Roman numeral in interval 0 < x < 4000 type Roman int func (n Roman) String() string { - if n <= 0 || n >= 4e6 { + if n <= 0 || n >= 4000 { return "" } - // this is efficient in Go. the seven operands are evaluated, - // then a single allocation is made of the exact size needed for the result. - return m6[n/1e6] + m5[n%1e6/1e5] + m4[n%1e5/1e4] + m3[n%1e4/1e3] + - m2[n%1e3/1e2] + m1[n%1e2/1e1] + m0[n%1e1] + return m3[n/1000] + m2[n%1000/100] + m1[n%100/10] + m0[n%10] } diff --git a/roman_test.go b/roman_test.go index 4ad62cf..a61436e 100644 --- a/roman_test.go +++ b/roman_test.go @@ -10,25 +10,12 @@ func TestRoman(t *testing.T) { s string }{ {0, ""}, - {1, "I"}, - {2, "II"}, - {3, "III"}, - {5, "V"}, - {8, "VIII"}, - {13, "XIII"}, - {21, "XXI"}, - {34, "XXXIV"}, - {55, "LV"}, - {89, "LXXXIX"}, - {100, "C"}, - {200, "CC"}, - {500, "D"}, + {1990, "MCMXC"}, + {2008, "MMVIII"}, + {1666, "MDCLXVI"}, {1e3 - 1, "CMXCIX"}, {2e3 - 1, "MCMXCIX"}, {4e3 - 1, "MMMCMXCIX"}, - {1e6 - 1, "C̅M̅X̅C̅I̅X̅CMXCIX"}, - {2e6 - 1, "M̅C̅M̅X̅C̅I̅X̅CMXCIX"}, - {4e6 - 1, "M̅M̅M̅C̅M̅X̅C̅I̅X̅CMXCIX"}, } for _, tc := range testCases { t.Run(tc.s, func(t *testing.T) { -- cgit v1.2.3