挑战

根据输入输出菱形图案的字符数最短代码。

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

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

/\
\/

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

测试用例

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

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

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

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

有帮助吗?

解决方案

Golfscript - 50 个字符

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

其他提示

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

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

57 个字符:

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

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 的符号来决定“\”或“/”。(在里面 {"\", , "/"}[[符号[p] /.s[[1]] 部分 )

用法

f[2, 2, 2]

或者

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

用于一次生成所有测试用例

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

红宝石 - 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}

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 东西
  • 滥用运算符重载/重定义

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

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

/\
\/

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

附言。如果您喜欢用空格分隔输入(1 6 1),对于 21c 的价格,将第一行替换为:

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

如果你更喜欢命令行参数,你可以多花 25c

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

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,

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

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

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

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

克洛尤尔

(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 )
/\/\/\/\/\/\/\/\/\/\
\/\/\/\/\/\/\/\/\/\/
/\/\/\/\/\/\/\/\/\/\
\/\/\/\/\/\/\/\/\/\/
/\/\/\/\/\/\/\/\/\/\
\/\/\/\/\/\/\/\/\/\/
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top