views:

543

answers:

6

Hi All,

Do the below without any conditional or comparison operator.

if (Number <= 0)
{
    Print '0';
}
else
{
    print Number;
}

thanks..

+8  A: 
print max(0, number)
soulmerge
In C/C++ this would be like print (number <= 0 ? 0 : number), which includes a comparison.
Ralph Rickenbach
Ok, but works for PHP, Ruby, Python, Java, C#, etc with slight variations on syntax.
soulmerge
If it is a comparison and not a builtin function, so are sign() and abs().
soulmerge
In C/C++ max and min are macros, which are extended by the precompiler, abs is a function in math.h, so there is a difference.
Ralph Rickenbach
My last comment was a response to a now deleted comment, where the author claimed that the function max() would count as a comparison in any language - I know that it would be a comparison in C/C++, but the question did not state any language.
soulmerge
+11  A: 

My original simple solution:

1. print( (abs(Number)+Number) / 2 )

That solution would work in most cases, unless Number is very large (more than half the maximum e.g. Number >= MAX_INT/2) in which case the addition may cause overflow.

The following solution solves the overflow problem:

2. print( (abs(Number)/2) + (Number/2) )

However, there may be a case in which Number is and must remain integer, and the division operator (/) is integer division, so that 7/2=3. In this case solution 2 won't work because if Number=7 it will print 6 (for this case solution 1 will work just fine).

So if we need to deal with both large numbers AND integer arithmetic, the following monstrosity comes to the rescue, adding compensation for the 1 that may be lost in the division by 2 in case of odd integer:

3. print( 
    ( (abs(Number)/2)+(Number/2) ) +
    ((
        (Number-(2*(Number/2))) + 
        (abs(Number)-(2*(abs(Number)/2)))
    ) / 2)
    )
Roee Adler
Might cause an overflow.
Ralph Rickenbach
@malach: What do you mean by overflow?
Roee Adler
Number+Number > Max of corresponding type
Ralph Rickenbach
@malach: Fixed, thanks :)
Roee Adler
If Number is odd, the division will cause either dataloss or conversion to double. Print will either not print number but number-1 or will depending on the language print it with decimal digits or other floating point representation.
Ralph Rickenbach
I actually assumed the number was floating point since there was no mention of integer limitation.
Roee Adler
And now I added a case for integer arithmetic....
Roee Adler
A: 

Assuming C or C++:

switch ((unsigned long)Number & ~(unsigned long)LONG_MAX) {
    case 0:
        printf("%d\n", Number);
        break;
    default:
        printf("0\n", Number);
        break;
}

If you consider switch to be a conditional operator, then try this:

unsigned long flag = (unsigned long)Number & ~(unsigned long)LONG_MAX;
flag /= (unsigned long)LONG_MAX + 1;
flag = 1 - flag;
printf("%d\n", Number * flag);
bdonlan
I would consider switch a classical comparison
Ralph Rickenbach
wouldn't a switch/case statement fail the "without any conditional or comparison operator" requirement?
none
switch isn't an operator. It's a statement.
bdonlan
Yes, IF is a statement, too, yet a conditional one. http://en.wikipedia.org/wiki/Conditional_statement
soulmerge
Yes... and the question asked to avoid conditional/comparison _operators_...
bdonlan
A: 

I haven't seen a solution yet that is valid for the complete domain.

An other solution is to call a function that raises an exception if the input value is 0 or below 0. Then catch the exception and print 0.

Or you can use a function Sign, that returns -1 if the input is <0, 0 if it's 0 and 1 otherwise.

print ((sign(x)+1) * sign(x) / 2) * x.

sign can be -1, 0 or 1, so ((sign(x)+1) * sign(x) / 2) can have the following values:

-1 -> ((-1+1)*-1)/2 = 0
 0 -> ((0+1)*0)/2 = 0
 1 -> ((1+1) * 1)/2  = 1

Another method is to create a lookup table that maps all non negative numbers to themself and the rest to 0.

But, in my opinion, the original function is much clearer. So why violate the KISS principle.

Gamecat
Because it's an interview question :) Sure point it out to them (that the original function is much clearer), but they want to test your ingenuity or something, not your coding principles.
Ant
I know, but sometimes they want you to answer the question they do not ask. Besides, it never harms if you act smart on your inverview, unless you aren't.
Gamecat
+6  A: 

Let's say that number is represented by an 8-bit two's complement integer.

Positive numbers including 0 all have the MSB set to 0.

Negative numbers all have the MSB set to 1.

So we take the complement of the MSB, extend it to the full 8 bits, and bitwise AND it with the original number, e.g.

Positive:

00110101 -> MSB is 0

11111111 -> complement of MSB extended

00110101 -> bitwise AND of above

Negative:

10110101 -> MSB is 1

00000000 -> complement of MSB extended

00000000 -> bitwise AND of above

No comparisons needed - I'm kind of assuming that bitwise AND isn't strictly a comparison.

Also, sorry for the lack of code, but you get the idea.

Cybergibbons
+1 Excellent solution for integers, not for floats. I just love binary math :)
Roee Adler
A: 

Similar to the accepted answer. Although acceptable where absolute value is implemented using comparisons, but also more prone to overflow:

print( (sqrt(Number * Number) + x) / 2);

patros