iteration: add repeatTime as an argument

This commit is contained in:
vinchent 2024-09-10 13:32:46 +02:00
parent 2684521bdd
commit 75304d4c23
2 changed files with 4 additions and 6 deletions

View File

@ -1,9 +1,7 @@
package iteration package iteration
const repeatTime = 5
// Repeat takes a string and repeat it 5 times // Repeat takes a string and repeat it 5 times
func Repeat(s string) string { func Repeat(s string, repeatTime int) string {
var res string var res string
for i := 0; i < repeatTime; i++ { for i := 0; i < repeatTime; i++ {
res += s res += s

View File

@ -6,13 +6,13 @@ import (
) )
func ExampleRepeat() { func ExampleRepeat() {
repeated := Repeat("ab") repeated := Repeat("ab", 5)
fmt.Println(repeated) fmt.Println(repeated)
// Output: ababababab // Output: ababababab
} }
func TestRepeat(t *testing.T) { func TestRepeat(t *testing.T) {
repeated := Repeat("a") repeated := Repeat("a", 5)
expected := "aaaaa" expected := "aaaaa"
if repeated != expected { if repeated != expected {
@ -22,6 +22,6 @@ func TestRepeat(t *testing.T) {
func BenchmarkRepeat(b *testing.B) { func BenchmarkRepeat(b *testing.B) {
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
Repeat("a") Repeat("a", 5)
} }
} }