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