In this challenge, I asked you to implement swap encoding.
Swap encoding is an encoding method where you iterate through the string, reversing substrings between identical characters. The basic algorithm is:
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!
Anyway, in this chat, @okie and I discussed what would happen if you repeatedly ran the swap algorithm on something until it returned to the original string. We managed to prove that it will always return.
Okay, that's a lot of preamble. Here's your challenge:
Write a program or function that takes a string as input, and returns the number of times it has to be swapped before it will return to itself.
Answers will be timed on their combined score on all the scored testcases, so please append a snippet to your program that runs all the testcases one after another.
I will time all answers on all on my computer (2018 Macbook Air), although please link to a downloadable interpreter/compiler for less mainstream languages.
Here is my reference implementation.
Simple testcases (Just to check it works):
1231 => 2 1234512345 => 6 123142523 Sandbox for Proposed Challenges => 1090 Collatz, Sort, Repeat => 51 Rob the King: Hexagonal Mazes => 144 Decompress a Sparse Matrix => 8610 14126453467324231 => 1412 12435114151241 - 288 Closest Binary Fraction => 242 Quote a rational number => 282 Solve the Alien Probe puzzle => 5598
Scored testcases (The ones I'm timing you on)
124351141512412435325 => 244129 Write a fast-growing assembly function => 1395376 And I'm now heading to the elevator => 207478 I'm in the now infamous elevator => 348112 1549827632458914598012734 => 1025264 i was always mediocre at golfing => 1232480 tho, i like some code challenges => 2759404 I am once again in an elevator => 1606256 A person with mannerism of cat => 867594
To make it fair, I have to time all programs under the same load on my computer. So if one person requests a retime, I have to retime them all. This means your time may fluctate.