The idea was to create a function that takes a string of some integers, text that can also include symbols like ',. etc. -If there is numbers in a string, return string -The first and last letter in a word should be untouched. Scramble only other letter.
- If we have a "'" symbol, don't scramble it.
The problem is that I expect to print out as in the want section, but getting another result . For example: instead of a -> aa , metatr -> metater etc. I use seed = 100
Function to tokenize:
import "regexp"
func tokenize(text string) []string {
re := regexp.MustCompile(`[A-Za-z0-9']+|[':;?().,!\\ ]`)
return re.FindAllString(text, -1)
}
Scramble code
import (
"math/rand"
"strconv"
)
func scramble(text string, seed int64) string {
rand.Seed(seed)
slicy := tokenize(text)
var str string = ""
for a := 0; a < len(slicy); a++ {
_, err := strconv.Atoi(slicy[a])
if err != nil {
var shufle []byte
for i := 1; i < len(slicy[a])-1; i++ {
shufle = append(shufle, slicy[a][i])
}
if slicy[a] != " " && slicy[a] != "." && slicy[a] != "," && slicy[a] != "(" && slicy[a] != ")" {
new_length := 0
for d := 0; d < len(shufle); d++ {
if string(shufle[d]) == "'" {
new_length = d - 1
}
}
if new_length != 0 {
for l := 0; l < new_length-1; l++ {
m := l + 1
shufle[l], shufle[m] = shufle[m], shufle[l]
}
} else {
rand.Shuffle(len(shufle), func(k, j int) {
shufle[k], shufle[j] = shufle[j], shufle[k]
})
}
var scrambled_list []byte
scrambled_list = append(scrambled_list, slicy[a][0])
for d := 0; d < len(shufle); d++ {
scrambled_list = append(scrambled_list, shufle[d])
}
scrambled_list = append(scrambled_list, slicy[a][len(slicy[a])-1])
str = str + string(scrambled_list)
} else {
switch slicy[a] {
case " ":
str += " "
case ".":
str += "."
case "(":
str += "("
case ")":
str += ")"
case ",":
str += ","
default:
}
}
} else {
return text
}
}
return str
}
When i test it, i fails on:
Input:
"it doesn't matter in what order the letters in a word are, the only important thing is that the first and last letter be at the right place."
Want:
"it deson't metatr in waht oredr the lettres in a wrod are, the only inmproatt thing is taht the frist and last letetr be at the right plcae."
What I get:
"it deson't metater in waht odrer the leretts in aa wrod are, the olny imtoaprnt thnig is that the fisrt and last leettr be at the right place."