55
\$\begingroup\$

Write a program that takes in an integer N via stdin or the command line.

If N is 0, the single letter O must be printed to stdout.


If N is positive, this horizontal ASCII art double helix, drawn N segments wide, must be printed.

If N is 1, the output is:

 /\
O  O
 \/

If N is 2, the output is:

 /\ /\
O  /  O
 \/ \/

If N is 3, the output is:

 /\ /\ /\
O  /  /  O
 \/ \/ \/

If N is 4, the output is:

 /\ /\ /\ /\
O  /  /  /  O
 \/ \/ \/ \/

The pattern continues in the exact same way for larger N. Note that forward slash (/) must be used in all places the helixes cross, except for the O ends.


If N is negative, this vertical ASCII art double helix, drawn -N segments tall, must be printed.

If N is -1, the output is:

 O
/ \
\ /
 O

If N is -2, the output is:

 O
/ \
\ /
 \
/ \
\ /
 O

If N is -3, the output is:

 O
/ \
\ /
 \
/ \
\ /
 \
/ \
\ /
 O

If N is -4, the output is:

 O
/ \
\ /
 \
/ \
\ /
 \
/ \
\ /
 \
/ \
\ /
 O

The pattern continues in the exact same way for smaller N. Note that backward slashes (\) must be used in all places the helixes cross, except for the O ends.

Details

  • Instead of a program, you may write a function that takes N as an integer and prints the result normally or returns it as a string.
  • The output for any N may optionally contain a trailing newline.
  • Any line of output for any N may optionally contain 4 or fewer trailing spaces.
  • There should never be any leading space that aren't part of the specified pattern.
  • The shortest code in bytes wins.
\$\endgroup\$
4
  • 9
    \$\begingroup\$ Brilliant Question! \$\endgroup\$
    – Sarima
    May 11, 2015 at 9:11
  • \$\begingroup\$ it seems to me that for n=0, it might be convenient to print <spc>O<spc> or \nO\n. Is unnecesary leading whitespace allowed? \$\endgroup\$ May 11, 2015 at 13:31
  • 1
    \$\begingroup\$ print "." Zoom in to see helix. *nodnod* \$\endgroup\$ May 11, 2015 at 20:05
  • \$\begingroup\$ @steveverrill That may have been helpful but there are so many answers now I don't want to change the rule. I've clarified that leading spaces not part of the pattern are not allowed. \$\endgroup\$ May 11, 2015 at 20:15

34 Answers 34

1
2
0
\$\begingroup\$

C (gcc), 161 160 bytes

Now more compliant than ever! Thanks to @EriktheOutgolfer for making it so.

#define g(n)for(i=n;i--;)printf(
i;f(n){if(n<0){puts(" O");g(-n)"/ \\\n\\ /\n%2c\n",i?92:79);}else{g(n)" /\\");g(1)"\nO"+!n);g(n)i?"  /":"  O\n");g(n)" \\/");}}

Try it online!

\$\endgroup\$
2
  • \$\begingroup\$ You can save a byte by making the Os uppercase. However, I think this answer is invalid (There should never be any leading space that aren't part of the specified pattern.) \$\endgroup\$ Jan 21, 2018 at 16:37
  • \$\begingroup\$ @EriktheOutgolfer Ah, yes. Not sure why I added unnecessary spaces there. Also, thanks for pointing out the Os are capital, since I completely missed that. \$\endgroup\$
    – gastropner
    Jan 21, 2018 at 20:31
0
\$\begingroup\$

SOGL V0.12, 37 bytes

 O¶/”▓:.θH{"⌠λ5⁸‘▓+};↕№+╬⁷.0>?I↔}.‽ O

Try it Here!

\$\endgroup\$
0
\$\begingroup\$

Ruby, 118 138 bytes

->i{print i<0?" O ":" /\\"*i+"\n"*(1-0**i),i<0?("\n/ \\\n\\ /\n \\"*-i)[0..-3]:i==0&&"O"||"O "+" / "*(i-1)+" O\n",i<0?" O ":" \\/"*i+"\n"}

Basically a if-else statement on all the 3 parts of the helixes :)

Try it online!

Any tips to get it even smaller would be greatly appericated!

Edit: Silly me forgot to remove all the parantheses, and didnt see the requirement of "O" if 0

\$\endgroup\$
0
\$\begingroup\$

Vyxal, 45 bytes

ȧDk/ðp*ð+⅛\/w$‹ẋf\Op\OJṄððdV⅛k\ðp*⅛¾?0<ßÞTvṅ⁋

Try it Online!

Transposing the input yields the vertical version :)

\$\endgroup\$
1
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.