Take the 2-minute tour ×
Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. It's 100% free, no registration required.

The following code, written in coffescript, will print an X on the screen.

Can this be written in fewer lines of code in coffescript (or any other language - it would be interesting to see)?

size  = 10
for i in [1..size]
    t = ""
    for z in [1..size]
        if z == i || z == (size - i)
            t +="x"
        else
            t += "  "
    console.log t
share|improve this question
add comment

migrated from stackoverflow.com Feb 11 '12 at 15:06

This question came from our site for professional and enthusiast programmers.

18 Answers

I believe 1 is sufficient.

console.log ({true:"x",false:" "}[t == i || t == (10 - i)] for i in [1..10]).join("") for t in [1..10]
share|improve this answer
add comment

Groovy

s=11; for(r in 1..s){for(c in 1..s){print((r==c||r==(s-c+1))?'x':' ')}; println""}

Also renaming size to s and sucking out most unnecessary spaces.

share|improve this answer
add comment

Dyalog APL - 26 Characters

Prints a 9x9 'X' using 1's and 0's

∨/{9 1⍴{⍵⍷⍳9}¨⍵}¨(⌽⍳9)(⍳9)

If you require variable input:

x←⎕⋄∨/{x 1⍴{⍵⍷⍳x}¨⍵}¨(⌽⍳x)(⍳x)

A shorter solution by marinus (14 Characters)

k∨⌽k←0=-/¨⍳2⍴9

And if you would like it to be spaces and X's (25 Characters)

' X'[1+k∨⌽k←0=-/¨⍳2⍴9]
share|improve this answer
1  
k∨⌽k←0=-/¨⍳2⍴9 (or k∨⌽k←0=-/¨⍳2⍴⎕) –  marinus Jan 6 '13 at 21:07
    
@marinus Very nice –  MrZander Jan 7 '13 at 0:07
1  
Um, ' X'[1+k∨⌽k←0=-/¨⍳2⍴9]. [] can take an argument of any shape and will replace each scalar by the value in the given vector at that index. –  marinus Jan 16 '13 at 2:49
    
@marinus Even better. I am still quite new at APL. –  MrZander Jan 16 '13 at 19:15
    
APL always feels like cheating for these types of things. :P +1 –  Shmiddty Feb 13 '13 at 22:03
add comment

Yep.

size  = 10; for i in [1..size] { t = ""; for z in [1..size] { if (z == i || z == (size - i)) { t +="x" } else { t += "  " } } console.log t }

You can still use semicolons in CS, it's just discouraged :P

share|improve this answer
add comment

I'm not familiar with coffeescript, but in almost any language you can cut your if/else statement down into one line:

t += (z == i || z == (size - i)) ? "x" : "  "
share|improve this answer
    
this is not true for coffescript: coffeescript.org/#conditionals –  max taldykin Feb 11 '12 at 11:55
    
on the second thougth, yes, in coffeescript if statement is an expression, so you can write the code like yours. –  max taldykin Feb 11 '12 at 11:58
add comment

Mostly just being silly, using an approach of "simplest way to pass the test":

data = ["x              x", "  x          x", "    x      x", "      x  x", "        x", "      x  x", "    x      x", "  x          x", "x              x"];
for line in data
  console.log line
share|improve this answer
add comment
size = 10
a = ((if i in [n, size - n] then "x" else " ") for i in [1..size] for n in [1..size])
console.log a.map((x) -> x.join ' ').join '\n'
share|improve this answer
add comment

C++ - in 1 Loop

int main() {
    for(int i=0;i<10;i++) {
    char a[11]="          ";
    a[i]=a[9-i]='x';
    cout<<a<<endl;
    }
return 0;
}
share|improve this answer
add comment

J, 26

u:32+88*(=+.(=8&-))"0/~i.9

9x9 'X' using x's.

share|improve this answer
add comment

Perl (49+1)

requires -E for say, counted in score

perl -E '$w=8;@;=($")x$w,@;[$_,$w-$_]=(X,X),say@;for 0..$w'

share|improve this answer
add comment

That can be six lines in Haskell, seven if you define separator:

xpart len n 
 | n*2-1 < len = concat (replicate (n-1) " ") ++ "x" ++ concat (replicate (len-n-n) " ") ++"x"++ concat (replicate (n-1) " ") ++ eol
 | n*2-1 ==len = concat (replicate (n-1) " ") ++ "x"++ concat (replicate (n-1) " ") ++ eol
 | n*2-1 > len = xpart len (len-n+1)
 where eol = "\n"

main = do
  putStrLn $ concat $ [xpart 10 n | n <-[1..10]]
share|improve this answer
2  
as always, one line is enough to write this in haskell: let n = 10 in putStrLn $ unlines[concat[if i==j || n-2-i==j then "x" else " " | j<-[0..2*(n-1)-1]] | i<-[0..n-2]] –  max taldykin Feb 11 '12 at 11:53
add comment

Scala

val s=11;(1 to s).map{i=>(1 to s).map{j=> print (if (i==j || j==s-i) "x" else "  ")};println()}

I reduced the name of size to s, and used map instead of foreach, to make it fit better on the screen.

share|improve this answer
add comment

k

Prints to STDOUT. This can probably be shorter:

{-1'" X"@(|'').(::;|:')@\:t#,/i,'(t:2##i:x#1)#0;}

eg.

k){-1'" X"@(|'').(::;|:')@\:t#,/i,'(t:2##i:x#1)#0;}7
X     X
 X   X
  X X
   X
  X X
 X   X
X     X
share|improve this answer
add comment

Groovy - 69 56 char

11.times{s=[" "]*10;s[it]=s[10-it]='x';println s.join()}

old version

k=(0..10);k.each{r->k.each{print(r==it||r==10-it?'x':' ')};println""}
share|improve this answer
add comment

Javascript 71

for(i=s=10;--i;console.log(x))for(j=s,x='';--j;)x+=j==i||j==s-i?'x':' '
share|improve this answer
add comment

Ruby - 60

11.times{|­i|s=" "*10;­s[i]=s[10-­i]='X';put­s s+"\n­"}

Readable Version:

11.times { |­i|
    s = " " * 10
    s[i] = s[10-­i] = 'X'
    put­s s + "\n­"
}

Pretty similar to the Groovy-version

share|improve this answer
add comment

VBA, 75

For i=0 To 8:z="":For j=0 To 8:z=z & IIf(i=j or i=8-j,"X"," "):Next:?z:Next
share|improve this answer
add comment

Perl (59+1)

Must be run like this (you must provide the size of the cross) :

./cross.pl -w=8

Where cross.pl is :

#!/bin/perl -s
for($x="xx
";$w--;$x=~s/x/x  /%s/^/ /gm){$_="$x$_$x"}print

Nice move chinese perl goth : your (49+1) is far better...

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.