7
\$\begingroup\$

This question already has an answer here:

Inspired by this, print an ascii diamond pattern built using 0-9.

Rules

  • Input n should be between 0-9, anything outside that range is undefined
  • The diamond should be padded with spaces
  • The middle line should always have no padding and be the only line to contain n, in the center
  • Print to stdout
  • As always, this is

Test Cases

  • n = 0
0
  • n = 1, print
 0 
010
 0 
  • n = 2, print
  0  
 010 
01210
 010 
  0  
  • n = 9, print
         0
        010
       01210
      0123210
     012343210
    01234543210
   0123456543210
  012345676543210  
 01234567876543210 
0123456789876543210
 01234567876543210
  012345676543210
   0123456543210
    01234543210
     012343210
      0123210
       01210
        010
         0
\$\endgroup\$

marked as duplicate by DJMcMayhem, Downgoat, Mego, manatwork, Emigna Jul 8 '16 at 7:20

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 6
    \$\begingroup\$ Unfortunately, someone already had the same challenge idea. \$\endgroup\$ – xnor Jul 8 '16 at 4:54
  • \$\begingroup\$ @xnor Wait - I don't think it's a duplicate. That asked you to print that specific diamond -- it was a k-complexity question. The answers will vary because this is asking you to generate it. \$\endgroup\$ – Insane Jul 8 '16 at 4:58
  • 2
    \$\begingroup\$ I'm not sold that the input makes a big difference, but I'll retract my dupehammer and let people vote. In particular, it seems to me like a lot of the answers could change their parameter 9 to an input. \$\endgroup\$ – xnor Jul 8 '16 at 5:03
  • 2
    \$\begingroup\$ I also feel like this is a duplicate, in the sense that answerers here could, in many cases, reasonably just look at the previous challenge’s solutions and replace a 9 by an input() or something. \$\endgroup\$ – Lynn Jul 8 '16 at 5:06
  • 3
    \$\begingroup\$ Starting from 0 might make a difference in messing up the arithmetic trick that 1111*1111=1234321, though I think that's unfortunate because it's a cool strategy. Is there something new that's allowed by the zeroes? \$\endgroup\$ – xnor Jul 8 '16 at 5:08
2
\$\begingroup\$

Vitsy, 50 bytes

I don't think I'm done golfing yet!

VVHr1mV{V2*1+\[2m]
:Xru
v&VvD([D\[' 'O]]-H1ml\NaOY

Explanation:

VVHr1mV{V2*1+\[2m]
V                  Pop n, set n as the global final variable (GFV).
 V                 Push the GFV to the current stack.
  H                Pop n, pop o, push each item of the range from o to n.
   r               Reverse the current program stack.
    1m             Call the method on line index 1.
      V{           Push the GFV to the bottom of the current stack.
        V2*1+      Push the GFV*2+1 to the stack.
             \[  ] Pop n, do the stuff in brackets n times.
               2m  Call the method on line index 2. Note that this is necessary due
                   to an unknown bug.

:Xru
:                  Clone the current stack, pushing a second stack to the stack 
                   stack and jumping to it.
 X                 Pop n and discard.
  r                Reverse the current stack.
   u               Pop the current stack and concatenate it to the previous.

v&VvD([D\[' 'O]]-H1ml\NaOY
v                  Pop n, save as a temporary variable.
 &                 Push a new stack to the stack stack and jump to it.
  V                Push the GFV.
   v               Push the temp var, clear the temp var memory.
    D([        ]   If the top item is not zero, do the stuff in brackets.
                   This is necessary as all loops always apply once.
       D\[' 'O]    Peek n, output n spaces.
   -               Pop n, pop o, push o-n.
    H              Pop n, pop o, push each item of the range from o to n.
     1m            Call the method at line index 1.
       l\N         Output all numbers in this stack.
          aO       Output a newline.
            Y      Pop and discard the current stack from the stack stack.

Try it online!

\$\endgroup\$
  • \$\begingroup\$ That was SpOOky as hell I didn't realize you deleted it \$\endgroup\$ – Insane Jul 8 '16 at 5:41
  • 2
    \$\begingroup\$ @Insane Yeah, I made a one byte error. :P Fixed now. \$\endgroup\$ – Addison Crump Jul 8 '16 at 5:42
  • \$\begingroup\$ @Insane Explanation added. \$\endgroup\$ – Addison Crump Jul 8 '16 at 5:55
  • \$\begingroup\$ @Insane Try this with input 40. ;) \$\endgroup\$ – Addison Crump Jul 8 '16 at 6:06

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