Swap encoding is an encoding method where you iterate through a string, reversing sections of it between pairs of identical characters.
The basic algorithm
For each character in the string:
Check: Does the string contain the character again, after the instance you found?
If so, then modify the string by reversing the section between the character and the next instance, inclusive.
Otherwise do nothing.
Start with the string 'eat potatoes'.
e is found again, so reverse that section:
o is found again, so reverse that section (doing nothing as it is palindromic)
t is found again, so reverse that section (doing nothing as it is palindromic)
a is found again, so reverse that section:
t is found again, so reverse that section:
None of 'op taes' are found again, so
eotatop taes is our final string!
Your challenge is to make a program or function that takes a string as input and returns it encoded as above. Capital letters and lowercase letters are treated as different, and all characters, including spaces, can be swapped.
Sandbox for Proposed Challenges => SahC Pr foropox dbosenallednges Collatz, Sort, Repeat => CoS ,ztrollaepeR ,tat Write a fast-growing assembly function => Wrgnufa ylbme asa f g-tesstcnoritiwoin Decompress a Sparse Matrix => Dera aM ssrtapmocese Sprix Rob the King: Hexagonal Mazes => Rog: Kinob eH lagnaM thezaxes Determine if a dot(comma) program halts => Detoc(tlamarommi finerp ha mrgod a) ets Closest Binary Fraction => CloinosestiB Fry tcaran Quote a rational number => Qunatebmuoiton re al ar Solve the Alien Probe puzzle => SorP Alveht en eilzzup elobe
Yes I just took the titles of various sandboxed challenges for the testcases.
My reference implementation is available here if anyone wants it.
This is actually an encoding as
reverse(swap(reverse(string))) decodes it.