Compare commits
14 Commits
7a32f26bd5
...
8629b537b0
Author | SHA1 | Date | |
---|---|---|---|
8629b537b0 | |||
abc546178d | |||
650ec27ec9 | |||
52825e49b3 | |||
715ea6477a | |||
6da23dc641 | |||
7d751cab3d | |||
b37f8a8d4c | |||
6f853c25d0 | |||
0a75d86850 | |||
2515644d4f | |||
3a0808c599 | |||
accb2c51dc | |||
5f93a0b843 |
58
roman/roman.go
Normal file
58
roman/roman.go
Normal file
@ -0,0 +1,58 @@
|
|||||||
|
package roman
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
type RomanNumeral struct {
|
||||||
|
Value uint16
|
||||||
|
Symbol string
|
||||||
|
}
|
||||||
|
|
||||||
|
var allRomanNumerals = []RomanNumeral{
|
||||||
|
{1000, "M"},
|
||||||
|
{900, "CM"},
|
||||||
|
{500, "D"},
|
||||||
|
{400, "CD"},
|
||||||
|
{900, "CM"},
|
||||||
|
{100, "C"},
|
||||||
|
{90, "XC"},
|
||||||
|
{50, "L"},
|
||||||
|
{40, "XL"},
|
||||||
|
{10, "X"},
|
||||||
|
{9, "IX"},
|
||||||
|
{5, "V"},
|
||||||
|
{4, "IV"},
|
||||||
|
{1, "I"},
|
||||||
|
}
|
||||||
|
|
||||||
|
var ErrNumberOutOfRange = errors.New("numbers should not be bigger than 3999")
|
||||||
|
|
||||||
|
func ConvertToRoman(arabic uint16) (string, error) {
|
||||||
|
if arabic > 3999 {
|
||||||
|
return "", ErrNumberOutOfRange
|
||||||
|
}
|
||||||
|
var converted strings.Builder
|
||||||
|
for _, numeral := range allRomanNumerals {
|
||||||
|
for arabic >= numeral.Value {
|
||||||
|
converted.WriteString(numeral.Symbol)
|
||||||
|
arabic -= numeral.Value
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return converted.String(), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// XXX: This convert function doesn't check the validity of the roman numerals string
|
||||||
|
func ConvertToArabic(roman string) uint16 {
|
||||||
|
var converted uint16
|
||||||
|
for _, numeral := range allRomanNumerals {
|
||||||
|
for strings.HasPrefix(roman, numeral.Symbol) {
|
||||||
|
converted += numeral.Value
|
||||||
|
// roman = roman[len(numeral.Symbol):]
|
||||||
|
roman = strings.TrimPrefix(roman, numeral.Symbol)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return converted
|
||||||
|
}
|
85
roman/roman_test.go
Normal file
85
roman/roman_test.go
Normal file
@ -0,0 +1,85 @@
|
|||||||
|
package roman
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"testing"
|
||||||
|
"testing/quick"
|
||||||
|
)
|
||||||
|
|
||||||
|
var cases = []struct {
|
||||||
|
Arabic uint16
|
||||||
|
Roman string
|
||||||
|
}{
|
||||||
|
{Arabic: 1, Roman: "I"},
|
||||||
|
{Arabic: 2, Roman: "II"},
|
||||||
|
{Arabic: 3, Roman: "III"},
|
||||||
|
{Arabic: 4, Roman: "IV"},
|
||||||
|
{Arabic: 5, Roman: "V"},
|
||||||
|
{Arabic: 6, Roman: "VI"},
|
||||||
|
{Arabic: 7, Roman: "VII"},
|
||||||
|
{Arabic: 8, Roman: "VIII"},
|
||||||
|
{Arabic: 9, Roman: "IX"},
|
||||||
|
{Arabic: 10, Roman: "X"},
|
||||||
|
{Arabic: 14, Roman: "XIV"},
|
||||||
|
{Arabic: 18, Roman: "XVIII"},
|
||||||
|
{Arabic: 20, Roman: "XX"},
|
||||||
|
{Arabic: 39, Roman: "XXXIX"},
|
||||||
|
{Arabic: 40, Roman: "XL"},
|
||||||
|
{Arabic: 47, Roman: "XLVII"},
|
||||||
|
{Arabic: 49, Roman: "XLIX"},
|
||||||
|
{Arabic: 50, Roman: "L"},
|
||||||
|
{Arabic: 100, Roman: "C"},
|
||||||
|
{Arabic: 90, Roman: "XC"},
|
||||||
|
{Arabic: 400, Roman: "CD"},
|
||||||
|
{Arabic: 500, Roman: "D"},
|
||||||
|
{Arabic: 900, Roman: "CM"},
|
||||||
|
{Arabic: 1000, Roman: "M"},
|
||||||
|
{Arabic: 1984, Roman: "MCMLXXXIV"},
|
||||||
|
{Arabic: 3999, Roman: "MMMCMXCIX"},
|
||||||
|
{Arabic: 2014, Roman: "MMXIV"},
|
||||||
|
{Arabic: 1006, Roman: "MVI"},
|
||||||
|
{Arabic: 798, Roman: "DCCXCVIII"},
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRomanNemerals(t *testing.T) {
|
||||||
|
for _, test := range cases {
|
||||||
|
t.Run(fmt.Sprintf("%d gets converted to %q", test.Arabic, test.Roman), func(t *testing.T) {
|
||||||
|
got, _ := ConvertToRoman(test.Arabic)
|
||||||
|
want := test.Roman
|
||||||
|
|
||||||
|
if got != want {
|
||||||
|
t.Errorf("got %q, want %q", got, want)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestConvertingToArabic(t *testing.T) {
|
||||||
|
for _, test := range cases {
|
||||||
|
t.Run(fmt.Sprintf("%q gets converted to %d", test.Roman, test.Arabic), func(t *testing.T) {
|
||||||
|
got := ConvertToArabic(test.Roman)
|
||||||
|
want := test.Arabic
|
||||||
|
|
||||||
|
if got != want {
|
||||||
|
t.Errorf("got %d, want %d", got, want)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPropertiesOfConversion(t *testing.T) {
|
||||||
|
assertion := func(arabic uint16) bool {
|
||||||
|
roman, err := ConvertToRoman(arabic)
|
||||||
|
if arabic > 3999 {
|
||||||
|
return err == ErrNumberOutOfRange
|
||||||
|
}
|
||||||
|
fromRoman := ConvertToArabic(roman)
|
||||||
|
return fromRoman == arabic
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := quick.Check(assertion, &quick.Config{
|
||||||
|
MaxCount: 1000,
|
||||||
|
}); err != nil {
|
||||||
|
t.Error("failed checks", err)
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user