Function to print a diamond shape

Sergei Nosov sergei.nosov at gmail.com
Fri Mar 21 07:54:04 PDT 2014


On Friday, 21 March 2014 at 13:59:27 UTC, Vladimir Panteleev 
wrote:
> On Friday, 21 March 2014 at 12:32:58 UTC, Sergei Nosov wrote:
>> 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
>>
>> Probably, the most boring way is
>>
>> foreach(i; 0..N)
>> {
>>    foreach(j; 0..N)
>>        write(" *"[i + j >= N/2 && i + j < 3*N/2 && i - j <= 
>> N/2 && j - i <= N/2]);
>
> write(" *"[abs(i-N/2) + abs(j-N/2) <= N/2]);
>
>>    writeln;
>> }

Beat me. Yours is even more boring. =)


More information about the Digitalmars-d-learn mailing list