22

挑战

根据输入,按字符计数的最短代码输出一个菱形图案。

输入由 3 个正数组成,分别代表菱形的大小和网格的大小。

菱形由 ASCII 字符/\空格组成。尺寸为 1 的钻石是:

/\
\/

网格的大小由菱形数量的宽度和高度组成。

测试用例

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

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

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

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

4

13 回答 13

10

Golfscript - 50 个字符

~@:3,[{[.3-~' '*\' '*'/'\.'\\'4$]2$*}%n*.-1%]*n*\;
于 2010-06-06T13:22:11.530 回答
8

高尔夫脚本 -57 个字符50 个字符

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

57 个字符:

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

于 2010-06-06T12:36:45.370 回答
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]]  

一次生成所有测试用例

于 2010-06-07T03:28:07.980 回答
4

红宝石 - 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}
于 2010-06-06T06:51:36.410 回答
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
于 2010-06-09T13:59:26.293 回答
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,

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

于 2010-06-06T06:14:20.470 回答
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东西
  • 滥用运算符重载/重新定义
于 2010-06-06T07:50:12.780 回答
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]:' ':' '}


可读犀牛版本:

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

/\
\/
于 2010-06-06T10:32:53.733 回答
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
于 2010-06-06T17:08:02.960 回答
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:])
于 2010-06-09T21:55:57.897 回答
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
/\/\/\/\/\/\
\/\/\/\/\/\/
/\/\/\/\/\/\
\/\/\/\/\/\/
于 2010-06-07T13:25:35.763 回答
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))
),

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

于 2010-06-06T11:05:29.647 回答
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 )
/\/\/\/\/\/\/\/\/\/\
\/\/\/\/\/\/\/\/\/\/
/\/\/\/\/\/\/\/\/\/\
\/\/\/\/\/\/\/\/\/\/
/\/\/\/\/\/\/\/\/\/\
\/\/\/\/\/\/\/\/\/\/
于 2013-10-05T12:35:17.097 回答