app.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 decodeWord(word []rune, wordLength int, encodedWords []string) (string, []string) {
  46. for index, encodedWord := range encodedWords {
  47. if len(encodedWord) != wordLength {
  48. continue
  49. }
  50. encoded := []rune(encodedWord)
  51. if word[0] != encoded[0] && word[wordLength-1] != encoded[wordLength-1] {
  52. continue
  53. }
  54. found := false
  55. partOfEncoded := encoded[1 : wordLength-1]
  56. for i := 1; i < wordLength-1; i++ {
  57. for j, letter := range partOfEncoded {
  58. if letter == word[i] {
  59. var newPartOfEncoded []rune
  60. newPartOfEncoded = append(newPartOfEncoded, partOfEncoded[:j]...)
  61. newPartOfEncoded = append(newPartOfEncoded, partOfEncoded[j+1:]...)
  62. partOfEncoded = newPartOfEncoded
  63. found = true
  64. break
  65. }
  66. }
  67. if !found {
  68. break
  69. }
  70. }
  71. if found {
  72. var newEncodedWords []string
  73. newEncodedWords = append(newEncodedWords, encodedWords[:index]...)
  74. newEncodedWords = append(newEncodedWords, encodedWords[index+1:]...)
  75. return encodedWord, newEncodedWords
  76. }
  77. }
  78. return string(word), encodedWords
  79. }
  80. func decodeText(text []rune, encodedWords []string) string {
  81. var currentWord []rune
  82. var newString []rune
  83. for _, item := range text {
  84. if unicode.IsPunct(item) || unicode.IsSpace(item) {
  85. currentWordLength := len(currentWord)
  86. if currentWordLength >= 4 {
  87. var decoded string
  88. decoded, encodedWords = decodeWord(currentWord, currentWordLength, encodedWords)
  89. currentWord = []rune(decoded)
  90. }
  91. for _, letter := range currentWord {
  92. newString = append(newString, letter)
  93. }
  94. currentWord = []rune{}
  95. newString = append(newString, item)
  96. continue
  97. }
  98. currentWord = append(currentWord, item)
  99. }
  100. return string(newString)
  101. }
  102. func main() {
  103. test := `This is a long looong test sentence,
  104. with some big (biiiiig) words!`
  105. encodedText, encodedWords := encodeText([]rune(test))
  106. fmt.Println(encodedText, encodedWords)
  107. fmt.Println(decodeText([]rune(encodedText), encodedWords))
  108. }