Code Golf:钻石图案

时间:2010-06-06 05:02:38

标签: language-agnostic code-golf rosetta-stone

挑战

按字符数排序的最短代码,根据输入输出钻石图案。

输入由3个正数组成,表示钻石的大小和网格的大小。

钻石由ASCII字符/\制成,带有空格。 1号钻石是:

/\
\/

网格的大小由钻石数量的宽度和高度组成。

测试用例

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

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

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

代码计数包括输入/​​输出(即完整程序)。

13 个答案:

答案 0 :(得分:10)

Golfscript - 50个字符

~@:3,[{[.3-~' '*\' '*'/'\.'\\'4$]2$*}%n*.-1%]*n*\;

答案 1 :(得分:8)

Golfscript - 57个字符 50个字符

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

57个字符:

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

答案 2 :(得分:7)

Mathematica - 纯功能

纯粹的功能性方法

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}]]

请注意,Mathematica正在求解一个找到钻石中直线函数的方程式。这是k中的丢番图方程:

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

对于每个元素,然后,如果找到解,则根据等式的lhs的符号确定“\”或“/”。 (在 {“\”,,“/”} [[Sign [p] / .s [[1]] 部分)

用法

f[2, 2, 2]

或者

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

一次生成所有测试用例

答案 3 :(得分:4)

Ruby - 115字节

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}

答案 4 :(得分:4)

Windows PowerShell, 124 123 121 119 116 112个字符

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

如果我们允许输入跨越三行而不是通常以空格分隔,我们可以将其降低到 109

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

作为脚本的参数(来自PowerShell内部),它是 105 字节:

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

然后会这样调用:

PS> .\diamond.ps1 2 2 2

答案 5 :(得分:3)

Python - 138个字符

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,

作为奖励,它也非常容易受到攻击!

答案 6 :(得分:3)

F#,233个字符

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))

趣味!我的F#代码高尔夫军火库的几个新位:

  • 使用stdin而不是繁琐的System.Console内容
  • 滥用运营商重载/重新定义

答案 7 :(得分:3)

JavaScript:261个字符(功能)

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个字符(带标准输入/输出的Rhino脚本)

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]:' ':' '}


可读Rhino版本:

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);
  }
}


测试用例:

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

/\
\/

答案 8 :(得分:3)

Perl - 161(工作程序)

($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(第二个版本) 这是更酷的想法......我正在使用数组插值的能力。

($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

完整的第二个变体:

    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

答案 9 :(得分:3)

Python,125个字符

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]]))

输入应以逗号分隔的形式提供,例如: 1,6,2

D:\CodeGolf> DiamondPattern.py

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

PS。如果您更喜欢用空格分隔的输入(1 6 1),则以21c的价格替换第一行:

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

如果您更喜欢命令行参数,那么对于25c以上,您可以拥有

import sys;s,c,r=map(int,sys.argv[1:])

答案 10 :(得分:2)

Haskell,136个字符

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]]

用法:

$ ./a.out
1
6
2
/\/\/\/\/\/\
\/\/\/\/\/\/
/\/\/\/\/\/\
\/\/\/\/\/\/

答案 11 :(得分:1)

Python - 126个字符

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))
),

为了清晰起见,添加了换行符和缩进。

答案 12 :(得分:0)

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 )
/\/\/\/\/\/\/\/\/\/\
\/\/\/\/\/\/\/\/\/\/
/\/\/\/\/\/\/\/\/\/\
\/\/\/\/\/\/\/\/\/\/
/\/\/\/\/\/\/\/\/\/\
\/\/\/\/\/\/\/\/\/\/