in reply to Golfing the day of Christmas
in thread Christmas Coding Blues

Are you guys using another version of perl than I am? At least here, the !-operator binds more tightly than the %-operator, so that (!$_%4 &c) just won't work.

... or am I just giving away your joke now ... ?

Anyway, I guess this would work (checking the calendar). 65 chars; based on yours:

sub f { #234567890#234567890#234567890#234567890#234567890#234567890#234567890 +#234567890 for(1..($b=pop)++){$b++;$b--if$_%4;$b++if$_%100;$b--if$_%400}$b%7 }
... or my best, so far, at 52:
sub f { #234567890#234567890#234567890#234567890#234567890#2 $b+=$_%400?$_%100?$_%4?0:1:0:1for 1..($b=pop)++;$b%7 }

Update: Checking the calendar again ...

The Sidhekin
print "Just another Perl ${\(trickster and hacker)},"

Replies are listed 'Best First'.
Re: Re: Golfing the day of Christmas
by jynx (Priest) on Dec 14, 2001 at 06:45 UTC

    This has a Golf Style Question attached to it...

    Your answer can be trimmed down to 47 characters:

    sub f { #23456789_123456789_123456789_123456789_123456789_12 $b+=$_%400?$_%100?$_%4?1:2:1:2for 1..pop;$b%7+1 }
    But this begs the question, should it be immediately reusable? My guess is yes, in which case this is a non-answer. The problem is that $b doesn't get reinitialized at the start of the sub so you have to either zero it or undef it in between every call.

    It returns correctly the first time, but incorrectly after that; is this ok for a golf? Or should it return correctly every time?

    jynx

      jynx, your sub does not even return the correct answer the first time, if $b has been set elsewhere -- you rely on it to be numerically zero initially. Moreover, you will get a 7, not a 0, for Sundays ... so on the whole, I can't say I like it.

      That is not to say my answer cannot be trimmed down. Indeed, I just did. And just so as not to leave your suggestion as the best (sorry; I really don't like it), I trimmed it to 46 chars:

      sub f { # 1 2 3 4 5 #234567890#234567890#234567890#234567890#234567890#23 $b+=!($_%4||/00$/&&$_%400)for 0..($b=pop);$b%7 }
      Comparing that to my first suggestion ... can you tell I really like the ternary operator? ;-)

      The Sidhekin
      print "Just another Perl ${\(trickster and hacker)},"