43
\$\begingroup\$

Write a program that takes in (via STDIN/command line) a non-negative integer N.

When N is 0, your program should print O (that's capital Oh, not zero).

When N is 1, your program should print

\|/
-O-
/|\

When N is 2 your program should print

\ | /
 \|/
--O--
 /|\
/ | \

When N is 3 your program should print

\  |  /
 \ | /
  \|/
---O---
  /|\
 / | \
/  |  \

For larger N, this pattern continues on in the same exact fashion. Each of the eight rays of the "sun" should be made of N of the appropriate -, |, /, or \ characters.

Details

  • Instead of a program, you may write a function that takes an integer. The function should print the sun design normally or return it as a string.
  • You must either

    • have no trailing spaces at all, or
    • only have enough trailing spaces so the pattern is a perfect (2N+1)*(2N+1) rectangle.
  • The output for any or all N may optionally have a trailing newline.

Scoring

The shortest code in bytes wins.

\$\endgroup\$
  • \$\begingroup\$ Is a leading newline allowed? Especially interesting for N=0. \$\endgroup\$ – Jakube May 7 '15 at 23:10
  • \$\begingroup\$ @Jakube No. Trailing only. \$\endgroup\$ – Calvin's Hobbies May 7 '15 at 23:12

37 Answers 37

1
2
1
\$\begingroup\$

Powershell, 108 97 93 bytes

filter f{try{$s=' '*--$_
"\$s|$s/"
$_|f|%{(" $_ ","-$_-")[$_-match'O']}
"/$s|$s\"}catch{'O'}}

Explanation:

  • the recursive algorithm throws an exception on $s=' '*--$_ when current number less then 1;
  • the output is a perfect rectangle with trailing spaces.

Less golfed test script:

filter f{
    try{
        $s=' '*--$_
        "\$s|$s/"
        $_|f|%{(" $_ ","-$_-")[$_-match'O']}
        "/$s|$s\"
    }catch{
        'O'
    }
}

@(

,(0,"O")
,(1,"\|/",
    "-O-",
    "/|\")
,(2,"\ | /",
    " \|/ ",
    "--O--",
    " /|\ ",
    "/ | \")
,(3,"\  |  /",
    " \ | / ",
    "  \|/  ",
    "---O---",
    "  /|\  ",
    " / | \ ",
    "/  |  \")

) | % {
    $n,$expected = $_
    $result = $n|f
    "$result"-eq"$expected"
    $result
}

Output:

True
O
True
\|/
-O-
/|\
True
\ | /
 \|/ 
--O--
 /|\ 
/ | \
True
\  |  /
 \ | / 
  \|/  
---O---
  /|\  
 / | \ 
/  |  \
|improve this answer|||||
\$\endgroup\$
1
\$\begingroup\$

Charcoal, 12 5 bytes

P*⊕θO

-7 bytes thanks to @ASCII-only, by using the builtin :*.

Try it online (verbose) or try it online (pure).

Explanation:

Print the 8-armed lines with a length of the input+1 (the +1 is because the center is included), without moving the cursor position:

MultiPrint(:*, Incremented(q));
P*⊕θ

Then print the "O" at the current location (still in the center, because we haven't moved due to the MultiPrint):

Print("O");
O
|improve this answer|||||
\$\endgroup\$
  • 2
    \$\begingroup\$ 5? \$\endgroup\$ – ASCII-only Jan 26 '19 at 23:44
  • \$\begingroup\$ @ASCII-only Thanks! Didn't even knew there was a :* builtin for this. \$\endgroup\$ – Kevin Cruijssen Jan 27 '19 at 12:01
0
\$\begingroup\$

Groovy - 113

f={n->s=2*n+1;(1..s).each{r->(1..s+1).each{c->print c==r?n+1==r?"O":"\\":c==s-r+1?"/":c==n+1?"|":c>s?"\n":" "}}}

I think the ternary logic could probably be cleaned up to give a shorter answer...

|improve this answer|||||
\$\endgroup\$
0
\$\begingroup\$

SmileBASIC, 87 bytes

INPUT N
FOR Y=-N TO N
FOR X=-N TO N?" \/|-     O"[(X==Y)+!(X+Y)*2+!X*3+!Y*4];
NEXT?NEXT
|improve this answer|||||
\$\endgroup\$
0
\$\begingroup\$

Japt, 26 bytes

Pub golfing so, as usual, could probably be shorter.

Æ'\+'/i|û´NÑÄÃp'Oi-pU¹·ê û

Try it

Æ'\+'/i|û´NÑÄÃp'Oi-pU¹·ê û     :Implicit input of integer U
Æ                              :Map the range [0,U)
 '\+                           :  Append to "\"
    '/i                        :    "/" prepended with
       |û                      :      "|" centre padded with spaces to length
         ´N                    :        Decrement the (singleton) array of inputs, casting it to an integer on the first iteration
           Ñ                   :        Multiply by 2
            Ä                  :        Add 1
             Ã                 :End map
              p                :Push
               'Oi             :  "O" prepended with
                  -pU          :    "-" repeated U times
                     ¹         :End push
                      ·        :Join with newlines
                       ê       :Palindromise (Mirror around the "O")
                         û     :Centre pad each line with spaces to the length of the longest
|improve this answer|||||
\$\endgroup\$
0
\$\begingroup\$

Canvas, 14 bytes

╵⌐|*⁸/+↔ω↶n┼O╋

Try it here!

|improve this answer|||||
\$\endgroup\$
0
\$\begingroup\$

Retina 0.8.2, 99 bytes

.+
$* X$&$*-
 ( *)X(-+)
$1\|/¶$2X$2¶$1/|\
+`^ ( *.)( *)
$1$2 | $2/¶$&
+` ( */)( *).+$
$&¶$1$2 | $2\

Try it online! Explanation:

.+
$* X$&$*-

Place the X and draw n -s to its right. This completes the output for n=0.

 ( *)X(-+)
$1\|/¶$2X$2¶$1/|\

If n>0 then copy the -s to the left, and add the first level of \|/ and /|\ around the X. This completes the output for n=1.

+`^ ( *.)( *)
$1$2 | $2/¶$&

Extend the \|/ upwards, removing a space of indent each time, adding it back on each side of the |.

+` ( */)( *).+$
$&¶$1$2 | $2\

Extend the /|\ downwards, removing a space of indent each time, adding it back on each side of the |.

|improve this answer|||||
\$\endgroup\$
1
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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