app.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 removeString(array []string, index int) []string {
  46. return append(array[:index], array[index+1:]...)
  47. }
  48. func removeRune(array []rune, index int) []rune {
  49. return append(array[:index], array[index+1:]...)
  50. }
  51. func decodeWord(word []rune, wordLength int, encodedWords []string) (string, []string) {
  52. for index, encodedWord := range encodedWords {
  53. if len(encodedWord) != wordLength {
  54. continue
  55. }
  56. encoded := []rune(encodedWord)
  57. if word[0] != encoded[0] && word[wordLength-1] != encoded[wordLength-1] {
  58. continue
  59. }
  60. found := false
  61. partOfEncoded := encoded[1 : wordLength-1]
  62. for i := 1; i < wordLength-1; i++ {
  63. for j, letter := range partOfEncoded {
  64. if letter == word[i] {
  65. partOfEncoded = removeRune(partOfEncoded, j)
  66. found = true
  67. break
  68. }
  69. }
  70. if !found {
  71. break
  72. }
  73. }
  74. if found {
  75. return encodedWord, removeString(encodedWords, index)
  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. }