Function to print a diamond shape

Ali Çehreli acehreli at yahoo.com
Thu Mar 20 15:55:42 PDT 2014


On 03/20/2014 03:48 PM, Timon Gehr wrote:
> 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));
>

Sweet! :)

     "* "[a+b>s/2]    // loving it

Ali



More information about the Digitalmars-d-learn mailing list