Add iteration package

This commit is contained in:
halfdan committed 2022-01-03 17:17:02 +01:00
1 parent c37e1c26e1
commit cb26678c66
2 files changed
+48

No files matched your search

+16
View File
@@ -0,0 +1,16 @@
package iteration
import "strings"
func Repeat(character string, count int) string {
var repeated string
for i := 0; i < count; i++ {
repeated += character
}
return repeated
}
func StringsRepeat(s string, count int) string {
return strings.Repeat(s, count)
}
+32
View File
@@ -0,0 +1,32 @@
package iteration
import (
"fmt"
"testing"
)
func TestRepeat(t *testing.T) {
repeated := Repeat("a", 5)
expected := "aaaaa"
if repeated != expected {
t.Errorf("expected %v, got %v", expected, repeated)
}
}
func BenchmarkRepeat(b *testing.B) {
for i := 0; i < b.N; i++ {
Repeat("a", 5)
}
}
func BenchmarkStringsRepeat(b *testing.B) {
for i := 0; i < b.N; i++ {
StringsRepeat("a", 5)
}
}
func ExampleRepeat() {
data := Repeat("a", 5)
fmt.Println(data)
// Outputs: aaaaa
}