Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

comment on

( [id://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.

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

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (2)
As of 2024-04-25 19:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found