Challenge:
Input:
A sorted list of positive integers.
Output:
The amount of integers which are still at the exact same index, after rotating the digits in each integer its index amount of times towards the left and sorting the modified list again.
Example:
Input: [8,49,73,102,259,762,2782,3383,9217,37846,89487,7471788]
Output (0-based indexing): 6
Output (1-based indexing): 5
Why?
0-based indexing:
After rotating each: [8,94,73,102,592,276,8227,3338,9217,63784,89487,7887471]
Sorted again: [8,73,94,102,276,592,3338,8227,9217,63784,89487,7887471]
Input indices: 0 1 2 3 4 5 6 7 8 9 10 11
Original input-list: [8,49,73,102,259,762,2782,3383,9217,37846,89487,7471788]
Modified list: [8,73,94,102,276,592,3338,8227,9217,63784,89487,7887471]
Modified indices: 0 2 1 3 5 4 7 6 8 9 10 11
Equal indices: ^ ^ ^ ^ ^ ^
So the output is: 6
1-based indexing:
After rotating each: [8,49,37,021,925,762,2278,3383,2179,37846,94878,8874717]
Sorted again: [8,(0)21,37,49,762,925,2179,2278,3383,37846,94878,8874717]
Input indices: 1 2 3 4 5 6 7 8 9 10 11 12
Original input-list: [8,49,73,102,259,762,2782,3383,9217,37846,89487,7471788]
Modified list: [8,21,37,49,762,925,2179,2278,3383,37846,94878,8874717]
Modified indices: 1 4 3 2 6 5 9 7 8 10 11 12
Equal indices: ^ ^ ^ ^ ^
So the output is: 5
Challenge rules:
- The input-list is guaranteed to only contain positive integers.
- The input-list is guaranteed to be sorted from lowest to highest.
- The input-list is guaranteed to contain at least two items.
- As you can see above, both 0-based and 1-based indexing is allowed. Please state in your answer which of the two you've used, since outputs can differ accordingly!
- Leading
0
s after rotating are ignored, which can be seen with the 1-based example above, where the integer102
becomes021
after rotating, and is then treated as21
. - Integers are guaranteed unique in the input-list, and are guaranteed to remain unique after the rotations are completed.
- Note that we only look at the positions of the rotated integers in correlation with the positions of the input, not with the values of the input-list. To clarify what I mean by this: with the input-list
[1234,3412]
and 1-based indexing, the list becomes[2341,1234]
after rotating each integer it's index amount of times, and then when sorted becomes[1234,2341]
. Although both the original input-list and the rotated list contains the integer1234
at the leading position, they aren't the same! The rotated1234
was3412
before. The 1-indexed output for this input-list is therefore0
, since the two integers have swapped their positions. - Input is flexible. Can be a list/stream/array of integers/strings/digit-arrays, etc. Please state what you've used if you don't take the inputs as integers.
General rules:
- This is code-golf, so shortest answer in bytes wins.
Don't let code-golf languages discourage you from posting answers with non-codegolfing languages. Try to come up with an as short as possible answer for 'any' programming language. - Standard rules apply for your answer with default I/O rules, so you are allowed to use STDIN/STDOUT, functions/method with the proper parameters and return-type, full programs. Your call.
- Default Loopholes are forbidden.
- If possible, please add a link with a test for your code (i.e. TIO).
- Also, adding an explanation for your answer is highly recommended.
Test cases:
Input: [8, 49, 73, 102, 259, 762, 2782, 3383, 9217, 37846, 89487, 7471788]
0-based output: 6
1-based output: 5
Input: [1234, 3412]
0-based output: 2
1-based output: 0
Input: [2349, 2820, 17499, 21244, 29842, 31857, 46645, 56675, 61643, 61787]
0-based output: 3
1-based output: 0
Input: [4976, 11087, 18732, 22643, 52735]
0-based output: 2
1-based output: 3
Input: [4414, 5866, 7175, 8929, 14048, 16228, 16809, 19166, 24408, 25220, 29333, 44274, 47275, 47518, 53355]
0-based output: 4
1-based output: 4
Input: [11205, 16820, 63494]
0-based output: 1
1-based output: 3
Feel free to generate more random test cases with (or draw inspiration from) this ungolfed 05AB1E program, where the input is the size of the random list (NOTE: the output of this generator might not comply with the rule "Integers are guaranteed unique in the input-list, and are guaranteed to remain unique after the rotations are completed", so keep that in mind when using it.)