21
\$\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\$
  • \$\begingroup\$ Sandbox; related \$\endgroup\$ – Stephen Jun 8 '17 at 18:16
  • 6
    \$\begingroup\$ ... I'm just gonna wait for the charcoal 1-byte solution. \$\endgroup\$ – Leaky Nun Jun 8 '17 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 Jun 8 '17 at 18:17
  • \$\begingroup\$ Also related \$\endgroup\$ – Value Ink Jun 8 '17 at 20:02
  • \$\begingroup\$ Related, related \$\endgroup\$ – xnor Jun 8 '17 at 20:05

31 Answers 31

6
\$\begingroup\$

Canvas, 3 bytes

╵\┼

Try it here!

half of the size of the Charcoal answer :D

╵    increment the input
 \   create a diagonal that long
  ┼  and quad-palindromize, mirroring what's required, with 1 overlap;
     This overlaps the `/` and `\`, resulting in `X`
\$\endgroup\$
  • \$\begingroup\$ Dang that is short. \$\endgroup\$ – dylnan Apr 5 '18 at 20:55
  • \$\begingroup\$ :||||||| +1 for golfiness \$\endgroup\$ – ASCII-only Apr 6 '18 at 5:51
  • \$\begingroup\$ also i wonder if stack-based languages basically always outgolf other languages? \$\endgroup\$ – ASCII-only Apr 6 '18 at 7:59
  • \$\begingroup\$ @ASCII-only jelly \$\endgroup\$ – Okx Apr 6 '18 at 8:19
  • \$\begingroup\$ @Okx *stack-based/tacit \$\endgroup\$ – ASCII-only Apr 6 '18 at 8:38
22
\$\begingroup\$

Charcoal, 6 bytes

PX⁺¹NX

Your nonsense ain't stopping me ;)

Try it online!

\$\endgroup\$
  • 2
    \$\begingroup\$ The TIO looks... interesting when you put a really big number into it \$\endgroup\$ – Stephen Jun 8 '17 at 18:28
  • \$\begingroup\$ @StephenS Only because of wrapping. \$\endgroup\$ – Okx Jun 8 '17 at 18:28
  • \$\begingroup\$ I know, it's just intuitive that all of the wrapped `\`s get left aligned (because spaces don't get wrapped) \$\endgroup\$ – Stephen Jun 8 '17 at 18:29
  • \$\begingroup\$ 5 chars (postdates challenge) just to get that little bit closer to Canvas \$\endgroup\$ – ASCII-only Apr 6 '18 at 7:10
16
\$\begingroup\$

JavaScript (ES6), 79 bytes

Uses a recursive function g that walks through a grid and builds the output character by character.

n=>(g=x=>`/\\ X
`[~x?x-y?x+y-w&&2:x-n?1:3:4]+(~y?g(~x--?x:y--&&w):''))(y=w=n*2)

How?

Both variables x and y iterate from 2n to -1, where n is the input.

For each position (x, y) in the grid, we pick one of these characters:

  • 0: /
  • 1: \
  • 2: space
  • 3: X
  • 4: newline

using the following tests:

  • ~x: Falsy if x == -1: we've reached an end of line.
  • x-y: Falsy if x == y: we're located on the anti-diagonal.
  • x+y-w: Falsy if x + y == w: we're located on the diagonal.
  • x-n: Falsy if x == n: because this test is only performed when x == y, this means that we're located in the exact center of the grid.

and the following decision tree:

decision tree

Demo

let f =

n=>(g=x=>`/\\ X
`[~x?x-y?x+y-w&&2:x-n?1:3:4]+(~y?g(~x--?x:y--&&w):''))(y=w=n*2)

console.log(f(0))
console.log(f(1))
console.log(f(4))

\$\endgroup\$
  • 4
    \$\begingroup\$ This is... JavaScript? What has happened to you, old friend. \$\endgroup\$ – Roberrrt Jun 10 '17 at 7:17
13
\$\begingroup\$

MATL, 16 bytes

'\/X 'iEQXytEP+)

Try it online!

Consider input 2 as an example. The stack is shown here upside down, i.e. lower elements are the ones most recently pushed.

'\/X '  % Push this string
        %   STACK: '\/X '
iEQ     % Input a number, n. Multiply by 2, add 1: gives 2*n+1
        %   STACK: '\/X '
                   5
Xy      % Identity matrix of that size
        %   STACK: '\/X '
                   [1 0 0 0 0;
                    0 1 0 0 0;
                    0 0 1 0 0;
                    0 0 0 1 0;
                    0 0 0 0 1]
tEP     % Duplicate, multiply each entry by 2, flip vertically
        %   STACK: '\/X '
                   [1 0 0 0 0;
                    0 1 0 0 0;
                    0 0 1 0 0;
                    0 0 0 1 0;
                    0 0 0 0 1]
                   [0 0 0 0 2;
                    0 0 0 2 0;
                    0 0 2 0 0;
                    0 2 0 0 0;
                    2 0 0 0 0]
+       % Add the two matrices
        %   STACK: '\/X '
                   [1 0 0 0 2;
                    0 1 0 2 0;
                    0 0 3 0 0;
                    0 2 0 1 0;
                    2 0 0 0 1]
)       % Index into the string. Indexing is 1-based and modular, so 1 picks
        % the first character ('\'), ..., 0 picks the last (space)
        %   STACK: ['\   /';
                    ' \ / ';
                    '  X  ';
                    ' / \ ';
                    '/   \']
        % Implicit display
\$\endgroup\$
  • \$\begingroup\$ I wanted to do something similar in Octave, but you beat me to it and in MATL it is even shorter than Octave, so great work! \$\endgroup\$ – Michthan Jun 9 '17 at 8:48
  • \$\begingroup\$ @Michthan Thanks! An Octave version would definitely be worth posting too. My atempt is at 38 bytes, what's yours? \$\endgroup\$ – Luis Mendo Jun 9 '17 at 11:50
  • \$\begingroup\$ I have been thinking about it all weekend and couldn't find a more efficient way than the one you are using here.. So all credits should go to you for an octave version. \$\endgroup\$ – Michthan Jun 12 '17 at 7:12
6
\$\begingroup\$

C, 108 106 bytes

a;g(n){for(int b=2*n,i=1,c=47;a+=i;b?puts(""):(i=-i,c=92),b-=2*i)printf("%*c%*c",a,b?c+45*i:88,b,b?c:10);}

Try it online!

(−2 golfing thanks to MD XF)

It prints two characters (at first, c = 47, which is a slash, and c + 45, which is a backslash; then they are swapped) with a dynamic field width.

The field widths start at 1 and 2n, and at each iteration, the first width is incremented by 1, and the second one is decremented by 2.

When the second field width becomes 0, it outputs 'X' and a newline instead of the regular characters, and reverses the direction of increments (i). A newline is printed for all other lines separately (puts("")).

\$\endgroup\$
  • \$\begingroup\$ 106 bytes: a,b,i=1,c;g(n){for(b=2*n,c=47;a+=i;b?puts(""):(i=-i,c=92),b-=2*i)printf("%*c%*c",a,b?c+45*i:88,b,b?c:10);} Try it online! \$\endgroup\$ – MD XF Jun 10 '17 at 2:56
  • \$\begingroup\$ Thanks for the idea! I only used a part of it to make sure the code can be called twice. \$\endgroup\$ – anatolyg Jun 10 '17 at 14:49
5
\$\begingroup\$

shortC, 111 bytes

s(x){Wx--)R" ")}j;f(x){O;j<x;j++)s(j),P92),s((x-j)*2-1),R"/\n");s(x);R"X\n");Wj--)s(j),P47),s((x-j)*2-1),R"\\\n

Based on my C answer. Conversions:

  • R -> printf(
  • P -> putchar(
  • W -> while(
  • O -> for(
  • Auto-inserted closing ");}

This also uses ASCII codes for \ and /.

Try it online!

\$\endgroup\$
5
\$\begingroup\$

Python 2, 81 bytes

r=range(2*input()+1)
for i in r:print''.join(' \/X'[i==j::2][r[~i]==j]for j in r)

Try it online!

\$\endgroup\$
5
\$\begingroup\$

C, 168 155 150 bytes

-5 thanks to Computronium

#define p printf(
s(x){while(x--)p" ");}j;f(x){for(;j<x;j++)s(j),p"\\"),s((x-j)*2-1),p"/\n");s(x);p"X\n");while(j--)s(j),p"/"),s((x-j)*2-1),p"\\\n");}

Can certainly be golfed; I'm doing so. Try it online!

Ungolfed:

int space(int x)
{
    while (x--)
        putchar(' ');
}

int f(int x)
{
    for (int j = 0; j < x; j++) {
        space(j);
        printf("\\");
        space((x-j)*2-1);
        printf("/\n");
    }

    space(x);
    puts("X");

    while (j--) {
        space(j);
        putchar('/');
        space((x-j)*2-1);
        printf("\\\n");
    }
}
\$\endgroup\$
  • 2
    \$\begingroup\$ You can lose 5 characters by defining p to be "printf(" instead of "printf". \$\endgroup\$ – Computronium Jun 9 '17 at 10:17
3
\$\begingroup\$

V, 21 bytes

éXÀñ>HÄÒ r\Á/YGpr/$r\

Try it online!

Hexdump:

00000000: e958 c0f1 3e48 c4d2 2072 5cc1 2f59 4770  .X..>H.. r\./YGp
00000010: 722f 2472 5c                             r/$r\

Explanation:

éX                      " Insert an 'X'
  Àñ                    " Arg1 times:
    >H                  "   Add a space to every line.
                        "   Conveniently, this also puts us on the first line
      Ä                 "   Duplicate this line
       Ò                "   And replace the whole line with spaces
         r\             "   Replace the first char with '\'
           Á/           "   Append a '/' char
             Y          "   Yank this line
              G         "   Move to the last line
               p        "   And paste the line we yanked
                r/      "   Replace the first character with a '/'
                  $     "   Move to the end of the line
                   r\   "   And replace the last character with a '\'

Essentially, we have Insert an X, n times extend the slashes.

But it's not quite that simple because we also have to add the slashes the first time. If the slashes were already there, we could write extend the slashes as:

>HÄX2pGÙX2p

Which would save us 6 bytes.

\$\endgroup\$
3
\$\begingroup\$

C#, 157 122 120 bytes

_=d=>"".PadLeft(d)
a=>{var s=_(a)+"X\n";for(int i=0;++i<=a;)s=$@"{_(a-i)}\{_(i*2-1)}/
{s+_(a-i)}/{_(i*2-1)}\
";return s;}

Ungolfed version:

 Func<int, string> _ = (d) =>"".PadLeft(d);
        Func<int, string> func = a => {

            var s = _(a) + "X\n";

            for (int i = 0; ++i <= a;) {

                s = $@"{_(a - i)}\{_(i * 2 - 1)}/
{s + _(a - i)}/{_(i * 2 - 1)}\
";

            }
            return s;
        };
\$\endgroup\$
  • 1
    \$\begingroup\$ You can move the other anonymous function out of the first and then include it as _=d=>new string(' ',d); (note no need for the braces around d). You can remove the curly braces around the for loop. Make use of a verbatim string with so you don't need to escape all of the backslashes. If you set i=0 then you can do ++i<a+1 and remove the i++. \$\endgroup\$ – TheLethalCoder Jun 9 '17 at 8:41
  • \$\begingroup\$ Also with verbatim strings you don't need to include \n an actual line feed will work, though I'm not sure if you will then be able to remove the braces around the for loop, you would have to try. \$\endgroup\$ – TheLethalCoder Jun 9 '17 at 8:42
  • 1
    \$\begingroup\$ After applying the first suggestion by TheLethalCoder, you can also replace the ++i<a+1 for ++i<=a EDIT You can also save 4 more bytes by changing the Func from new string(' ',d) to "".PadLeft(d) \$\endgroup\$ – auhmaan Jun 9 '17 at 8:54
  • \$\begingroup\$ Thanks for suggestions, @TheLethalCoder am I allowed to declare 2 functions like that, wouldn't that make a lot of C# golfing shorter if we can do that? \$\endgroup\$ – LiefdeWen Jun 9 '17 at 9:22
  • 1
    \$\begingroup\$ @StefanDelport There's a meta discussion about it somewhere but as it stands I believe you can as long as you show the functions name. In this case _. \$\endgroup\$ – TheLethalCoder Jun 9 '17 at 9:36
3
\$\begingroup\$

Mathematica, 71 bytes

(Partially inspired by Jenny_mathy's 104-byte solution)

""<>#&/@(#"\\"+Reverse@#"/"&@IdentityMatrix[2#+1]/.{0->" ",a_+_->"X"})&

Returns a list of strings.

Explanation: IdentityMatrix[2#+1] makes a matrix of the right size with 1s along the diagonal and 0s elsewhere. Next, we multiply it by "\\" (an escaped backslash), which makes it a matrix with backslashes along the diagonal and 0s elsewhere, since of course 1 times backslash is backslash and 0 times backslash is 0. We add this to "/" times its reverse to make the X shape. We're nearly done, except there are still 0s everywhere, and the middle is "\\" + "/". We fix these two things by substituting " " for 0 and "X" for a_+_, which matches any sum of two things (like _+_ should, except Mathematica is too clever for its own good and interprets that as 2 times _). Finally, ""<>#&/@ turns this into a list of strings.

\$\endgroup\$
3
\$\begingroup\$

Java (OpenJDK 8), 135 bytes

i->{int k=0,j,l=2*i+1;String[]s=new String[l];for(;k<l;k++)for(s[k]="",j=0;j<l;j++)s[k]+=j==k?j==i?"X":"\\":j==l-1-k?"/":" ";return s;}

Lambda expression that takes and integer and returns an array of Strings

Try it online!

Ungolfed:

i->{
    int k=0,j,l=2*i+1;                // Some variables to be used
    String[]s=new String[l];            // Return array (size 2*i+1)
    for(;k<l;k++)                       // For each array entry
        for(s[k]="",j=0;j<l;j++)        // Set each character to 
            s[k]+=j==k?j==i?"X":"\\"    // \ or X if it's the jth character of the jth row
                 :j==l-1-k?"/"          // / if it's the opposite char
                 :" ";                  // else blank
    return s;
}
\$\endgroup\$
3
\$\begingroup\$

T-SQL, 201 bytes

DECLARE @ INT SELECT @=a FROM t DECLARE @i INT=@
WHILE @>0BEGIN PRINT SPACE(@i-@)+'\'+SPACE(2*@-1)+'/'SET @-=1 END
PRINT SPACE(@i)+'X'WHILE @<@i BEGIN SET @+=1 PRINT SPACE(@i-@)+'/'+SPACE(2*@-1)+'\'END

Formatted:

DECLARE @ INT 
SELECT @=a FROM t 
DECLARE @i INT=@
WHILE @>0
    BEGIN
        PRINT SPACE(@i-@)+'\'+SPACE(2*@-1)+'/'
        SET @-=1 
    END
PRINT SPACE(@i)+'X'
WHILE @<@i 
    BEGIN 
        SET @+=1 
        PRINT SPACE(@i-@)+'/'+SPACE(2*@-1)+'\'
    END

Input is via column a in named table t, per our guidelines.

\$\endgroup\$
3
\$\begingroup\$

Ruby, 66 bytes

Recursive function.

f=->x{?X[x]||"\\#{s=' '*(2*x-1)}/
#{f[x-1].gsub /^/,' '}
/#{s}\\"}

Try it online!

Explanation

f=->x{                  # Start of recursive function named `f`
      ?X[x]||           # Return 'X' if x==0, otherwise the following:
"\\#{s=' '*(2x-1)}/     #  Top edge of the cross. Save no. of spaces needed
#{f[x-1]                #  Get result of f[x-1]
        .gsub /^/,' '}  #  Regex sub to left-pad every line w/ a space
/#{s}\\"                #  Bottom edge of cross (insert saved no. of spaces)
\$\endgroup\$
3
\$\begingroup\$

Jelly, 24 17 bytes

Ḥ‘=þ`µḤ+Uị“/\x ”Y

