in reply to Perl6 Contest: Test your Skills

A little analysis would go a long way. The first thing is that you don't have to score the hand - you have to see if the hand has two points. If it does, you win. If it doesn't, you lose.

So, the trick is to prove you cannot construct a hand and cut-card that do not meet these criteria. A proof by negation, if you will. I will leave that as an exercise for the reader.


  • In general, if you think something isn't in Perl, try it out, because it usually is. :-)
  • "What is the sound of Perl? Is it not the sound of a wall that people have stopped banging their heads against?"

Replies are listed 'Best First'.
Re^2: Perl6 Contest: Test your Skills
by Limbic~Region (Chancellor) on May 19, 2005 at 19:24 UTC
    dragonchild,
    I agree that once you know you have at least 2 points you can give up. If you look at my p5 solution in that thread, I solved the problem in 63 seconds (3 to generate a candidate list and 60 to disqualify them).

    That's not the point of this challenge. The challenge is about practicing p6 features and syntax with efficiency only a secondary goal.

    Cheers - L~R

      This is my point:
      # Assumption: @hand[0]<val> == 5; sub is_good( @hand ) returns Bool { # If you have a 10, J, Q, or K anywhere in @hand, you win. if any(@hand<val>) == 10 { return true; } # If you have more than one of anything, you win. my %cardvals; %cardvals{ @hand[0..4]<num> }++; if any( %cardvals.values ) > 1 { return true; } # If you have 3+ cards in a row, you win. for 1 .. 11 -> $start { if all( @cardvals{ ($start, $start+1, $start+2) } ) { return true; } } # If +%suit{@hand[0..3]} == 1, you win. # Note: The suit of the cut-card is irrelevant. my %suits; %suits{ @hand[0..3]<suit> }++; if +%suit == 1 { return true; } # Alternately, this could have been written: # if all( @hand[0..3]<suit> ) == any( @hand[0..3]<suit> ) { # return true; # } return false; }

      • In general, if you think something isn't in Perl, try it out, because it usually is. :-)
      • "What is the sound of Perl? Is it not the sound of a wall that people have stopped banging their heads against?"
        dragonchild,
        Right. With all due respect to thor, I believe the original problem was poorly stated (as perhaps was my challenge). I know the ultimate goal was only to determine if such a hand existed, but the process outlined by which to do it was to actually score each hand:

        What I want to do is write a program that brute forces the issue. That is to say that I want to create a program that scores all 5-card hands that have a 5 in them and see what happens.

        I would prefer that the actual score be tabulated since it is a harder problem, but I am fine with aborting from score() early if you know the score is going to be > 1. This is afterall not about verifying something we already know - it is about exposure to Perl6.

        Cheers - L~R