algorithms/shuffle/shuffle.go

26 lines
386 B
Go
Raw Normal View History

2020-11-18 18:24:48 +00:00
package shuffle
2020-11-18 18:40:58 +00:00
import (
"math/rand"
"strings"
)
// Shuffle returns a shuffled version of the given string
//
// Shuffle("rtfm") -> fmrt
func Shuffle(arr string) string {
var result = make([]string, len(arr))
for k, v := range strings.Split(arr, "") {
j := rand.Intn(len(arr) - 0)
tmp := v
result[k] = result[j]
result[j] = tmp
}
return strings.Join(result, "")
2020-11-18 18:24:48 +00:00
}