Function to print a diamond shape
Timon Gehr
timon.gehr at gmx.ch
Thu Mar 20 15:48:33 PDT 2014
On 03/20/2014 10:25 PM, Ali Çehreli wrote:
> This is a somewhat common little exercise: Write a function that takes
> the size of a diamond and produces a diamond of that size.
>
> When printed, here is the output for size 11:
>
> *
> ***
> *****
> *******
> *********
> ***********
> *********
> *******
> *****
> ***
> *
>
> What interesting, boring, efficient, slow, etc. ways are there?
>
> Ali
import std.stdio, std.range, std.algorithm, std.math;
enum s=11;
writef("%(%s\n%)", (i=>i.map!(a=>i.map!(b=>"* "[a+b>s/2])))
(iota(-s/2,s/2+1).map!abs));
More information about the Digitalmars-d-learn
mailing list