cardinal golf

Tkil tkil-sdpm at scrye.com
Fri Feb 6 19:31:10 CST 2004


~sdpm~
>>>>> "Charles" == C Abney <cabney at ucsd.edu> writes:

| perl -le 'for(1..125){($b,$c)=reverse split//;
|           print$_,($c!=1?($b==1?"st":$b==2?"nd":$b==3?"rd":"th"):"th")}'

Hm.  I don't think I saw the rules, but I imagine it has to do with
how tightly you can write cardinal suffixes?  On a command line that
many characters long.

My current record is 61 total characters (including "perl" and the
closing single tick).  Skip to the bottom to see my solution, although
I left the trail I followed here (including a false start; those are
marked "wrong".)

How about:

  # oops, this is wrong
  perl -le '@x=(qw(th st nd rd),("th")x6);for(1..70){print$_,$x[$_%10]}'

Similar idea, but with strings:

  # this one too
  perl -le '$x="thstndrdthththththth";for(1..75){print$_,substr$x,$_%10*2,2}'

And even further:

  # still wrong.
  perl -le '$x="thstndrd"."th"x6;for(1..70){print$_,substr$x,$_%10*2,2}'

Back to 70 characters.

Ok, screw efficiency and use a goofy loop construct as well:

  # yup, still wrong.
  perl -le 'print$_,substr"thstndrd"."th"x6,$_%10*2,2 for 1..62'

Ah, I missed the 'teens.

  perl -le 'print$_,/1.$/?"th":substr"thstndrd"."th"x6,$_%10*2,2 for 1..73'

And just to squeeze two more characters out:

  perl -le'print$_,/1.$/?"th":substr"thstndrd"."th"x6,$_%10*2,2for 1..71'

This is getting gross.  Let's move that conditional in:

  perl -le'print$_,substr"thstndrd"."th"x6,/1.$/?0:$_%10*2,2for 1..68'

And maybe something other than ?:...

  perl -le'print$_,substr"thstndrd"."th"x6,!/1.$/*$_%10*2,2for 1..67'

Same thing if we go back to arrays:

  perl -le'print$_,(qw(th st nd rd),("th")x6)[!/1.$/*$_%10]for 1..67'

Ha.  To make sure that I've grovelled as low as I'm willing to go,
let's use barewords!

  perl -le'print$_,(th,st,nd,rd,(th)x6)[!/1.$/*$_%10]for 1..61'

Ok, I think I'm done.

Thanks for the fun.  Also about 20 minutes, here.  :)

t.
~sdpm~

The posting address is: san-diego-pm-list at hfb.pm.org

List requests should be sent to: majordomo at hfb.pm.org

If you ever want to remove yourself from this mailing list,
you can send mail to <majordomo at happyfunball.pm.org> with the following
command in the body of your email message:

    unsubscribe san-diego-pm-list

If you ever need to get in contact with the owner of the list,
(if you have trouble unsubscribing, or have questions about the
list itself) send email to <owner-san-diego-pm-list at happyfunball.pm.org> .
This is the general rule for most mailing lists when you need
to contact a human.




More information about the San-Diego-pm mailing list