31
\$\begingroup\$

Given a non-negative integer, print out an X that is that big. X is the output for input 0, and you will add slashes equal to the input in each direction to extend the X for larger inputs.

Test Cases

0

X

1

\ /
 X
/ \

2

\   /
 \ /
  X
 / \
/   \

...

10

\                   /
 \                 /
  \               /
   \             /
    \           /
     \         /
      \       /
       \     /
        \   /
         \ /
          X
         / \
        /   \
       /     \
      /       \
     /         \
    /           \
   /             \
  /               \
 /                 \
/                   \

Rules

You may either print the output, or return a string or list of string from a function. A trailing newline, as well as extra interior whitespace that does not affect appear, is allowed.

This is , so shortest answer in bytes wins!

\$\endgroup\$
6
  • \$\begingroup\$ Sandbox; related \$\endgroup\$
    – Stephen
    Commented Jun 8, 2017 at 18:16
  • 9
    \$\begingroup\$ ... I'm just gonna wait for the charcoal 1-byte solution. \$\endgroup\$
    – Leaky Nun
    Commented Jun 8, 2017 at 18:16
  • \$\begingroup\$ @LeakyNun I'd be surprised if Charcoal can easily deal with the X in the middle (part of why I added it) but who knows :D \$\endgroup\$
    – Stephen
    Commented Jun 8, 2017 at 18:17
  • \$\begingroup\$ Also related \$\endgroup\$
    – Value Ink
    Commented Jun 8, 2017 at 20:02
  • \$\begingroup\$ Related, related \$\endgroup\$
    – xnor
    Commented Jun 8, 2017 at 20:05

46 Answers 46

1
2
2
\$\begingroup\$

Perl 5 -n, 59 bytes

map{//;say map$_|$'?$_-$'?$_+$'?$":'/':'\\':X,@a}@a=-$_..$_

Try it online!

Uses the same logic as my ASCII sun answer here.

It treats the output as an X-Y grid from -n to +n where n is the input value. If X==-Y, output a slash. If X==Y, output a backslash. Put an X at the origin. Everything else is a space.

\$\endgroup\$
1
  • \$\begingroup\$ Amazing work! I spent far too long trying to get mine down! 😅 You can drop one more byte using v92 instead of '\\' too! \$\endgroup\$ Commented Apr 25 at 8:06
2
\$\begingroup\$

YASEPL, 114 111 bytes

=i!m'!n$!u$m]2!f$47»!b$92»!l$+`1!o;" ",i~#b$u-i*-;" ",o~>f!i+n}l,m-`2!n[3--!o;" ",m~>"X"!m]3=m-!l$!b)f!f$92»|`3

explanation (poor)

=i!m'!n$!u$m]2!f$47»!b$92»!l$+`1!o;" ",i~#b$u-i*-;" ",o~>f!i+n}l,m-`2!n[3--!o;" ",m~>"X"!m]3=m-!l$!b)f!f$92»|`3         packed
=i                                                                                                                      increment variable
  !m'                                                                                                                   input 
     !n$                                                                                                                N = 1
        !u$m                                                                                                            U = M
            ]2                                                     `2                                                   If U != 0 ...
              !f$47»!b$92»                                                                                                F = "/", B = "\"
                          !l$+                                                                                            set L to 2
                              `1                          !i  }l,m                                                        while (L == 2 and I < M) xor (L == 1 and I > M) ...
                                !o;" ",i~                                                                                   write a space I amount of times (inline)
                                         #b                                                                                 write B (inline)
                                           $u-i*-;" ",o~                                                                    write a space [U - I] * 2 - 1 amount of times (inline)
                                                        >f                                                                  print F with newline at end
                                                            +n                                                              add N to I and loop.
                                                                  -                                                     decrement I
                                                                     !n[3                                    `3         If N == 1 ...
                                                                         --                                               subtract 2 from N
                                                                           !o;" ",m~>"X"                                  write a space M amount of times inline and an X with newline
                                                                                        !m]3                 `3           if M != 0 ...
                                                                                            =m-                             set M to -1
                                                                                               !l$                          set L to 1
                                                                                                  !b)f!f$92»                swap B and F
                                                                                                            |               loop back to 1 and  print other half
\$\endgroup\$
2
\$\begingroup\$

K (ngn/k), 19 18 bytes

-1 byte from @att

"X\\/"@2/~|:\=1+2*

Try it online!

Thanks to @tubular.

  • 1+2* multiply the (implicit) input by 2, then add 1
  • = generate an identity matrix of the above size
  • |:\ return a list containing the identity matrix and its reverse (i.e. the antidiagonal)
  • ~ "not" the above, i.e. convert 1's to 0's and 0's to 1's
  • 2/ convert to base-2
  • "X\\/"@ index into a string containing the appropriate characters
