Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

I've been working on an exercise to generate the English "Twelve days of Christmas song". So far, I've come up with the following:

for(<DATA>){s/%/ing/;$_=($i++?$i-1+print" ":a)." $_";print"On the $i",(st,nd,rd)[$i-1]||th," day of Christmas, my true love gave to me: ",$t=$_.$t}__DATA__ partridge in a pear tree. turtle doves and French hens, call% birds, golden rings, geese a-lay%, swans a-swimm%, maids a-milk%, ladies danc%, lords a-leap%, pipers pip%, drummers drumm%,

That generates the following text:

On the 1st day of Christmas, my true love gave to me: a partridge in a pear tree. On the 2nd day of Christmas, my true love gave to me: 2 turtle doves and a partridge in a pear tree. On the 3rd day of Christmas, my true love gave to me: 3 French hens, 2 turtle doves and a partridge in a pear tree. On the 4th day of Christmas, my true love gave to me: 4 calling birds, 3 French hens, 2 turtle doves and a partridge in a pear tree. On the 5th day of Christmas, my true love gave to me: 5 golden rings, 4 calling birds, 3 French hens, 2 turtle doves and a partridge in a pear tree. On the 6th day of Christmas, my true love gave to me: 6 geese a-laying, 5 golden rings, 4 calling birds, 3 French hens, 2 turtle doves and a partridge in a pear tree. On the 7th day of Christmas, my true love gave to me: 7 swans a-swimming, 6 geese a-laying, 5 golden rings, 4 calling birds, 3 French hens, 2 turtle doves and a partridge in a pear tree. On the 8th day of Christmas, my true love gave to me: 8 maids a-milking, 7 swans a-swimming, 6 geese a-laying, 5 golden rings, 4 calling birds, 3 French hens, 2 turtle doves and a partridge in a pear tree. On the 9th day of Christmas, my true love gave to me: 9 ladies dancing, 8 maids a-milking, 7 swans a-swimming, 6 geese a-laying, 5 golden rings, 4 calling birds, 3 French hens, 2 turtle doves and a partridge in a pear tree. On the 10th day of Christmas, my true love gave to me: 10 lords a-leaping, 9 ladies dancing, 8 maids a-milking, 7 swans a-swimming, 6 geese a-laying, 5 golden rings, 4 calling birds, 3 French hens, 2 turtle doves and a partridge in a pear tree. On the 11th day of Christmas, my true love gave to me: 11 pipers piping, 10 lords a-leaping, 9 ladies dancing, 8 maids a-milking, 7 swans a-swimming, 6 geese a-laying, 5 golden rings, 4 calling birds, 3 French hens, 2 turtle doves and a partridge in a pear tree. On the 12th day of Christmas, my true love gave to me: 12 drummers drumming, 11 pipers piping, 10 lords a-leaping, 9 ladies dancing, 8 maids a-milking, 7 swans a-swimming, 6 geese a-laying, 5 golden rings, 4 calling birds, 3 French hens, 2 turtle doves and a partridge in a pear tree.

My script is 343 bytes, which is better than zip and tar+gzip but worse than gzip:

-rw-r--r-- 1 john john 294 May 1 13:29 12days.gz -rw-r--r-- 1 john john 343 Apr 30 04:00 12days.pl -rw-r--r-- 1 john john 370 May 1 13:30 12days.tar.gz -rw-r--r-- 1 john john 2169 May 1 13:29 12days.txt -rw-r--r-- 1 john john 439 May 1 13:30 12days.zip

Does anybody have a shorter solution? For simplicity's sake, I've limited the challenge to digits rather than written numbers.


In reply to 12 days of Perl? by johnaj

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 cooling their heels in the Monastery: (7)
As of 2024-03-28 21:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found