Advertisement
DOGGYWOOF

Untitled

Jul 12th, 2024
7
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.96 KB | None | 0 0
  1. -- Define a function to jumble text
  2. local function jumbleText(text)
  3. local jumble = {}
  4. for i = 1, #text do
  5. jumble[i] = text:sub(i, i)
  6. end
  7. for i = #jumble, 2, -1 do
  8. local j = math.random(i)
  9. jumble[i], jumble[j] = jumble[j], jumble[i]
  10. end
  11. return table.concat(jumble)
  12. end
  13.  
  14. -- Define a function to unjumble text
  15. -- Note: For simplicity, this function assumes the text was jumbled with the same method
  16. local function unjumbleText(jumbledText)
  17. -- This is a placeholder since unjumbling requires knowing the original positions
  18. -- Implementing a real unjumbling would need a more sophisticated approach
  19. return "Unable to unjumble without original positions"
  20. end
  21.  
  22. -- Define a function to read text from a file
  23. local function readFile(fileName)
  24. local file = fs.open(fileName, "r")
  25. if not file then
  26. error("File not found")
  27. end
  28. local text = file.readAll()
  29. file.close()
  30. return text
  31. end
  32.  
  33. -- Define a function to write text to a file
  34. local function writeFile(fileName, text)
  35. local file = fs.open(fileName, "w")
  36. file.write(text)
  37. file.close()
  38. end
  39.  
  40. -- Main program
  41. local inputFileName = "input.txt"
  42. local jumbledFileName = "jumbled.txt"
  43. local unjumbledFileName = "unjumbled.txt"
  44.  
  45. -- Read the original text
  46. local originalText = readFile(inputFileName)
  47.  
  48. -- Jumble the text
  49. local jumbledText = jumbleText(originalText)
  50.  
  51. -- Write the jumbled text to a file
  52. writeFile(jumbledFileName, jumbledText)
  53.  
  54. -- Try to unjumble the text (Note: This won't work correctly with the current unjumble function)
  55. local unjumbledText = unjumbleText(jumbledText)
  56.  
  57. -- Write the unjumbled text to a file
  58. writeFile(unjumbledFileName, unjumbledText)
  59.  
  60. -- Check if the unjumbled text matches the original text
  61. if unjumbledText == originalText then
  62. print("The unjumbled text matches the original text.")
  63. else
  64. print("The unjumbled text does not match the original text.")
  65. end
  66.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement