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"; }
In reply to Diamonds for fun by perl_nando
For: | Use: | ||
& | & | ||
< | < | ||
> | > | ||
[ | [ | ||
] | ] |