DEV Community

Discussion on: Daily Challenge #35 - Find the Outlier

Collapse
 
oscherler profile image
Olivier “Ölbaum” Scherler

Erlang.

  • If the first two numbers have the same parity, I search the rest of the array for the other parity;
  • If they have different parities, I rotate them with the third number and check the resulting three-element array.
-module( outlier ).

-include_lib("eunit/include/eunit.hrl").

outlier( [ A, B, C | Rest ] ) ->
    case { abs( A rem 2 ), abs( B rem 2 ) } of
        { S, S } -> outlier( [ C | Rest ], 1 - S );
        { _, _ } -> outlier( [ B, C, A ] )
    end.
outlier( [ A | _ ], S ) when abs( A rem 2 ) == S ->
    A;
outlier( [ _ | Rest ], S ) ->
    outlier( Rest, S ).    

outlier_test_() -> [
    ?_assertEqual( 11, outlier( [ 2, 4, 0, 100, 4, 11, 2602, 36 ] ) ),
    ?_assertEqual( 160, outlier( [ 160, 3, 1719, 19, 11, 13, -21 ] ) ),
    ?_assertEqual( 15, outlier( [ 4, 8, 15, 16, 24, 42 ] ) ),

    ?_assertEqual( 2, outlier( [ 1, 2, 3, 5 ] ) ),
    ?_assertEqual( 1, outlier( [ 1, 2, 4, 6 ] ) ),
    ?_assertEqual( 2, outlier( [ 2, 1, 3, 5 ] ) ),
    ?_assertEqual( 1, outlier( [ 2, 1, 4, 6 ] ) ),

    ?_assertError( function_clause, outlier( [ 16, 6, 40, 66, 68, 28 ] ) ),
    ?_assertError( function_clause, outlier( [ 16, 6 ] ) )
].