***roman: refactor in the OOP way
This commit is contained in:
parent
b37f8a8d4c
commit
7d751cab3d
@ -2,25 +2,25 @@ package roman
|
||||
|
||||
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 {
|
||||
var converted strings.Builder
|
||||
for arabic > 0 {
|
||||
switch {
|
||||
case arabic > 9:
|
||||
converted.WriteString("X")
|
||||
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
|
||||
for _, numeral := range allRomanNumerals {
|
||||
for arabic >= numeral.Value {
|
||||
converted.WriteString(numeral.Symbol)
|
||||
arabic -= numeral.Value
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user