mirror of
https://github.com/cubixle/codekata-golang.git
synced 2026-04-24 20:04:43 +01:00
20201202
This commit is contained in:
32
20201202/main.go
Normal file
32
20201202/main.go
Normal file
@@ -0,0 +1,32 @@
|
||||
package main
|
||||
|
||||
var r2d = map[uint8]int{
|
||||
'I': 1,
|
||||
'V': 5,
|
||||
'X': 10,
|
||||
'L': 50,
|
||||
'C': 100,
|
||||
'D': 500,
|
||||
'M': 1000,
|
||||
}
|
||||
|
||||
func main() {
|
||||
|
||||
}
|
||||
|
||||
func RomaToDigit(s string) int {
|
||||
l := len(s)
|
||||
ret := 0
|
||||
for i := 0; i < l; i++ {
|
||||
c := r2d[s[i]]
|
||||
ret += c
|
||||
if i >= l-1 {
|
||||
break
|
||||
}
|
||||
cn := r2d[s[i+1]]
|
||||
if cn > c {
|
||||
ret -= 2 * c
|
||||
}
|
||||
}
|
||||
return ret
|
||||
}
|
||||
47
20201202/main_test.go
Normal file
47
20201202/main_test.go
Normal file
@@ -0,0 +1,47 @@
|
||||
package main
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestRomaToDigit(t *testing.T) {
|
||||
type args struct {
|
||||
s string
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
want int
|
||||
}{
|
||||
{
|
||||
"test1",
|
||||
args{"III"},
|
||||
3,
|
||||
},
|
||||
{
|
||||
"test2",
|
||||
args{"IV"},
|
||||
4,
|
||||
},
|
||||
{
|
||||
"test3",
|
||||
args{"VI"},
|
||||
6,
|
||||
},
|
||||
{
|
||||
"test4",
|
||||
args{"LVIII"},
|
||||
58,
|
||||
},
|
||||
{
|
||||
"test5",
|
||||
args{"MCMXCIV"},
|
||||
1994,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := RomaToDigit(tt.args.s); got != tt.want {
|
||||
t.Errorf("RomaToDigit() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user