Function to print a diamond shape
monarch_dodra
monarchdodra at gmail.com
Mon Mar 24 04:24:26 PDT 2014
On Sunday, 23 March 2014 at 18:28:18 UTC, Jay Norwood wrote:
> On Sunday, 23 March 2014 at 17:30:20 UTC, bearophile wrote:
>
>>
>> The task didn't ask for a computationally efficient solution
>> :-) So you are measuring something that was not optimized for.
>> So there's lot of variance.
>>
>> Bye,
>> bearophile
>
> Yes, this is just for my own education. My builds are using
> the dmd compiler on windows, and some posts indicate I should
> expect better optimization currently with the ldc compiler...
> so maybe I'll get on a linux box and retest with ldc.
So it's about speed now? Then I submit this:
//----
void printDiamond(size_t N)
{
char[32] rawSpace = void;
char[64] rawStars = void;
char* pSpace = rawSpace.ptr;
char* pStars = rawStars.ptr;
if (N > 64)
{
pSpace = new char[](N/2).ptr;
pStars = new char[](N).ptr;
}
pSpace[0 .. N/2] = ' ';
pStars[0 .. N] = '*';
N/=2;
foreach (n ; 0 .. N + 1)
writeln(pSpace[0 .. N - n], pStars[0 .. 2*n+1]);
foreach_reverse (n ; 0 .. N)
writeln(pSpace[0 .. N - n], pStars[0 .. 2*n+1]);
}
//----
More information about the Digitalmars-d-learn
mailing list