Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
This pattern seems to work:
- - - - L L R L R L R R - L L L R R L - - - R R L R - L R - R R - L L -
I wrote a proof of theory script which uses this fixed strategy technique. I don't know if it satisfies all of the requirements of the challenge, but it does appear to work.

Each of the three fixed weighs results in either 0, 1, or -1. These weigh results are joined together to create a unique key for each ball/weight combination which are then used to generate a lookup table that can then be used to quickly find the oddball and its weight in any given set.

use strict; use warnings; my $ballfinder = My::Oddball->new( [5,6,8,10], [7,9,11,12], # L/R set 1 [2,3,4,7], [5,6,11,12], # L/R set 2 [1,4,10,11], [2,5,7,8], # L/R set 3 ) or die "Bogus ball arrangement"; # Now we have a valid table, let's use it to find some random oddballs for (1..5){ my $weight = (0.9, 1.1)[ int(rand(2)) ]; my $oddball = (1..12)[ int(rand(12)) ]; my $balls = [ qw/ 0 1 1 1 1 1 1 1 1 1 1 1 1 / ]; $balls->[$oddball] = $weight; print "Randomly chosen ball: $oddball, ".($weight < 1 ? 'light' : 'he +avy')."\n"; print $ballfinder->detect($balls)."\n\n"; } package My::Oddball; sub new { my ( $class, $L1, $R1, $L2, $R2, $L3, $R3 ) = @_; my $table = { }; # Iterate through all possible combinations: balls 1-12, light & heavy foreach my $weight qw/ 0.9 1.1 / { for my $oddball (1..12){ my $balls = [ qw/ 0 1 1 1 1 1 1 1 1 1 1 1 1 / ]; $balls->[$oddball] = $weight; my $key = canon_key( balance( [ @$balls[@$L1] ], [ @$balls[@$R1] ] ), balance( [ @$balls[@$L2] ], [ @$balls[@$R2] ] ), balance( [ @$balls[@$L3] ], [ @$balls[@$R3] ] ), ); # If the table is valid, we should get a unique key # for each ball/weight combination return if exists $table->{$key}; $table->{$key} = "Ball $oddball is ".($weight < 1 ? "light" : "he +avy"); } } return bless { table => $table, sets => [ $L1, $R1, $L2, $R2, $L3, $R +3 ] }, $class; } sub detect { my ($self, $balls) = @_; my ( $L1, $R1, $L2, $R2, $L3, $R3 ) = @{$self->{sets}}; my $key = canon_key ( balance( [ @$balls[@$L1] ], [ @$balls[@$R1] ] ), balance( [ @$balls[@$L2] ], [ @$balls[@$R2] ] ), balance( [ @$balls[@$L3] ], [ @$balls[@$R3] ] ), ); return $self->{table}{$key}; } sub canon_key { join( '_' => @_ ) } sub balance { my ( $setA, $setB ) = @_; return weigh($setA) cmp weigh($setB); } sub weigh { my ($set) = @_; my $sum = 0; $sum += $_ for @$set; return $sum; }

In reply to Re^2: Odd Ball Challenge by djohnston
in thread Odd Ball Challenge by Limbic~Region

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (4)
As of 2024-04-24 22:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found