\$\endgroup\$
2
  • 1
    \$\begingroup\$ -1: "X\\/"@2/~|:\=1+2* \$\endgroup\$
    – att
    Commented Sep 7 at 2:49
  • \$\begingroup\$ Nice! Shuffling the order of the output characters avoids needing to use 1|:` to ensure an input of 0` is handled correctly \$\endgroup\$
    – coltim
    Commented Sep 7 at 13:26
1
\$\begingroup\$

Perl 5, 110 + 1 = 111 bytes

Uses -n flag.

$x=$_;$,="\n";push@x,$"x($x-$_)."\\".$"x(2*--$_+1)."/"while$_>0;say@x,$"x$x."x";for(reverse@x){y!\\/!/\\!;say}
\$\endgroup\$
1
\$\begingroup\$

QBIC, 90 bytes

~b=0|?@X`\[0,:-1|X=space$(a)┘Z=Z+X+@\`+space$((b-a)*2-1)+@/`+X+@┘`]Z=Z+space$(b)+A+_fZ

How this monstrosity works, is left as an excercise for the reader...

Sample output:

Command line: 3
\     /
 \   / 
  \ /  
   X
  / \  
 /   \ 
/     \
\$\endgroup\$
1
\$\begingroup\$

Visual Basic.Net, 454 450 Bytes

Option Strict Off
module m
sub main(a As String())
dim v=Convert.toInt32(a(0))
for i as Integer=v to 1 step -1
for j as Object=1 to v-i
w(" ")
next
w("\")
for j as Object=1 to i*2-1
w(" ")
next
console.writeline("/")
next
console.writeline(new String(" ",v)&"X")
for i as Object=1 to v
for j as Object=1 to v-i
w(" ")
next
w("/")
for j as Object=1 to i*2-1
w(" ")
next
console.writeline("\")
next
end sub
sub w(s)
console.write(s)
end Sub
end module

not sure whether making a func for writeline will save some bytes thanks to Stephen S for pointing at as ... removal also changed integer into object last edit changed the first one back

\$\endgroup\$
6
  • \$\begingroup\$ You're a brave one :) I believe you can remove all of the as Types, because if you have Option Strict Off, VB.NET acts like a loosely typed language. \$\endgroup\$
    – Stephen
    Commented Jun 8, 2017 at 21:25
  • \$\begingroup\$ Currently on Linux mono is anoying \$\endgroup\$ Commented Jun 8, 2017 at 21:27
  • \$\begingroup\$ mono doesn't cares about Option Strict, or at least thats what I think \$\endgroup\$ Commented Jun 8, 2017 at 21:29
  • \$\begingroup\$ Oh it seemed functions != for loops thanks for the golf \$\endgroup\$ Commented Jun 8, 2017 at 21:36
  • \$\begingroup\$ Isn't Option Strict off by default? It is in Visual Studio at least \$\endgroup\$
    – Stephen
    Commented Jun 8, 2017 at 22:25
1
\$\begingroup\$

05AB1E, 22 bytes

F'\IN-úR.∞})Âí'Xs)˜.c»

Try it online!

Explanation

F                        # for N in [0...input-1] do
 '\                      # push a backslash
   IN-ú                  # prepend input-N spaces
       R                 # reverse
        .∞               # mirror
          }              # end loop
           )             # wrap stack in a list
            Â            # bifurcate
             í           # reverse each item
              'Xs        # push an "X" between the 2 lists on the stack
                 )˜      # wrap in flattened list
                   .c    # pad lines to equal length
                     »   # join on newlines

Alternative 22 byte solution

F'\N·>ú'/ì})Âí'X¸«ì.c»
\$\endgroup\$
1
\$\begingroup\$

Pyke, 14 bytes

\XQV.X \   /\/

Try it here!

\X             - "X"
  QV           - repeat input times:
    .X \   /\/ -  surround(^, all=" ", 
                              tl="\", 
                              left=" ",  
                              right=" ",  
                              lower=" ",  
                              tr="/",  
                              br="\",  
                              bl="/")
\$\endgroup\$
1
\$\begingroup\$

tcl, 134

proc P {x s b} {time {puts [format %[incr ::i $x]s%[expr ($::n-$::i)*2+2]s $s $b]} $::n}
P 1 \\ /
puts [format %[incr i]s X]
P -1 / \\

demo

Set n on the first line.

May be I can golf it more using a recursive approach

\$\endgroup\$
1
\$\begingroup\$

R, 75 72 bytes

y=diag(b<-2*scan()+1);write(c(" ",'\\','/','X')[y+2*y[,b:1]+1],'',b,,'')

Inspired by this answer, generates an implicit matrix and writes it to stdout; reads the size from stdin. It has to build a matrix of space characters and uses sep='' b/c otherwise it has spacing issues.

diag(b)                     # generates a main diagonal of 1, rest 0
2*diag(b)[,b:1]             # the other diagonal is 2
                            # [,b:1] reverses columns
                            # [b:1,] reverses the rows; they're equivalent
diag(b)+2*diag(b)[,b:1]     # sums, so we get 1 for main diagonal
                            # 2 for other diagonal, 3 for center
diag(b)+2*diag(b)[,b:1]+1   # add 1 since R is 1-indexed
                            # the result is indices for the vector
c(' ','\\','/','X')

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Zsh, 83 bytes

2=${(l:$1:)}
S=$2\ $2
F()for i;(S[i]=\\;S[-i]=/;<<<$S)
F {1..$1}
<<<$2X
F {-$1..-1}

Try it online!

\$\endgroup\$
1
\$\begingroup\$

sed, 112 111 bytes

s/^0/X/
/X/q
s/.*/echo {0..&}/e
s/[^ ]//g
s%.*%\\&&/%
s/ //p
:a
/^\//Q
h;s%\\ /% X%p;x
s%(\\)? (/)?%\2 \1%gp
ba

Try it online!

basically a port of my sed answer to another challenge (see explanation there) which uses the engine from this answer

\$\endgroup\$
1
\$\begingroup\$

J, 25 bytes

' \/X'{~1(+2*|.)@=@i.@++:

Try it online!

Arrived at independently, but very similar approach to Adam's APL answer. J has a primitive = that returns the identity matrix when given a list of distinct number, making that part even shorter.

\$\endgroup\$
1
\$\begingroup\$

Rust, 150 bytes

|n|{let w=|e|" ".repeat(e);for i in 0..n{print!("{}\\{}/
",w(i),w(2*n-2*i-1))}print!("{}X
",w(n));for i in 0..n{print!("{}/{}\\
",w(n-i-1),w(2*i+1))}}

Attempt This Online!

\$\endgroup\$
1
\$\begingroup\$

Pip -l, 22 bytes

YEYa*2+1(" /\X"y*2+Ry)

Try It Online!

Explanation

YEYa*2+1(" /\X"y*2+Ry)­⁡​‎‎⁡⁠⁤‏⁠‎⁡⁠⁢⁡‏⁠‎⁡⁠⁢⁢‏⁠‎⁡⁠⁢⁣‏⁠‎⁡⁠⁢⁤‏‏​⁡⁠⁡‌⁢​‎‎⁡⁠⁢‏⁠‎⁡⁠⁣‏‏​⁡⁠⁡‌⁣​‎‎⁡⁠⁡‏‏​⁡⁠⁡‌⁤​‎‎⁡⁠⁤⁤‏⁠‎⁡⁠⁢⁡⁡‏⁠‎⁡⁠⁢⁡⁢‏‏​⁡⁠⁡‌⁢⁡​‎‎⁡⁠⁢⁡⁣‏⁠‎⁡⁠⁢⁡⁤‏⁠‎⁡⁠⁢⁢⁡‏‏​⁡⁠⁡‌⁢⁢​‎‎⁡⁠⁣⁡‏⁠‎⁡⁠⁢⁢⁢‏‏​⁡⁠⁡‌⁢⁣​‎‎⁡⁠⁣⁢‏⁠‎⁡⁠⁣⁣‏⁠‎⁡⁠⁣⁤‏⁠‎⁡⁠⁤⁡‏⁠‎⁡⁠⁤⁢‏⁠‎⁡⁠⁤⁣‏‏​⁡⁠⁡‌­
   a*2+1                ; ‎⁡Twice the argument plus 1
 EY                     ; ‎⁢Identity matrix of that size
Y                       ; ‎⁣Yank into y
               y*2      ; ‎⁤Multiply each number in y by 2
                  +Ry   ; ‎⁢⁡Add y reversed
                        ; ‎⁢⁡We now have a 2D array containing 2s on the main diagonal,
                        ; 1s on the main antidiagonal, 3 at the intersection,
                        ; and 0s elsewhere
        (            )  ; ‎⁢⁢Use that array as a (multi-)index into
         " /\X"         ; ‎⁢⁣this string
                        ; ‎⁢⁣Join each row and output them newline-separated (-l flag)
💎

Created with the help of Luminespire.

\$\endgroup\$
1
\$\begingroup\$

Go, 199 193 bytes

import."strings"
func f(n int)(s string){S,R:=" ",Repeat
for i:=range n{s+=R(S,i)+`\`+R(S,max(0,2*(n-i)-1))+`/
`}
s+=R(S,n)+`X
`
for i:=range n{s+=R(S,n-i-1)+`/`+R(S,max(0,2*i+1))+`\
`}
return}

Attempt This Online!

  • -6 by Go 1.22+ range-over-int.
\$\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.