in reply to (GOLF) pearls and perl

step 2a : weigh 9-11 against 1-3. if they're the same, we know it's 12. weigh 12 against any of the others to determine if it's lighter or heavier then you're done. if 9-11 was heavier than 1-3, you know that the real one is heavy. weigh 9 against 10. if they're equal 11 is the real one. otherwise, the heavier of 9 and 10 is the real one.

step 2a of your algorithm is not correct. tachyon's right. you'll lose your head--there's an extra weighing here. instead, it should go something like this:

step 2a : weigh 9 against 10. if they're the same, we know it's 11 or twelve. then weigh 9 against 11. if they're the same, we know it's twelve; if they're different, it's 11 and you're done. if 9 and 10 are different, weigh 9 against 11. if they're the same, it's 10; if they're different, it's 9 and you're done.

my logic for this bit, which works under strict and warnings, is

$p[8]==$p[9]?$p[8]==$p[10]?12:11:$p[8]==$p[10]?10:9;

or, building the return matrix and putting it in a sub:

do{([9,10],[11,12])[$p[8]==$p[9]]->[$p[8]==$p[10]]}

which is one char shorter.

~Particle ;Þ

Replies are listed 'Best First'.
Re: Re: (GOLF) pearls and perl
by thraxil (Prior) on Apr 04, 2002 at 15:39 UTC

    my prose explanation may be unclear but i think it's correct. look closely at the example code. running it gives:

    pearl #0 was light. found in 3 tests.
    pearl #0 was heavy. found in 3 tests.
    pearl #1 was light. found in 3 tests.
    pearl #1 was heavy. found in 3 tests.
    pearl #2 was light. found in 3 tests.
    pearl #2 was heavy. found in 3 tests.
    pearl #3 was light. found in 3 tests.
    pearl #3 was heavy. found in 3 tests.
    pearl #4 was light. found in 3 tests.
    pearl #4 was heavy. found in 3 tests.
    pearl #5 was light. found in 3 tests.
    pearl #5 was heavy. found in 3 tests.
    pearl #6 was light. found in 3 tests.
    pearl #6 was heavy. found in 3 tests.
    pearl #7 was light. found in 3 tests.
    pearl #7 was heavy. found in 3 tests.
    pearl #8 was light. found in 3 tests.
    pearl #8 was heavy. found in 3 tests.
    pearl #9 was light. found in 3 tests.
    pearl #9 was heavy. found in 3 tests.
    pearl #11 was light. found in 3 tests.
    pearl #10 was heavy. found in 3 tests.
    pearl #11 was light. found in 3 tests.
    pearl #11 was heavy. found in 3 tests.
    

    if you get to step 2a, you've already used the balance once and you know that the pearl is in 9-12 and therefore 1-8 are all fakes. weighing 1-3 against 9-11 (your second use of the balance) tells you immediately whether it's 12 or one of 9-11 as well as whether it's light or heavy. if you know it's 12 you have to do another weigh (your third) to tell if it's lighter or heavier. if it's one of the other 3, you weigh 9 against 10 (your third). equal means that it's 11 and you know if it's light or heavy from the previous 1-3 vs 9-11 weighing. if you know that it's heavy you take the heavier of the two you just weighed; lighter, you take the lighter of the two.

    is that more clear?

    actually, i think your version of 2a will run into four weighs. take the case where it's 12. first, you've already weighed 1-4 against 5-8 before you get there. then you weigh 9 against 10 (#2). they're the same so you weigh 9 against 11 (#3). you know that it's 12 but don't have any way of knowing if it's heavy or light so your best bet then would be to guess and be happy with a 50/50 chance of losing your head.

    anders pearson

      yeah, you're right. my solution ignores the weight... oops! i've bookmarked this thread, i'd like to work on it when i get some time. i'd like to set up an 3d answer matrix, and use code only in the indices (as in (answers)[code]) like i did in my (incorrect) example above.

      ~Particle ;Þ