app.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. return
  19. }
  20. func encodeText(text []rune) (string, []string) {
  21. var currentWord []rune
  22. var newString []rune
  23. var encodedWords []string
  24. for _, item := range text {
  25. if unicode.IsPunct(item) || unicode.IsSpace(item) {
  26. currentWordLength := len(currentWord)
  27. if currentWordLength >= 4 {
  28. beforeEncoding := string(currentWord)
  29. encodeWord(currentWord, currentWordLength)
  30. if string(currentWord) != beforeEncoding {
  31. encodedWords = append(encodedWords, beforeEncoding)
  32. }
  33. }
  34. for _, letter := range currentWord {
  35. newString = append(newString, letter)
  36. }
  37. currentWord = []rune{}
  38. newString = append(newString, item)
  39. continue
  40. }
  41. currentWord = append(currentWord, item)
  42. }
  43. sort.Strings(encodedWords)
  44. return string(newString), encodedWords
  45. }
  46. func main() {
  47. test := `This is a long looong test sentence,
  48. with some big (biiiiig) words!`
  49. fmt.Println(encodeText([]rune(test)))
  50. }