Try it online!

How it Works

Ḥ‘=þ`µḤ+Uị“/\x ”Y   main link, input a
Ḥ‘                  input doubled and incremented
   þ                Make a table: Apply  
  =                 "equals"/ to      
    `               each element in range(2a+1) cartesian multiplied with itself.
                      eg. For input 1: [1=1,1=2,1=3],[2=1,2=2,2=3],[3=1,3=2,3=3]      
     µ              on this array:
       +            add: 
      Ḥ             double of it to
        U           its reverse (changes south-east to north-west)
         ị“/\x ”    index into the string "/\x " to get the right characters
                Y   join by newlines for the final output.

-6 bytes thanks to @LeakyNun and -1 byte with an additional improvement

\$\endgroup\$
2
\$\begingroup\$

Batch, 201 bytes

@echo off
set s= /
for /l %%i in (2,1,%1)do call set s=  %%s%%
set s=\%s%
for /l %%i in (-%1,1,%1)do call:c
exit/b
:c
echo %s%
set s=%s:\ = \%
set s=%s:X =/\%
set s=%s:\/=X %
set s=%s: /=/ %

Starts by building up the top line, then after printing each line, moves the \ right one space and the / left once space, making sure that they make an X in the middle.

\$\endgroup\$
2
\$\begingroup\$

