not using buffer to save b/op

This commit is contained in:
Branden Colen
2021-04-02 11:47:47 -05:00
parent a814f4356c
commit 168b572a55
2 changed files with 17 additions and 9 deletions
+14 -6
View File
@@ -28,19 +28,27 @@ import (
rom "github.com/brandenc40/romannumeral"
)
func ExampleRomanToInt() {
integer, err := rom.ToInt("IV")
func ExampleStringToInt() {
integer, err := rom.StringToInt("IV")
if err != nil {
panic(err)
}
fmt.Println(integer == 4)
fmt.Println(integer == 4) // True
}
func ExampleIntToRoman() {
roman, err := rom.FromInt(4)
func ExampleBytesToInt() {
integer, err := rom.BytesToInt([]byte("IV"))
if err != nil {
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
View File
@@ -53,14 +53,14 @@ func outOfBounds(input int) bool {
}
func intToRoman(input int) []byte {
output := bytes.Buffer{}
var output []byte
for _, rom := range _numerals {
for input >= rom.val {
output.Write(rom.sym)
output = append(output, rom.sym...)
input -= rom.val
}
}
return output.Bytes()
return output
}
// StringToInt converts a roman numeral string to an integer. Roman numerals for numbers