Draw An ASCII Double Helix
CJam, 56 55 53 52 50 bytes
S'O:Ori:X0>"\/"=" / \\\ / "+Xz*1>O]s3/X"z"<~N*X\O?
Look at that size! The main culprits are N = 0
special case and the \
instead of /
in the vertical helix.
Here is how it works:
S'O:O e# Put a space on stack. Now put char O on stack
e# and assign it to variable O. This is not really
e# helping in golfing as using 'O everywhere is
e# same number of bytes
ri:X e# Read input as in integer and store it in X
0>"\/"= e# If X is greater than 0, choose /, otherwise \
" / \\\ / " e# Put this string on stack
+ e# Append to chosen \ or /
Xz* e# Repeat it abs(X) times
1> e# Remove the first character from repeated string
O] e# Put char O on stack, wrap everything in an array
e# and convert it to string.
3/ e# Split the string into parts of length 3
X"z"<~ e# If X is positive, transpose the array to get a
e# horizontal helix, otherwise it would be vertical
N* e# Join the parts with newline
X\O? e# If X was 0, then pick char O instead of this
e# final joined string.
The code is divided into three parts:
- The part
X0>"\/"=" / \\\ / "+
gives either"/ / \\\ / "
or"\ / \\\ / "
which is crucial as the helix is simply made up of alternate"/ \"
and"\ /"
joined by either" / "
or" \ "
. For instance, if you consider input to be2
, then your final repeated string would be"/ / \\ / / / \\ / "
(without escaping). This obviously has extra/
at the beginning and an extra space at the ending. - Second part is to correct the above string with additional things and split. For an input
2
, the desired final string without newlines would be" O / \\\ / / / \\\ / O"
, but after the above point, we only have"/ / \\\ / / / \\\ / "
. So we remove the first character, add a space and'O
at the beginning and another'O
at the end. Then we finally split it into parts of 3 - Finally, we decide whether to transpose this split string for a vertical helix or not; Join the parts by newlines; And choose between this and a single character
'O
(for input 0 case)
Try it online here
JavaScript(ES6), 126 132 133
A=n=>(F=(f,j='')=>f+(j+f).repeat(n-1),n>0?F(' /\\')+`
o${F(' ','/')}o
`+F(' \\/'):(n=-n)?` o${F(`
/ \\
\\ /
`,' \\')} o`:'o')
// Test
for(i=0;i<10;i++)
P.innerHTML = P.innerHTML + A(i)+'\n\n\n',
N.innerHTML = N.innerHTML + A(-i)+'\n\n\n'
pre {
font-size: 10px;
line-height: 9px;
}
<table>
<tr><th>Positive</th><th>Negative</th></tr>
<tr><td valign=top><pre id=P></pre></td><td><pre id=N></pre></td></tr>
</table>
Using templated string, newlines count.
More readable
A=n=>(
F=(f,j='')=>f+(j+f).repeat(n-1),
n > 0 ? F(' /\\') + '\no' + F(' ','/') + 'o\n'+F(' \\/')
: (n=-n) ? ' o' + F('\n/ \\\n\\ /\n',' \\')'+' o':'o'
)
Pyth, 52 bytes
M[Jj"/\\"*hGdjP*G+*2dH*2\O_J)?jb?gQ\/>Q0msdCg_Q\\Q\O
Demonstration.
Explanation:
The first section, M[Jj"/\\"*hGdjP*G+*2dH*2\O_J)
, defines a function, g
, which takes two inputs. The first input, G
, is the number of repetions to use. This is the absolute value of the input. The second input, H
, is the character to place at the center of the spirals.
The function returns a 3 element list, which consists of the 3 lines of the positive spiral, and the 3 columns of the negative spiral.
The first element is defined by Jj"/\\"*hGd
. *hGd
is the string of G+1
spaces. j"/\\"*hGd
joins that string with "/\"
as the delimeter. The J
at the beginning saves the resultant value for future use.
The second element is jP*G+*2dH*2\O
. We start with +*2dH
. This is two spaces followed by the input character. Then, we repeat that string G
times with *G
. Then, we remove its final character with P
. Finally, we surround this string with two O
characters, with j ... *2\O
.
The third element is generated with _J
. This is simply the reverse of the first line.
The latter section, ?jb?gQ\/>Q0msdCg_Q\\Q\O
selects between three different posibilities, positive, negative and zero. The first if-then conditions on Q
, the input. The second conditions on >Q0
, whether the input is positive.
If Q
is zero, \O
, the character O
, is printed.
If Q
is nonzero, we join the result of the second ternary on newlines and print it, with jb
. If Q
is positive, the list joined and printed is gQ\/
, g(Q,"/")
.
If Q
is negative, the list joined and printed is msdCg_Q\\
. We start with g_Q\\
, which is g(-Q,"\")
. Then we transpose the rows and columns with C
. msd
turns the resultant tuples of characters into strings, ready to be joined on newlines and printed.