finding signs of 3 numbers

60 Views Asked by At

This is slightly more of a coding problem than a math problem but I think it is still relevant.

So let's say I have 3 numbers A,B,C and I can only call a given function if two are negative and one is positive or vice versa with 2 positive and one negative. Also I need to be able to tell which of the 3 numbers is the odd one out.

It's tedious and a little unclean looking to do:

if(A < 0 && B < 0 && C > 0)

{

  //C is odd one out;

} else if(A < 0 && B > 0 && C < 0){

 //B is odd one out

} etc...

2

There are 2 best solutions below

1
On BEST ANSWER

Assuming none of them are zero,

let P = A*B*C
if P > 0                       % either + + + or + - - 
    if min(A,B,C) > 0
        print('invalid input') % corresponds to + + +
    else
        oddoneout = max(A,B,C)
    end
else                           % either - - - or + + -
    if max(A,B,C) < 0
        print('invalid input') % corresponds to - - -
    else
        oddoneout = min(A,B,C)
    end
end
2
On

Simply, if the product $ABC$ is positive then return a positive variable and if $ABC$ is negative then return a negative variable.

Edit added sample code.

if ABC > 0:
    for X in [A, B, C]:
        if X > 0: return X
else:
    for X in [A, B, C]:
        if X < 0: return X