iteration

This commit is contained in:
Michael Thomson 2024-05-25 00:50:51 -04:00
parent fbb4226a84
commit ef3c63d7f4
No known key found for this signature in database
2 changed files with 29 additions and 0 deletions

11
iteration/repeat.go Normal file
View File

@ -0,0 +1,11 @@
package iteration
const repeatCount = 5
func Repeat(character string) string {
var repeated string
for i := 0; i < repeatCount; i++ {
repeated += character
}
return repeated
}

18
iteration/repeat_test.go Normal file
View File

@ -0,0 +1,18 @@
package iteration
import "testing"
func TestRepeat(t *testing.T) {
repeated := Repeat("a")
expected := "aaaaa"
if repeated != expected {
t.Errorf("expected %q but got %q", expected, repeated)
}
}
func BenchmarkRepeat(b *testing.B) {
for i := 0; i < b.N; i++ {
Repeat("a")
}
}