when -1 { return (9, $result_2 == $result_3 ?? -1 :: 1 } #### when 0 { return (12, %ball{12} <=> %ball{0}) } #### 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); } } }