***roman: refactor in the OOP way
This commit is contained in:
parent
b37f8a8d4c
commit
7d751cab3d
@ -2,25 +2,25 @@ package roman
|
|||||||
|
|
||||||
import "strings"
|
import "strings"
|
||||||
|
|
||||||
|
type RomanNumeral struct {
|
||||||
|
Value int
|
||||||
|
Symbol string
|
||||||
|
}
|
||||||
|
|
||||||
|
var allRomanNumerals = []RomanNumeral{
|
||||||
|
{10, "X"},
|
||||||
|
{9, "IX"},
|
||||||
|
{5, "V"},
|
||||||
|
{4, "IV"},
|
||||||
|
{1, "I"},
|
||||||
|
}
|
||||||
|
|
||||||
func ConvertToRoman(arabic int) string {
|
func ConvertToRoman(arabic int) string {
|
||||||
var converted strings.Builder
|
var converted strings.Builder
|
||||||
for arabic > 0 {
|
for _, numeral := range allRomanNumerals {
|
||||||
switch {
|
for arabic >= numeral.Value {
|
||||||
case arabic > 9:
|
converted.WriteString(numeral.Symbol)
|
||||||
converted.WriteString("X")
|
arabic -= numeral.Value
|
||||||
arabic -= 10
|
|
||||||
case arabic > 8:
|
|
||||||
converted.WriteString("IX")
|
|
||||||
arabic -= 9
|
|
||||||
case arabic > 4:
|
|
||||||
converted.WriteString("V")
|
|
||||||
arabic -= 5
|
|
||||||
case arabic > 3:
|
|
||||||
converted.WriteString("IV")
|
|
||||||
arabic -= 4
|
|
||||||
default:
|
|
||||||
converted.WriteString("I")
|
|
||||||
arabic -= 1
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user