From 168b572a55defca3ea366e5e568774adcb74fa0f Mon Sep 17 00:00:00 2001 From: Branden Colen Date: Fri, 2 Apr 2021 11:47:47 -0500 Subject: [PATCH] not using buffer to save b/op --- README.md | 20 ++++++++++++++------ romannumeral.go | 6 +++--- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index d93374c..909a410 100644 --- a/README.md +++ b/README.md @@ -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 } ``` diff --git a/romannumeral.go b/romannumeral.go index 4b0d499..70663d1 100644 --- a/romannumeral.go +++ b/romannumeral.go @@ -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