6
1
Given a string or list, calculate the effects of gravity on that string/list.
The string will be made of 5 characters:
Nhas a weight of 2Lhas a weight of 1Shas a weight of -2Dhas a weight of -1
(the 5th character is whatever you want, it means empty space. I use (space) in my examples)
Items with negative weight fall upwards (towards the top of the string), and items with positive weight fall downwards (towards the bottom of the string).
When an object collides with something, one of three things happens:
If the object collides with the top or bottom "walls" of the input, then it just stops.
If the object collides with another object, their weight/velocity is summed (i.e.
NandDwould give1). If the result is positive, both objects fall downwards. If it's negative, they fall upwards.If an object (let's call it
A) collides with an objectB, andBis already pinned against the ceiling/floor,Awill just come to rest againstB.
All non-occupied spaces of the string/list should be filled with the empty character.
If it wasn't clear already: each character in the string/line is on top of the next one. i.e. NL is the same as:
N
L
And SD is:
(blank)
S
D
Test cases: (uses (the space character) to show empty space)
" L D " -> " LD " or " LD "
" SDLN " -> "SD LN"
" NLLSSD " -> "NLLSSD "
3"Given a delimited string or list." Can also use a string without delimiters? Since all items are just individual characters that shouldn't be a problem, right? And what's the expected outcome for
N0SorL0D? – Martin Ender – 2017-05-10T18:59:32.190@MartinEnder About the delimited string, that was left over form a previous version of the question. It has been edited out. And I added a test case to address the second issue. If it is still unclear, please let me know. – Comrade SparklePony – 2017-05-10T20:52:50.700
Why the
NULcharacter? – mbomb007 – 2017-05-10T21:21:01.270Should've specified a directional order-of-operations. – Magic Octopus Urn – 2017-05-16T19:54:02.673
Side note: if "physics" is to be accounted for, letters with negative weights should probably fall downwards just like the rest. – Julian Wolf – 2017-05-16T23:04:09.073
Would
␣␣NSDLLD␣␣becomeNSD␣␣␣␣LLD? – zgrep – 2017-05-18T16:05:04.040@zgrep Yes, it would. – Comrade SparklePony – 2017-05-18T17:02:08.500
Do things such as
-2and2fall/rise faster than-1and1? – zgrep – 2017-05-18T17:07:07.407@zgrep Everything falls/rises at the same rate. – Comrade SparklePony – 2017-05-18T17:08:05.440