Chessboard pattern
Golfscript - 17 chars
~:N,{"X "N*>N<n}%
Analysis
~
convert input to an int
:N
store in the variable N
,{...}
for each value of [0...N-1]
"X "N*
repeat "X " to give a string of N*2 characters
>
take the substring starting from the loop index...
N<
...ending N characters later
n
put a newline a the end of each string
Perl, 41 40
for$i(1..$_){say substr" X"x$_,$i%2,$_}
Perl 5.10 or later, run with perl -nE 'code'
(n
counted in code size)
Sample output:
$ perl -nE'for$i(1..$_){say substr" X"x 40,$i%2,$_}' <<<5
X X X
X X
X X X
X X
X X X
$ perl -nE'for$i(1..$_){say substr" X"x 40,$i%2,$_}' <<<8
X X X X
X X X X
X X X X
X X X X
X X X X
X X X X
X X X X
X X X X
Pyth, 13 chars
Note: Pyth is much too new to be eligible to win. However, it was a fun golf and I thought I'd share it.
VQ<*QX*d2N\XQ
Try it here.
How it works:
Q = eval(input())
VQ for N in range(Q):
< Q [:Q]
*Q (Q* )
X*d2N\X assign_at(" "*2, N, "X")
Basically, this uses X
to generate "X "
or " X"
alternately, then repeats that string Q
times, and takes its first Q
characters. This is repeated Q
times.
How does the X
(assign at) function work? It takes the original string, " "
in this case, an assignment location, N
in this case, and a replacement character, "X"
in this case. Since Pyth's assignments are modular, this replaces the space at location N%2
with an X
, and returns the resultant string, which is therefore "X "
on the first, third, etc. lines, and " X"
on the others.