roman: add 5 and refactor
This commit is contained in:
parent
2515644d4f
commit
0a75d86850
@ -4,12 +4,19 @@ import "strings"
|
||||
|
||||
func ConvertToRoman(arabic int) string {
|
||||
var converted strings.Builder
|
||||
for i := arabic; i > 0; i-- {
|
||||
if i == 4 {
|
||||
for arabic > 0 {
|
||||
switch {
|
||||
case arabic > 4:
|
||||
converted.WriteString("V")
|
||||
arabic -= 5
|
||||
case arabic > 3:
|
||||
converted.WriteString("IV")
|
||||
break
|
||||
arabic -= 4
|
||||
default:
|
||||
converted.WriteString("I")
|
||||
arabic -= 1
|
||||
}
|
||||
converted.WriteString("I")
|
||||
}
|
||||
|
||||
return converted.String()
|
||||
}
|
||||
|
@ -12,6 +12,7 @@ func TestRomanNemerals(t *testing.T) {
|
||||
{"2 gets converted to II", 2, "II"},
|
||||
{"3 gets converted to III", 3, "III"},
|
||||
{"4 gets converted to IV", 4, "IV"},
|
||||
{"5 gets converted to V", 5, "V"},
|
||||
}
|
||||
for _, test := range cases {
|
||||
t.Run(test.Description, func(t *testing.T) {
|
||||
|
Loading…
Reference in New Issue
Block a user