Introduction:
After I posted two rainbow-related challenges: Codegolf Rainbow : Fun with Integer-Arrays 1 and Codegolf Rainbow : Draw in Black-and-White 2, the following comment was made by @ChrisM in the ASCII (Draw in Black-and-White) challenge:
Maybe you know this and it's by design (I know that rainbows are not rhombuses or ascii, too, and higher orders' positions get more complicated), but aren't the colours reversed in the 2nd rainbow?
And he's indeed right. When you see a double rainbow, the second one is actually a reflection of the first, so the colors are reversed. With three rainbows, it's an actual double rainbow, with the third one being a reflection of one of the other two. And with four, there are two rainbows, and two reflections of those.
So, let's make a third related challenge using that fact.
Challenge:
Inputs: A positive integer n
which is >=2
, and a list of integers of size >= n+1
.
Output: The same list ordered as follows:
- First split the input lists in sub-lists of size
n
(where the trailing sub-list could be of any size in the range[1,n]
). - Then we do the following based on the amount of sub-lists
m
:- Sort the first
m - m//2
amount of sub-lists from lowest to highest (where//
is integer-divide). (I.e. with 6 sub-lists the first three will be sorted from lowest to highest; with 5 sub-lists the fist three will be sorted from lowest to highest.) - Sort the last
m//2
amount of sub-lists from highest to lowest (where//
is integer-divide). (I.e. with 6 sub-lists the last three will be sorted from highest to lowest; with 5 sub-lists the last two will be sorted from highest to lowest.)
- Sort the first
- Merge all sub-lists together to form a single list again
Examples:
Inputs: n=7
and [3,2,1,-4,5,6,17,2,0,3,5,4,66,-7,7,6,-5,2,10]
Step 1: [[3,2,1,-4,5,6,17],[2,0,3,5,4,66,-7],[7,6,-5,2,10]]
Step 2: [[-4,1,2,3,5,6,17],[-7,0,2,3,4,5,66],[10,7,6,2,-5]]
Step 3 / Output: [-4,1,2,3,5,6,17,-7,0,2,3,4,5,66,10,7,6,2,-5]
Inputs: n=4
and [7,4,-8,9,3,19,0,-23,-13,13]
Step 1: [[7,4,-8,9],[3,19,0,-23],[-13,13]]
Step 2: [[-8,4,7,9],[-23,0,3,19],[13,-13]]
Step 3 / Output: [-8,4,7,9,-23,0,3,19,13,-13]
Challenge rules:
- The integer input
n
is guaranteed to be larger than 1. - The size of the integer-list is guaranteed to be larger than
n
. - The trailing sub-list can be smaller than
n
(as can be seen in the examples and test cases). - I/O format is flexible in any reasonable format. Can be a list/array of integers or decimals, a comma/space/newline delimited string, stream of integers, etc. (Output may not be a 2D list of lists like step 2. Step 3 to flatten it back into a single list is required for this challenge.)
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, 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.
- Also, adding an explanation for your answer is highly recommended.
Test cases:
Inputs: n=7 and [3,2,1,-4,5,6,17,2,0,3,5,4,66,-7,7,6,-5,2,10]
Output: [-4,1,2,3,5,6,17,-7,0,2,3,4,5,66,10,7,6,2,-5]
Inputs: n=4 and [7,4,-8,9,3,19,0,-23,-13,13]
Output: [-8,4,7,9,-23,0,3,19,13,-13]
Inputs: n=2 and [7,-3,1]
Output: [-3,7,1]
Inputs: n=3 and [1,6,99,4,2]
Output: [1,6,99,4,2]
Inputs: n=2 and [5,2,9,3,-5,-5,11,-5,4,12,9,-2,0,4,1,10,11]
Output: [2,5,3,9,-5,-5,-5,11,4,12,9,-2,4,0,10,1,11]
Inputs: n=3 and [5,2,9,3,-5,-5,11,-5,4,12,9,-2,0,4,1,10,11]
Output: [2,5,9,-5,-5,3,-5,4,11,12,9,-2,4,1,0,11,10]