Add iteration package
This commit is contained in:
parent
c37e1c26e1
commit
cb26678c66
16
iteration/repeat.go
Normal file
16
iteration/repeat.go
Normal 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
32
iteration/repeat_test.go
Normal 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
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user