views:

1219

answers:

12

The challenge

The shortest code by character count to output a a pattern of diamonds according to the input.

The input is composed of 3 positive numbers representing the size of the diamond and the size of the grid.

A diamond is made from the ASCII characters / and \ with spaces. A diamond of size 1 is:

/\
\/

The size of the grid consists from width and height of number of diamonds.

Test cases

Input:
    1 6 2
Output:
    /\/\/\/\/\/\
    \/\/\/\/\/\/
    /\/\/\/\/\/\
    \/\/\/\/\/\/

Input: 
    2 2 2
Output:
     /\  /\ 
    /  \/  \
    \  /\  /
     \/  \/ 
     /\  /\ 
    /  \/  \
    \  /\  /
     \/  \/ 

Input 
    4 3 1
Output:
       /\      /\      /\   
      /  \    /  \    /  \
     /    \  /    \  /    \
    /      \/      \/      \
    \      /\      /\      /
     \    /  \    /  \    /
      \  /    \  /    \  /
       \/      \/      \/

Code count includes input/output (i.e full program).

+2  A: 

Python - 138 chars

s,r,c=eval("input(),"*3)
x=range(s);o="";l="\/";i=0
for k in x+x[::-1]:y=" "*(s-1-k);o+=(y+l[i<s]+"  "*k+l[i>=s]+y)*c+"\n";i+=1
print o*r,

as a bonus it's also incredibly vulnerable to attack!

Claudiu
Does not read input!
Brian
it didn't say it should? it said input is part of the size of the program?
Claudiu
@Claudiu: Yes, input _handling_.
Cam
Read the last line of the challenge.
LiraNuna
fair enmough, fixed!
Claudiu
+3  A: 

Ruby - 115 bytes

a,b,c=gets.split.map &:to_i;puts (a...a+c*d=a*2).map{|y|(0...b*d).map{|x|x%d==y%d ?'\\':x%d==d-y%d-1?'/':' '}.to_s}
neoneye
neoneye
The parentheses around it should be unnecessary, though. A single space after `map` should suffice
Joey
+3  A: 

F#, 233 chars

let[|a;b;c|],(+),z,(!)=stdin.ReadLine().Split[|' '|]|>Array.map int,String.replicate," ",printfn"%s"
for r in 1..c do
 for n in 1..a do !(b+(a-n+z^"/"^2*n-2+z^"\\"^a-n+z))
 for n in 1..a do !(b+(n-1+z^"\\"^2*a-2*n+z^"/"^n-1+z))

Fun! A couple new bits for my F# code-golf arsenal:

  • using stdin rather than the cumbersome System.Console stuff
  • abusing operator overloading/redefinition
Brian
So has `stdin` always been in the F# library, or did you guys added just for golfing ;)
Juliet
:P I guess it has always been there for a long time, I only recently discovered it.
Brian
+3  A: 

JavaScript: 261 chars (Function)

function f(s,w,h){for(y=h,g=s*2;y--;){for(i=0,o=[];i<s;i++)for(x=0,o[i]=[],o[i+s]=[];x<w;x++){o[i][s-i-1+g*x]='/';o[i][s-i+i*2+g*x]='\\';o[i+s][g*x+i]='\\';o[i+s][g+g*x-i-1]='/'}for(a=0,z='';a<g;a++,console.log(z),z='')for(b=0;b<g*w;b++)z+=o[a][b]?o[a][b]:' '}}

JavaScript: 281 chars (Rhino Script with Standard Input/Output)

a=arguments;s=+a[0];w=+a[1];h=+a[2];for(y=h,g=s*2;y--;){for(i=0,o=[];i<s;i++)for(x=0,o[i]=[],o[i+s]=[];x<w;x++){o[i][s-i-1+g*x]='/';o[i][s-i+i*2+g*x]='\\';o[i+s][g*x+i]='\\';o[i+s][g+g*x-i-1]='/'}for(a=0,z='';a<g;a++,print(z),z='')for(b=0;b<g*w;b++)z+=o[a]?o[a][b]?o[a][b]:' ':' '}


Readable Rhino Version:

size = +arguments[0];
width = +arguments[1];
height = +arguments[2];

