Thread overview | ||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
March 20, 2014 Function to print a diamond shape | ||||
---|---|---|---|---|
| ||||
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 |
March 20, 2014 Re: Function to print a diamond shape | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ali Çehreli | On Thu, 20 Mar 2014 14:25:02 -0700, 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
What's the appropriate output for an even number?
|
March 20, 2014 Re: Function to print a diamond shape | ||||
---|---|---|---|---|
| ||||
Posted in reply to Justin Whear | On 03/20/2014 02:30 PM, Justin Whear wrote: > What's the appropriate output for an even number? Great question! :) Size must be odd. I have this in my function: enforce(size % 2, format("Size cannot be an even number. (%s)", size)); Ali |
March 20, 2014 Re: Function to print a diamond shape | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ali Çehreli | On Thursday, 20 March 2014 at 21:25:03 UTC, Ali Çehreli wrote:
> What interesting, boring, efficient, slow, etc. ways are there?
>
> Ali
Well one of the more convoluted methods that I can think of would be to define a square as a set of four vectors, rotate 45 degrees, and then create a rasterizer that checks for the presence of the rect at sequential points, and plots those to the console.
|
March 20, 2014 Re: Function to print a diamond shape | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ali Çehreli | 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.
|
March 20, 2014 Re: Function to print a diamond shape | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ali Çehreli | On Thursday, 20 March 2014 at 21:25:03 UTC, Ali Çehreli wrote: > What interesting, boring, efficient, slow, etc. ways are there? > > Ali I'd be interested in seeing a solution using "iota", and the currently proposed "each" or "tee". A quick protype to draw a triangle would be: iota(0, n).each!(a=>q{%(*%)}.writefln(a.iota()))(); or iota(0, n).each!(a=>'*'.repeat(a).writeln())(); Adapting that to do a diamond should be straight forward? It would be a good benchmark of functional vs imperative code, and the usability of "each". |
March 20, 2014 Re: Function to print a diamond shape | ||||
---|---|---|---|---|
| ||||
Posted in reply to Brad Anderson | On 03/20/2014 03:03 PM, Brad Anderson wrote: > I'm not entirely happy with it but: I am not happy with my attempt either. :) > void main() > { > import std.algorithm, std.range, std.stdio, std.conv; > > enum length = 5; > auto rng = > chain(iota(length), iota(length, -1, -1)) Ooh. I like that. That would have never occurred to me. :) > .map!((a => " ".repeat(length-a)), > (a => "#".repeat(a*2+1))) > .map!(a => chain(a[0].joiner, a[1].joiner, "\n")) > .joiner; > > writeln(rng); > } Does that compile for you? Failed for me with v2.066-devel-d0f461a: ./phobos/std/typetuple.d(550): Error: template instance F!(__lambda1) cannot use local '__lambda1' as parameter to non-global template AppliedReturnType(alias f) ./phobos/std/typetuple.d(556): Error: template instance deneme.main.staticMap!(AppliedReturnType, __lambda1) error instantiating ./phobos/std/algorithm.d(404): instantiated from here: staticMap!(AppliedReturnType, __lambda1, __lambda2) deneme.d(161788): instantiated from here: map!(Result) That is pointing at this line: .map!((a => " ".repeat(length-a)), A regression? > Had some trouble with the result coming out as integers instead of > something string-like. I had the same problem at one point. I will try to understand when that happens. Ali |
March 20, 2014 Re: Function to print a diamond shape | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ali Çehreli | 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));
|
March 20, 2014 Re: Function to print a diamond shape | ||||
---|---|---|---|---|
| ||||
Posted in reply to Chris Williams | On 03/20/2014 02:52 PM, Chris Williams wrote:
> On Thursday, 20 March 2014 at 21:25:03 UTC, Ali Çehreli wrote:
>> What interesting, boring, efficient, slow, etc. ways are there?
>>
>> Ali
>
> Well one of the more convoluted methods that I can think of would be to
> define a square as a set of four vectors, rotate 45 degrees, and then
> create a rasterizer that checks for the presence of the rect at
> sequential points, and plots those to the console.
A slightly convoluted solution that I've come up with considers the diamond as three pieces:
1) Top triangle
2) The widest line
3) The bottom triangle, which happens to be the .retro of the first part
auto bottomHalf = topHalf.retro;
auto diamond = chain(topHalf, widestLine, bottomHalf).joiner("\n");
Ali
|
March 20, 2014 Re: Function to print a diamond shape | ||||
---|---|---|---|---|
| ||||
Posted in reply to Timon Gehr | 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
|
Copyright © 1999-2021 by the D Language Foundation