app.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. package main
  2. import (
  3. "fmt"
  4. "math/rand"
  5. "sort"
  6. "unicode"
  7. )
  8. func encodeWord(word []rune, wordLength int) {
  9. if wordLength == 4 {
  10. word[1], word[2] = word[2], word[1]
  11. return
  12. }
  13. toShuffle := word[1 : wordLength-1]
  14. toShuffleLength := wordLength - 2
  15. rand.Shuffle(toShuffleLength, func(i, j int) {
  16. toShuffle[i], toShuffle[j] = toShuffle[j], toShuffle[i]
  17. })
  18. }
  19. func encodeText(text []rune) (string, []string) {
  20. var currentWord []rune
  21. var newString []rune
  22. var encodedWords []string
  23. for _, item := range text {
  24. if unicode.IsPunct(item) || unicode.IsSpace(item) {
  25. currentWordLength := len(currentWord)
  26. if currentWordLength >= 4 {
  27. beforeEncoding := string(currentWord)
  28. encodeWord(currentWord, currentWordLength)
  29. if string(currentWord) != beforeEncoding {
  30. encodedWords = append(encodedWords, beforeEncoding)
  31. }
  32. }
  33. for _, letter := range currentWord {
  34. newString = append(newString, letter)
  35. }
  36. currentWord = []rune{}
  37. newString = append(newString, item)
  38. continue
  39. }
  40. currentWord = append(currentWord, item)
  41. }
  42. sort.Strings(encodedWords)
  43. return string(newString), encodedWords
  44. }
  45. func main() {
  46. test := `This is a long looong test sentence,
  47. with some big (biiiiig) words!`
  48. fmt.Println(encodeText([]rune(test)))
  49. }