for (y = 0; y < height; y++) {
  o = [];
  for (i = 0; i < size; i++) {
    // Will draw the top and bottom halves of each diamond row 
    // in a single pass. Using array o[] to store the data:
    o[i] = [];
    o[i + size] = [];
    for (x = 0; x < width; x++) {
      // Draw the top half of the diamond row:
      o[i][(size - i - 1) + (size * 2 * x)] = '/';
      o[i][(size - i) + (i * 2) + (size * 2 * x)] = '\\';
      // Draw the bottom half of the diamond row:
      o[i + size][(size * 2 * x) + i] = '\\';
      o[i + size][(size * 2) + (size * x * 2) - i - 1] = '/';
    }
  }
  // Output the full diamond row to console from array o[]:
  for (a = 0; a < size * 2; a++) {
    z = "";
    for (b = 0; b < size * 2 * width; b++) {
      z += o[a] ? o[a][b] ? o[a][b] : ' ' : ' ';
    }
    print(z);
  }
}


Test Cases:

java org.mozilla.javascript.tools.shell.Main diamonds.js 4, 3, 2

   /\      /\      /\    
  /  \    /  \    /  \   
 /    \  /    \  /    \  
/      \/      \/      \ 
\      /\      /\      / 
 \    /  \    /  \    /  
  \  /    \  /    \  /   
   \/      \/      \/    
   /\      /\      /\    
  /  \    /  \    /  \   
 /    \  /    \  /    \  
/      \/      \/      \ 
\      /\      /\      / 
 \    /  \    /  \    /  
  \  /    \  /    \  /   
   \/      \/      \/   


java org.mozilla.javascript.tools.shell.Main diamonds.js 2, 6, 1

 /\  /\  /\  /\  /\  /\
/  \/  \/  \/  \/  \/  \
\  /\  /\  /\  /\  /\  /
 \/  \/  \/  \/  \/  \/ 


java org.mozilla.javascript.tools.shell.Main diamonds.js 1, 1, 1

/\
\/
Daniel Vassallo
A: 

Python - 126 chars

s,w,h=input();z=s*2;w*=z;h*=z;
print("%s"*w+"\n")*h%tuple(
    " \/"[(i/w%z==i%z)+((i/w+1)%z==-i%z)*2] for i in range(s*w,w*(h+s))
),

Line breaks and indentation added for clarity.

Amnon
And shoot you they did... (Just so you know, I wouldn't mod you down. I just won't mod you up until your code reads input...)
Pretzel
Ouch. Repented.
Amnon
+8  A: 

Golfscript - 57 chars 50 chars

~\:b;\:a,{[.a-~" "*'/'@' '*.'\\'4$]b*}%n*.-1%](*n*

57 chars:

~:c;:b;:a,{:§;b{" "a§)-*."/"" "§2**@'\\'\}*]}%n*.-1%]c*n*

Ventero
+10  A: 

Golfscript - 50 chars

~@:3,[{[.3-~' '*\' '*'/'\.'\\'4$]2$*}%n*.-1%]*n*\;
Nabb
Golfscript won again, yawn
M28
@M28: It used to be J :-)
Joey
+3  A: 

Perl - 161 (working program)

($s,$n,$m)=@ARGV;$i=$s;@a=qw(/ \\);--$a;do{$r.=sprintf("%${i}s".' 'x(($s-$i)*2)."%-${i}s",@a)x$n."\n";$i=1,$a=-$a,@a=@a[-1,0]unless$i+=$a}while$i<=$s;print$r x$m

Perl - 119 (second variant) It's more cool idea... I'm using ability of interpolation of arrays to strings.

($s,$n,$m)=@ARGV;map{@a=@b=('')x$s;$a[-$_]='/';$b[$_-1]='\\';$z.="@a@b"x$n."\n";$x.="@b@a"x$n."\n"}1..$s;print"$z$x"x$m

Full second variant:

    my ($s,$n,$m) = @ARGV; # take command line parameters
    my ($z,$x); # variables for upper and lower parts of diamond
    for (1..$s) { # lines of half diamond
        my (@a,@b); # temporary arrays
        @a=@b=('')x$s; # fill arrays with empty strings
        $a[-$_]='/'; # left part of diamond
        $b[$_-1]='\\'; # rigth part of diamond
        $z .= "@a@b" x $n . "\n"; # adding n upper parts of diamonds
        $x .= "@b@a" x $n . "\n"; # adding n lower parts of diamonds
    }
    print "$z$x" x $m; # "$z$x" - horizontal line of diamonds
zloyrusskiy
+5  A: 

Mathematica - Pure Functional

A pure functional approach

f[a_, b_, c_]:=Grid[Array[If[(s = FindInstance [Abs[p =(2((2k+1)a + #1)-1)]   
                  == (2#2-1), k, Integers])!={}, 
                  {"\\", , "/"}[[Sign[p] /. s[[1]]]]] &, 2 a {c, b}]]

Note that Mathematica is solving an equation for finding the function of the straight lines in the diamonds. It's a Diophantine equation in k:

 Abs[(2((2 * k + 1)a + x)-1)] == (2 * y -1) (only find solutions for Integer k)

For each element, and then, if a solution is found, deciding the "\" or "/" based on the sign of the lhs of the equation. (in the {"\", , "/"}[[Sign[p] /. s[[1]] part )

Usage

f[2, 2, 2]

Or

Grid[f[2, 2, 2], f[1, 6, 2], f[4, 3, 3]]  

for generating all test cases at once

belisarius
Going through solving an equation is wonderfully convoluted. Me likes :-)
Joey
@Johannes Rössel here is obviously overshooting, just for amusement, but it's a very useful approach when the boundary conditions change the form of the equations to be solved
belisarius
+2  A: 

Haskell, 136 chars

r=readLn
main=do
 n<-r;w<-r;h<-r;let d=2*n;y?x|mod(x+y-1)d==n='/'|mod(x-y)d==n='\\'|True=' '
 mapM putStrLn[map(y?)[1..d*w]|y<-[1..d*h]]

Usage:

$ ./a.out
1
6
2
/\/\/\/\/\/\
\/\/\/\/\/\/
/\/\/\/\/\/\
\/\/\/\/\/\/
KennyTM
+4  A: 

Windows PowerShell, 124 123 121 119 116 112 chars

$s,$w,$h=-split$input
$(($a=1..$s|%{$x=' '*($s-$_--)
"$x/$('  '*$_)\$x"*$w})
$a|%{-join($_[-$s..($s-1)])*$w})*$h

If we allow the input to span three lines instead of being generally whitespace-separated we can get it down to 109:

$s,$w,$h=@($input)
$(($a=1..$s|%{$x=' '*($s-$_--)
"$x/$('  '*$_)\$x"*$w})
$a|%{-join($_[-$s..($s-1)])*$w})*$h

As arguments to the script (from within PowerShell) it'd be 105 bytes:

$s,$w,$h=$args
$(($a=1..$s|%{$x=' '*($s-$_--)
"$x/$('  '*$_)\$x"*$w})
$a|%{-join($_[-$s..($s-1)])*$w})*$h

This would then be called like this:

PS> .\diamond.ps1 2 2 2
Joey
Shell, lolcakes.
DeadMG
+2  A: 

Python, 125 chars

s,c,r=input()
l=[c*('%*c%*c%*s'%(s-i,47,2*i+1,92,s-i-1,''))for i in range(s)]
print'\n'.join(r*(l+[i[::-1]for i in l[::-1]]))

Input should be provided in comma-separated form, e.g. 1,6,2:

D:\CodeGolf> DiamondPattern.py

1,6,2
/\/\/\/\/\/\
\/\/\/\/\/\/
/\/\/\/\/\/\
\/\/\/\/\/\/

PS. if you prefer input separated with spaces (1 6 1), for the price of 21c replace first line with:

s,c,r=map(int,raw_input().split())

If you prefer command line arguments, for 25c more you can have

import sys;s,c,r=map(int,sys.argv[1:])
Nas Banov
For fairness, it's common to follow the examples - changing the input specification is usually frowned upon.
LiraNuna
@LiraNuna: Claudiu solution in Python does not follow the problem example either - one is supposed to enter the 3 numbers on 3 different lines. Perl solution proposed does not do I/O at all but uses cmd line arguments - do you take issue with these too?
Nas Banov