in reply to Holiday parcel puzzle

Hi Cavac,

Can I just say I've really enjoyed thinking this one over. Thanks for posting. Now I have already come across this puzzle before and knew what the best option would be. However I have never until now really delved into the math and certainly never tried to apply such a problem into code. I may try and do so with other problems I come across, as an ongoing training technique.

At first I thought I was going to have to use arrays and hashes or a mixture of the two. However after thinking about it and trying to reduce the significance of the array/hash values, and coding, I came up with what I think is a fairly straight forward conditional routine that tests for both the punter sticking and changing after each initial guess.

I made an effort to approach the problem as if I had never seen it before and did not know the outcome already too. I hope that the module retains it's integrity for that

#! perl -T use strict; use warnings; # set number of coconutsouffles the punter has to eat if they stick wi +th or change choice. my $stickycoconutsouffle=0; my $coconutsouffle=0; for(1..99999){ #set boolean flag (punter guess is false until correct) my $tf = undef; # punter guesses correctly first time, set correct to true if(int(rand(3))==2){ $tf = ! $tf }; # increase count for punter sticking with choice if they are correct; $stickycoconutsouffle++ if $tf; # increase count for punter changing choice and being correct after co +ntainer removed $tf = ! $tf; $coconutsouffle++ if $tf; } # end for # how many souffles does our punter get? print $/."Sticking with original choice they recieve $/$/$stickycoconu +tsouffle Coconut Souffle's.".$/x2, "Changing their original choice they recieve $/$/$coconutsouffle + Coconut Souffle's.".$/x2; exit 0; ------------------------- results Sticks with original choice they recieve 33172 Coconut Souffle's. Changing their original choice they recieve 66827 Coconut Souffle's.

no data structures required I think. Happy Xmas & New Year!

Don

Replies are listed 'Best First'.
Re^2: Holiday parcel puzzle
by cavac (Prior) on Dec 29, 2011 at 22:43 UTC

    Interesting solution. Guess mine was a bit too complicated.

    BREW /very/strong/coffee HTTP/1.1
    Host: goodmorning.example.com
    
    418 I'm a teapot