Function to print a diamond shape

Brad Anderson eco at gnuk.net
Thu Mar 20 15:03:20 PDT 2014


On Thursday, 20 March 2014 at 21:25:03 UTC, 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

I'm not entirely happy with it but:

   void main()
   {
     import std.algorithm, std.range, std.stdio, std.conv;

     enum length = 5;
     auto rng =
        chain(iota(length), iota(length, -1, -1))
       .map!((a => " ".repeat(length-a)),
             (a => "#".repeat(a*2+1)))
       .map!(a => chain(a[0].joiner, a[1].joiner, "\n"))
       .joiner;

     writeln(rng);
   }

Had some trouble with the result coming out as integers instead 
of something string-like.


More information about the Digitalmars-d-learn mailing list