Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

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:

1 121 12321 121 1
And you get the idea for all the way up to 9...

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.


#!/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

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others lurking in the Monastery: (11)
    As of 2018-03-20 15:17 GMT
    Find Nodes?
      Voting Booth?
      When I think of a mole I think of:

      Results (254 votes). Check out past polls.