This challenge is inspired by this very nice answer by TidB.
In TidB's answer, every eight character is in the correct order: gnilwoB edoC
(Code Bowling
backwards). The other strings however are were in a strange, random order.
Your challenge is to fix this.
Take a (non-empty) string and a positive integer n
as input. The string will contain ASCII characters in the range: 32-126 (space to tilde).
You must sort the string in ascending order (seen from the left, based on the ASCII-code value), but skip every n
th character, starting from the end of the string. As an example, let's take the string abcdABC123
as input, and n=4
, then we'll get:
abcdABC123 <- Input string. (n=4)
_b___B___3 <- These will not be sorted (every 4th starting from the end)
1_2AC_acd_ <- The remaining characters, sorted
1b2ACBacd3 <- The final string (the output)
Another example:
9876543210 <- Input string (n=2)
_8_6_4_2_0 <- These will not be sorted
1_3_5_7_9_ <- The remaining characters, sorted
1836547290 <- The final string (the output)
The input string can be taken on an optional format (string, list of characters, list of single character strings ...). The input integer can also be taken on an optional format.
Test cases:
The format will be n=__
, followed by the input string on the next line. The output is on the line below.
n=1 (All elements will stay in place)
nafgaksa1252#"%#
nafgaksa1252#"%#
n=214 (The last character will stay in place. All other are sorted.
&/lpfAVD
&/AVflpD
n=8
g7L9T E^n I{><#ki XSj!uhl y= N+|wA}Y~Gm&o?'cZPD2Ba,RFJs% V5U.W;1e 0_zM/d$bH`@vKoQ 43Oq*C
g n !#$%&'i*+,./01l234579;w<=>?@ADoEFGHIJKBLMNOPQR STUVWXYeZ^_`abcdhjkmqsuovyz{|}~C