app.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 {
  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. partOfEncoded = append(partOfEncoded[:j], partOfEncoded[j+1:]...)
  60. found = true
  61. break
  62. }
  63. }
  64. if !found {
  65. break
  66. }
  67. }
  68. if found {
  69. encodedWords = append(encodedWords[:index], encodedWords[index+1:]...)
  70. return encodedWord
  71. }
  72. }
  73. return string(word)
  74. }
  75. func decodeText(text []rune, encodedWords []string) string {
  76. var currentWord []rune
  77. var newString []rune
  78. for _, item := range text {
  79. if unicode.IsPunct(item) || unicode.IsSpace(item) {
  80. currentWordLength := len(currentWord)
  81. if currentWordLength >= 4 {
  82. decoded := decodeWord(currentWord, currentWordLength, encodedWords)
  83. currentWord = []rune(decoded)
  84. }
  85. for _, letter := range currentWord {
  86. newString = append(newString, letter)
  87. }
  88. currentWord = []rune{}
  89. newString = append(newString, item)
  90. continue
  91. }
  92. currentWord = append(currentWord, item)
  93. }
  94. return string(newString)
  95. }
  96. func main() {
  97. test := `This is a long looong test sentence,
  98. with some big (biiiiig) words!`
  99. encodedText, encodedWords := encodeText([]rune(test))
  100. fmt.Println(encodedText, encodedWords)
  101. fmt.Println(decodeText([]rune(encodedText), encodedWords))
  102. }