To shuffle a string \$s\$, Alice applies the following algorithm:
- She takes the ASCII code of each character, e.g.
"GOLF"→ \$[ 71, 79, 76, 70 ]\$ - She sorts this list from lowest to highest: \$[ 70, 71, 76, 79 ]\$
- She reduces each value modulo the length of the string (4 in this case), leading to the list \$A = [ 2, 3, 0, 3 ]\$
- For each character at position \$n\$ (0-indexed) in the original string, she exchanges the \$n\$-th character with the \$A[n]\$-th character:
- exchange \$s[0]\$ with \$s[2]\$:
"GOLF"is turned into"LOGF" - exchange \$s[1]\$ with \$s[3]\$:
"LOGF"is turned into"LFGO" - exchange \$s[2]\$ with \$s[0]\$:
"LFGO"is turned into"GFLO" - exchange \$s[3]\$ with \$s[3]\$: this one doesn't change anything
- exchange \$s[0]\$ with \$s[2]\$:
She then sends the shuffled string "GFLO" to Bob.
Task
Your task is to help Bob understand Alice's message by applying the reverse algorithm: given a shuffled string as input, output the original string.
The input string is guaranteed to contain only printable ASCII characters (codes 32 to 126).
This is code-golf, so the shortest answer wins.
Test cases
Input:
AAA
GFLO
ron'llckR'o
Br b,!mn oGognooid
eAies a.turel vee.st hnrw .v
Output:
AAA
GOLF
Rock'n'roll
Good morning, Bob!
As we travel the universe...