I'm dealing with go/golang, trying to write code that takes a string and seed. It should return a shuffled string where the 1st and last letters in the word, punctuation and numbers are untouched.
Example:
my name is Nikki. My number is 333
should be
my nmae is Nkiki. My nebumr is 333
I'm very new to goland/go. Have tried to do it with rand.Shuffle
:
func splitText(text string) []string {
re := regexp.MustCompile("[A-Za-z0-9']+|[':;?().,!\\ ]")
return re.FindAllString(text, -1)}
func scramble(text string, seed int64) string {
rand.Seed(seed)
split := splitText(text)
for i := range split {
e := split[i]
r := []byte(e)
for i := 0; i < len(r); i++ {
l := len(r) - 1
if i == 0 {
return string(r[0])
}
if i == l {
return string(r[l])
}
if i > 0 || i < l {
n := rand.Intn(l)
x := r[i]
r[i] = r[n]
r[n] = x
}
if (r[i] >= 32 && r[i] <= 62) || (r[i] >= 91 && r[i] <= 96) {
return string(r[i])
}
}
}
return text
}
func main() {
v := ("The quick brown fox jumps over the lazy dog.")
scramble(v, 100)
fmt.Println(scramble(v, 100))
}
But this returns only one letter and only if remoe f==0
or f==l
, it doesn't hop to the next word.