PHP, 115 bytes

for(;$i<$c=1+2*$argn;$b?:$t[$i-1]="\\".!$t[$c-$i]="/",$o.="$t\n")$t=str_pad(" X"[$b=$argn==+$i++],$c," ",2);echo$o;

Try it online!

\$\endgroup\$
2
\$\begingroup\$

Retina, 74 bytes

.+
$* X
+`^ ( *).( *)
$1\  $2/¶$&
+`¶ ( *).( *).?$
$&¶$1/  $2\
m` (\W)$
$1

Try it online! Explanation:

.+
$* X

Place the X.

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

Starting at the X, working upwards, place a \ diagonally to the left each time. Also place a / two more spaces after the / than last time.

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

Starting at the X, working downwards, place a / diagonally to the left each time. Also place a \ two more spaces after the / than last time.

m` (\W)$
$1

The number of spaces between the two diagonals needs to be odd, so the last space on each line (except the original X line) is deleted.

\$\endgroup\$
2
\$\begingroup\$

Mathematica, 131 bytes

(F[x_,y_]:=Table[x<>StringJoin@Table[" ",i]<>y,{i,1,#*2,2}];Column[Join[Reverse@F["\\","/"],{"X"},F["/","\\"]],Alignment->Center])&


Mathematica, 104 bytes

here is another approach using Grid

(S=DiagonalMatrix[Table["\\",r=2#+1]];Table[S[[r+1-i,0+i]]="/",{i,r}];S[[#+1,#+1]]="X";Grid@S/. 0->" ")&
\$\endgroup\$
2
\$\begingroup\$

APL (Dyalog), 25 bytes

Requires ⎕IO←0 which is default on many systems.

' \/X'[(⊢+2×⌽)∘.=⍨⍳1+2×⎕]

Try it online!

' \/'[] index the string with

 get input

 multiply by two

1+ add one

 than many integers

∘.=⍨ equality table (i.e. identity matrix; NW-SE diagonal)

() apply the following tacit function on that

   the argument

  + plus

   two times

   the horizontally mirrored argument (i.e. NE-SW diagonal)

\$\endgroup\$
2
\$\begingroup\$

Bash, 138 bytes

for i in `seq $1;seq $[$1-1] -1 1`
{ $[a++]
printf "%*c%*s\n" `echo ' '$i $[a>$1?1:2] $[($1-i)*2]' '$[a<$1?1:2]`
}|sed 's/22/X/'|tr 12 /\\

Try it online!

Really long, bash heates '\ and /'

Less golfed

 for i in {1..10} {9..1};{
   $[a++];                      #argument as padding, prints 1 for \ and 2 for /
   printf "%*c%*s\n" `echo ' '$i $[a>$1?1:2] $[($1-i)*2]' '$[a<$1?1:2]`;
  }|sed 's/22/X/g' | tr 12 /\\
\$\endgroup\$
2
\$\begingroup\$

Python 2, 95 84 bytes

-10 bytes thanks to @FelipeNardiBatista

f=lambda s,i='\n':i+(s and'\\'+'  '*~-s+' /'+f(s-1,i+' ')+i+'/'+'  '*~-s+' \\'or'X')

Try it online!

\$\endgroup\$
  • 1
    \$\begingroup\$ 85 bytes: TIO \$\endgroup\$ – Felipe Nardi Batista Jun 9 '17 at 14:10
  • \$\begingroup\$ @FelipeNardiBatista thanks a lot \$\endgroup\$ – ovs Jun 9 '17 at 15:02
2
\$\begingroup\$

05AB1E, 21 bytes

F'\Nú}'X¹ú).B€.∞ø€.∞»

Try it online!

\$\endgroup\$
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\$
  • \$\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 Jun 8 '17 at 21:25
  • \$\begingroup\$ Currently on Linux mono is anoying \$\endgroup\$ – polyglotrealIknow Jun 8 '17 at 21:27
  • \$\begingroup\$ mono doesn't cares about Option Strict, or at least thats what I think \$\endgroup\$ – polyglotrealIknow Jun 8 '17 at 21:29
  • \$\begingroup\$ Oh it seemed functions != for loops thanks for the golf \$\endgroup\$ – polyglotrealIknow Jun 8 '17 at 21:36
  • \$\begingroup\$ Isn't Option Strict off by default? It is in Visual Studio at least \$\endgroup\$ – Stephen Jun 8 '17 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\$

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.