Challenge
Given input as a list/array of natural numbers that each represent the floor of a class a student has to go to, sort the numbers in a way such that the MAD (mean average deviation) number of flights between classes is minimized, thus saving energy and preventing the chance of being late to a class.
When sorted like this, the highest floor is in the middle of the list/array and the lowest floors are on the sides.
Rules
- The student starts and ends on the first floor for entering and exiting the school building.
- The student only uses the stairs to get to each class.
- The list is in sequential order.
- This is code-golf, so answers will be scored in bytes with fewer bytes being the goal.
Test cases
Input | Possible Outputs |
---|---|
8, 9, 6, 4, 7, 10, 5, 7, 8 | 4, 6, 7, 8, 10, 9, 8, 7, 5 5, 7, 8, 9, 10, 8, 7, 6, 4 |
4, 7, 3, 5, 7, 8, 4, 2, 4 | 2, 4, 4, 7, 8, 7, 5, 4, 3 3, 4, 5, 7, 8, 7, 4, 4, 2 |
4, 1, 2, 3, 2, 9, 4, 6, 2, 8 | 2, 2, 4, 6, 9, 8, 4, 3, 2, 1 1, 2, 3, 4, 8, 9, 6, 4, 2, 2 |