parent
e7f7e71890
commit
9048bc281e
@ -0,0 +1,56 @@
|
||||
package inttoroman
|
||||
|
||||
import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
func intToRoman(num int) string {
|
||||
var result strings.Builder
|
||||
for num > 0 {
|
||||
if num >= 1000 {
|
||||
num = num - 1000
|
||||
result.WriteString("M")
|
||||
} else if num >= 900 {
|
||||
num = num - 900
|
||||
result.WriteString("CM")
|
||||
} else if num >= 500 {
|
||||
num = num - 500
|
||||
result.WriteString("D")
|
||||
} else if num >= 400 {
|
||||
num = num - 400
|
||||
result.WriteString("CD")
|
||||
} else if num >= 100 {
|
||||
num = num - 100
|
||||
result.WriteString("C")
|
||||
} else if num >= 90 {
|
||||
num = num - 90
|
||||
result.WriteString("XC")
|
||||
} else if num >= 50 {
|
||||
num = num - 50
|
||||
result.WriteString("L")
|
||||
} else if num >= 40 {
|
||||
num = num - 40
|
||||
result.WriteString("XL")
|
||||
} else if num >= 10 {
|
||||
num = num - 10
|
||||
result.WriteString("X")
|
||||
} else if num >= 9 {
|
||||
num = num - 9
|
||||
result.WriteString("IX")
|
||||
} else if num >= 5 {
|
||||
num = num - 5
|
||||
result.WriteString("V")
|
||||
} else if num >= 4 {
|
||||
num = num - 4
|
||||
result.WriteString("IV")
|
||||
} else if num >= 1 {
|
||||
num = num - 1
|
||||
result.WriteRune('I')
|
||||
}
|
||||
}
|
||||
return result.String()
|
||||
}
|
||||
|
||||
func Convert(num int) string {
|
||||
return intToRoman(num)
|
||||
}
|
@ -0,0 +1,68 @@
|
||||
package inttoroman_test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"git.jxs.me/leetgo/inttoroman"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestZero(t *testing.T) {
|
||||
require.Equal(t, "", inttoroman.Convert(0))
|
||||
}
|
||||
|
||||
func TestOne(t *testing.T) {
|
||||
require.Equal(t, "I", inttoroman.Convert(1))
|
||||
}
|
||||
|
||||
func TestTwo(t *testing.T) {
|
||||
require.Equal(t, "II", inttoroman.Convert(2))
|
||||
}
|
||||
|
||||
func TestFour(t *testing.T) {
|
||||
require.Equal(t, "IV", inttoroman.Convert(4))
|
||||
}
|
||||
|
||||
func TestFive(t *testing.T) {
|
||||
require.Equal(t, "V", inttoroman.Convert(5))
|
||||
}
|
||||
|
||||
func TestSix(t *testing.T) {
|
||||
require.Equal(t, "VI", inttoroman.Convert(6))
|
||||
}
|
||||
|
||||
func TestNine(t *testing.T) {
|
||||
require.Equal(t, "IX", inttoroman.Convert(9))
|
||||
}
|
||||
|
||||
func TestEleven(t *testing.T) {
|
||||
require.Equal(t, "XI", inttoroman.Convert(11))
|
||||
}
|
||||
|
||||
func TestIntToRoman(t *testing.T) {
|
||||
cases := map[string]int{
|
||||
"": 0,
|
||||
"I": 1,
|
||||
"II": 2,
|
||||
"IV": 4,
|
||||
"V": 5,
|
||||
"IX": 9,
|
||||
"X": 10,
|
||||
"XX": 20,
|
||||
"XII": 12,
|
||||
"XL": 40,
|
||||
"L": 50,
|
||||
"XC": 90,
|
||||
"C": 100,
|
||||
"LVIII": 58,
|
||||
"DCXIII": 613,
|
||||
"MCMXCIV": 1994,
|
||||
}
|
||||
|
||||
for roman, decimal := range cases {
|
||||
t.Run(fmt.Sprint(decimal), func(t *testing.T) {
|
||||
require.Equal(t, roman, inttoroman.Convert(decimal))
|
||||
})
|
||||
}
|
||||
}
|
@ -0,0 +1,23 @@
|
||||
12. Integer to Roman
|
||||
Medium
|
||||
|
||||
Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M.
|
||||
|
||||
Symbol Value
|
||||
I 1
|
||||
V 5
|
||||
X 10
|
||||
L 50
|
||||
C 100
|
||||
D 500
|
||||
M 1000
|
||||
|
||||
For example, 2 is written as II in Roman numeral, just two one's added together. 12 is written as XII, which is simply X + II. The number 27 is written as XXVII, which is XX + V + II.
|
||||
|
||||
Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII. Instead, the number four is written as IV. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX. There are six instances where subtraction is used:
|
||||
|
||||
I can be placed before V (5) and X (10) to make 4 and 9.
|
||||
X can be placed before L (50) and C (100) to make 40 and 90.
|
||||
C can be placed before D (500) and M (1000) to make 400 and 900.
|
||||
|
||||
Given an integer, convert it to a roman numeral.
|
Loading…
Reference in new issue