Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re^3: Odd Ball Challenge

by kaif (Friar)
on Jun 25, 2005 at 22:27 UTC ( [id://469966]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Odd Ball Challenge
in thread Odd Ball Challenge

I'm having trouble understanding how it even compiles. Isn't

when -1 { return (9, $result_2 == $result_3 ? +? -1 :: 1 }

a syntax error (mismatched parentheses)? If so, there are a few lines like that. Also, one line refers to

when 0 { return (12, %ball{12} <=> %ball{0}) }

where 0 should probably be 1. Finally, I confirm all of djohnston's results by translation to Perl 5 (for example, just imagine ball 1 is heavier and run through the code mentally --- it indeed says something about either ball 3 or 5). Pardon the ugly code; the Perl 6 is certainly prettier. But as Limbic~Region says, solving the riddle isn't the challenge, right?

use Test::More tests => 24; use warnings; for( 1 .. 12 ) { %ball = map { $_ => 1; } 1..12; for $d ( -1, +1 ) { $ball{$_} = 1 + $d * 0.1; is_deeply( [$_, $d], [Find_Odd_Ball()], "($_, $d)" ); } } sub Find_Odd_Ball { my $result_1 = $ball{1} + $ball{2} + $ball{3} + $ball{4} <=> $ball +{5} + $ball{6} + $ball{7} + $ball{8}; if( $result_1 == 0 ) { my $result_2 = $ball{9} + $ball{10} <=> $ball{1} + $ball{11}; if( $result_2 == 0 ) { return (12, $ball{12} <=> $ball{1}); } else { my $result_3 = $ball{9} <=> $ball{10}; if( $result_3 == -1 ) { return (9, $result_2 == $result_3 ? -1 : 1 ); } elsif ($result_3 == 0 ) { return (11, $result_2 * -1); } else { return (10, $result_2 == $result_3 ? 1 : -1 ); } } } else { my $result_2 = $ball{1} + $ball{2} + $ball{5} <=> $ball{3} + $ +ball{6} + $ball{10}; if( $result_2 == -1 ) { my $result_3 = $ball{1} <=> $ball{2}; if( $result_3 == -1 ) { return (1, $result_1); } elsif( $result_3 == 0 ) { return (6, $result_1 * -1); } else { return (2, $result_1 * -1); } } elsif( $result_2 == 0 ) { my $result_3 = $ball{7} <=> $ball{8}; if( $result_3 == -1 ) { return (8, $result_1 * -1); } elsif( $result_3 == 0 ) { return (4, $result_1); } else { return (7, $result_1 * -1); } } else { return $ball{3} == $ball{10} ? (5, $result_1 * -1) : (3, $ +result_1); } } }

Replies are listed 'Best First'.
Re^4: Odd Ball Challenge
by Limbic~Region (Chancellor) on Jun 26, 2005 at 16:09 UTC
    kaif,
    I'm having trouble understanding how it even compiles.

    As I stated - it doesn't compile or run. The typo you pointed out isn't the problem though. There are several bugs I uncovered in testing this code that won't even let a very minimal implementation run.

    Thanks for the code - I will investigate further when I get a chance.

    Cheers - L~R

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://469966]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (5)
As of 2024-04-19 23:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found