One geeky afternoon at work, we challenged each other to write the smallest possible program in any language to print a diamond made out of numbers, from 1 to a maximum of 9.
For example a diamond of 1 would just be 1.
A diamond of 2 would be:
1 121 1
A diamond of 3 would be:
And you get the idea for all the way up to 9...1 121 12321 121 1
One of them wrote several lines of c# code. Another wrote a really long one line of code that had predefined outputs. So I entered the prizeless contest, and I decided to use Perl.
I'm sure there is a way to make a long and ugly one liner out of this, but I wanted to share what I came up with after reading chapter 1 and 2 of Learning Perl.
Enjoy!
#!/usr/bin/perl print 'Diamond size (1-9): '; chomp($diamondSize = <STDIN>); foreach $i(@i = (1..$diamondSize, reverse (1..$diamondSize-1))) { print ' ' x ($diamondSize - $i), (@d ="1".."$i"), (reverse @d[ +0..$#d-1]), "\n"; }
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Diamonds for fun
by BrowserUk (Patriarch) on Mar 05, 2011 at 11:10 UTC | |
by a (Friar) on Mar 06, 2011 at 00:15 UTC | |
by LanX (Saint) on May 04, 2011 at 16:22 UTC | |
by Tux (Canon) on May 04, 2011 at 17:23 UTC | |
Re: Diamonds for fun
by ambrus (Abbot) on Mar 07, 2011 at 14:13 UTC | |
by toolic (Bishop) on Mar 07, 2011 at 14:45 UTC | |
by hbm (Hermit) on Mar 07, 2011 at 20:38 UTC | |
by ambrus (Abbot) on Mar 07, 2011 at 21:14 UTC | |
Re: Diamonds for fun
by repellent (Priest) on Mar 06, 2011 at 09:26 UTC | |
Re: Diamonds for fun
by ambrus (Abbot) on Oct 20, 2012 at 19:52 UTC | |
Re: Diamonds for fun
by LanX (Saint) on May 04, 2011 at 12:35 UTC |
Back to
Cool Uses for Perl