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 |