Skip to content

Instantly share code, notes, and snippets.

@haru01
Last active May 31, 2017 00:25
Show Gist options
  • Save haru01/f0814ac672b9d794e8db7830b0f968ea to your computer and use it in GitHub Desktop.
Save haru01/f0814ac672b9d794e8db7830b0f968ea to your computer and use it in GitHub Desktop.
// roman_test.go
import (
"fmt"
"testing"
)
func TestToRoman(t *testing.T) {
testCases := []struct {
in Arabian
want string
}{
{1, "I"},
{2, "II"},
{5, "V"},
}
for _, test := range testCases {
name := fmt.Sprintf("%v=>%vと変換できること", test.in, test.want)
t.Run(name, func(t *testing.T) {
if got := test.in.ToRoman(); got != test.want {
t.Errorf("Arabian(%v).ToRoman(): got %v want %v", test.in, got, test.want)
}
})
}
}
// roman.go
package roman
type Arabian uint16
func (in Arabian) ToRoman() string {
numbers := []struct {
arabian Arabian
roman string
}{
{5, "V"},
{1, "I"},
}
var out string
for remain := in; remain != 0; {
for _, n := range numbers {
if remain >= n.arabian {
out += n.roman
remain = remain - n.arabian
}
}
}
return out
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment