Code Golf: Diamond Pattern
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).
Golfscript - 50 chars
~@:3,[{[.3-~' '*\' '*'/'\.'\\'4$]2$*}%n*.-1%]*n*\;
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*
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
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
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}
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 cumbersomeSystem.Console
stuff - abusing operator overloading/redefinition
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
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
/\
\/
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:])
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!
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
/\/\/\/\/\/\
\/\/\/\/\/\/
/\/\/\/\/\/\
\/\/\/\/\/\/
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.
Clojure
(def ^:dynamic fc \/)
(def ^:dynamic sc \\)
(defn spaces [size]
(apply str (repeat size " ")))
(defn linestr[size line-no]
(let [sp (spaces size )
fh (doto (StringBuilder. sp)
(.setCharAt (- size line-no) fc)
(.toString))
sh (.replace (apply str (reverse fh)) fc sc) ]
(str fh sh)))
(defn linestr-x[number size line-no]
(apply str (repeat number (linestr size line-no))))
(defn print-all[number size]
(loop [line-no 1 lines []]
(if (> (inc size) line-no)
(recur (inc line-no) (conj lines (linestr-x number size line-no)))
lines)))
(defn diamond[number size]
(let [fh (print-all number size) ]
(binding [fc \\ sc \/]
(flatten [fh (reverse (print-all number size)) ]))))
(defn print-diamond[size cols rows]
(doseq [x (flatten (repeat rows (diamond cols size))) ]
(println x)))
(print-diamond 4 3 1)
user=> (print-diamond 1 10 3 )
/\/\/\/\/\/\/\/\/\/\
\/\/\/\/\/\/\/\/\/\/
/\/\/\/\/\/\/\/\/\/\
\/\/\/\/\/\/\/\/\/\/
/\/\/\/\/\/\/\/\/\/\
\/\/\/\/\/\/\/\/\/\/
精彩评论