Output a binary path from a number
MATL, 14 bytes
J_iB^YsJ+'o-'&XG
Produces graphical output as a path starting at coordinates (0,0). Try it at MATL Online! Or see some offline examples below:
Input
7
:Output:
Input
699050
:Output:
If you prefer, you can see the path as complex coordinates for 9 bytes:
J_iB^YsJ+
Try it online!
Explanation
J_ % Push -1j (minus imaginary unit)
i % Push input number
B % Convert to binary. Gives an array of 0 and 1 digits
^ % Power, element-wise. A 0 digit gives 1, a 1 digit gives -1j
Ys % Cumulative sum. Produces the path in the complex plane
J+ % Add 1j, element-wise. This makes the complex path start at 0
'o-' % Push this string, which defines plot makers
&XG % Plot
MATL, 10 bytes
YsG~YsQ1Z?
Inputs an array of binary digits. Outputs a matrix.
Try it online!
Explanation
Ys % Implicit input: array of binary digits. Cumulative sum. This gives the
% row coordinates
G % Push input again
~ % Negate: change 0 to 1 and 1 to 0
Ys % Cumulative sum
Q % Add 1. This gives the column coordinates
1Z? % Matrix containing 1 at those row and column coordinates and 0 otherwise.
% Implicit display
Jelly, 8 bytes
¬œṗ+\Ṭz0
A monadic link accepting a number as a list of ones and zeros (e.g. 13
is [1,1,0,1]
) returning a list of lists of ones and zeros where the first list is the first row.
Try it online! or see a formatted test-suite
How?
¬œṗ+\Ṭz0 - Link: list L e.g. [1,1,0,0,1,1,0,1] (i.e. 205)
¬ - logical NOT L [0,0,1,1,0,0,1,0]
\ - cumulative reduce L by:
+ - addition [1,2,2,2,3,4,4,5]
œṗ - partition @ truthy indices [[1,2],[2],[2,3,4],[4,5]]
Ṭ - un-truth (vectorises) [[1,1],[0,1],[0,1,1,1],[0,0,0,1,1]]
z0 - transpose with filler 0 [[1,0,0,0],[1,1,1,0],[0,0,1,0],[0,0,1,1],[0,0,0,1]]
- i.e. 1000
1110
0010
0011
0001