29
8
Challenge
Given a list of integers, show how gravity sort would be done.
Gravity Sort
In gravity sort, imagine the numbers as rows of asterisks. Then, everything falls, and the new rows will be obviously sorted. Let's look at an example:
[2, 7, 4, 6]
:
**
*******
****
******
-------
**
****
*******
******
-------
** | 2
**** | 4
****** | 6
******* | 7
Notice that this is pretty much just parallelized bubble sort.
Exact Specs
On each iteration, starting from the top row, take every asterisk from the row that doesn't have an asterisk below it, and move it down a row. Keep doing that until the list is sorted.
Input
Input will be a list of strictly positive integers.
Output
For the output, you must output each step. You can choose any two non-whitespace printable ASCII characters, one to be the "asterisks", and one to be the separating "dashes". The rows of asterisks must be separated with a standard newline of some sort (e.g. \n
or \r\f
). The row of dashes must be at least the width of the widest row (otherwise your asterisks are going to fall too far down!). A row of dashes at the very bottom is optional. A trailing newline at the end is permitted. Trailing spaces on each line are permitted.
Test Cases
input will be represented as a list, then output will be listed immediately below. Test cases are separated by a double-newline.
[4, 3, 2, 1]
****
***
**
*
----
***
** *
* *
**
----
**
* *
** *
***
----
*
**
***
****
[6, 4, 2, 5, 3, 1]
******
****
**
*****
***
*
------
****
** **
****
***
* **
***
------
**
****
*** **
* *
***
*****
------
**
***
* *
*** **
****
*****
------
**
*
***
****
******
*****
------
*
**
***
****
*****
******
[8, 4, 2, 1]
********
****
**
*
--------
****
** ****
* **
**
--------
**
* **
** ****
****
--------
*
**
****
********
Please feel free to correct my test cases if they're wrong, I made them by hand :)
Note: Do not output the sorted list at the end. :)
Scoring
All of your programs will be written on top of each other. You wouldn't want pieces of your program to fall down, so make sure you have the shortest code!
1Can we avoid printing dashes? and Instead of printing asterisks can we print matrix of 0s and 1s?I think format of printing adds nothing to the challenge. – rahnema1 – 2017-05-11T15:44:37.977
@rahnema1 1. You may replace the dashes with some other non-whitespace character 2. No. – HyperNeutrino – 2017-05-11T15:51:20.890
I believe you are missing an asterisk on the 2nd iteration of your last test case – MildlyMilquetoast – 2017-05-11T16:56:40.917
@MistahFiggins Ah, yes. Thanks! – HyperNeutrino – 2017-05-11T16:58:42.153
1If we don't want pieces of the program to fall down, does this mean that we can't have longer lines of code on top of our shorter lines of code? :o – Value Ink – 2017-05-11T20:19:31.117
@ValueInk lol It doesn't really work because your code will get mixed in with other code falling onto your line. :P – HyperNeutrino – 2017-05-11T20:50:09.063
@ValueInk that's actually a decent idea for a restricted-source challenge, though maybe only for 2D languages. (too easy with line continuation in most langs) – Rɪᴋᴇʀ – 2017-05-11T21:49:14.553
1Hey that's how I sort my books! – Robert Fraser – 2017-07-10T15:05:37.143