roman: fix the number range
This commit is contained in:
parent
650ec27ec9
commit
abc546178d
@ -3,7 +3,7 @@ package roman
|
||||
import "strings"
|
||||
|
||||
type RomanNumeral struct {
|
||||
Value int
|
||||
Value uint16
|
||||
Symbol string
|
||||
}
|
||||
|
||||
@ -24,7 +24,7 @@ var allRomanNumerals = []RomanNumeral{
|
||||
{1, "I"},
|
||||
}
|
||||
|
||||
func ConvertToRoman(arabic int) string {
|
||||
func ConvertToRoman(arabic uint16) string {
|
||||
var converted strings.Builder
|
||||
for _, numeral := range allRomanNumerals {
|
||||
for arabic >= numeral.Value {
|
||||
@ -36,8 +36,8 @@ func ConvertToRoman(arabic int) string {
|
||||
return converted.String()
|
||||
}
|
||||
|
||||
func ConvertToArabic(roman string) int {
|
||||
var converted int
|
||||
func ConvertToArabic(roman string) uint16 {
|
||||
var converted uint16
|
||||
for _, numeral := range allRomanNumerals {
|
||||
for strings.HasPrefix(roman, numeral.Symbol) {
|
||||
converted += numeral.Value
|
||||
|
@ -7,7 +7,7 @@ import (
|
||||
)
|
||||
|
||||
var cases = []struct {
|
||||
Arabic int
|
||||
Arabic uint16
|
||||
Roman string
|
||||
}{
|
||||
{Arabic: 1, Roman: "I"},
|
||||
@ -68,13 +68,18 @@ func TestConvertingToArabic(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestPropertiesOfConversion(t *testing.T) {
|
||||
assertion := func(arabic int) bool {
|
||||
assertion := func(arabic uint16) bool {
|
||||
if arabic > 3999 {
|
||||
return true
|
||||
}
|
||||
roman := ConvertToRoman(arabic)
|
||||
fromRoman := ConvertToArabic(roman)
|
||||
return fromRoman == arabic
|
||||
}
|
||||
|
||||
if err := quick.Check(assertion, nil); err != nil {
|
||||
if err := quick.Check(assertion, &quick.Config{
|
||||
MaxCount: 1000,
|
||||
}); err != nil {
|
||||
t.Error("failed checks", err)
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user