Add iteration package

This commit is contained in:
Fabian Becker 2022-01-03 17:17:02 +01:00
parent c37e1c26e1
commit cb26678c66
2 changed files with 48 additions and 0 deletions

16
iteration/repeat.go Normal file
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
iteration/repeat_test.go Normal file
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
}