mirror of
https://github.com/rwinkhart/convertroman.git
synced 2026-09-05 16:37:22 -04:00
not using buffer to save b/op
This commit is contained in:
@@ -28,19 +28,27 @@ import (
|
|||||||
rom "github.com/brandenc40/romannumeral"
|
rom "github.com/brandenc40/romannumeral"
|
||||||
)
|
)
|
||||||
|
|
||||||
func ExampleRomanToInt() {
|
func ExampleStringToInt() {
|
||||||
integer, err := rom.ToInt("IV")
|
integer, err := rom.StringToInt("IV")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
fmt.Println(integer == 4)
|
fmt.Println(integer == 4) // True
|
||||||
}
|
}
|
||||||
|
|
||||||
func ExampleIntToRoman() {
|
func ExampleBytesToInt() {
|
||||||
roman, err := rom.FromInt(4)
|
integer, err := rom.BytesToInt([]byte("IV"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
fmt.Println(roman == "IV")
|
fmt.Println(integer == 4) // True
|
||||||
|
}
|
||||||
|
|
||||||
|
func ExampleIntToString() {
|
||||||
|
roman, err := rom.IntToString(4)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
fmt.Println(roman == "IV") // True
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|||||||
+3
-3
@@ -53,14 +53,14 @@ func outOfBounds(input int) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func intToRoman(input int) []byte {
|
func intToRoman(input int) []byte {
|
||||||
output := bytes.Buffer{}
|
var output []byte
|
||||||
for _, rom := range _numerals {
|
for _, rom := range _numerals {
|
||||||
for input >= rom.val {
|
for input >= rom.val {
|
||||||
output.Write(rom.sym)
|
output = append(output, rom.sym...)
|
||||||
input -= rom.val
|
input -= rom.val
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return output.Bytes()
|
return output
|
||||||
}
|
}
|
||||||
|
|
||||||
// StringToInt converts a roman numeral string to an integer. Roman numerals for numbers
|
// StringToInt converts a roman numeral string to an integer. Roman numerals for numbers
|
||||||
|
|||||||
Reference in New Issue
Block a user