Returning another argument from two possible arguments without using conditions

For example, if I have a function that is guaranteed to receive 5 or 7 as an argument, I want the function to return 5 if they were received 7 and 7, if they were received 5 without any conditions.

I was asked this in an interview and were pretty dumb, thanks.

+42
java
Feb 24 '13 at 20:37
source share
7 answers

Simple arithmetic:

return 7 - input + 5;

(which can be simplified as return 12 - input; )

Say the input is 7:

return 7 - 7 + 5 β†’ return 5

Or, if input 5:

return 7 - 5 + 5 β†’ return 7

+94
Feb 24 '13 at 20:39
source share

You can use any simple commutative calculation that can be undone:

  • complement: f(x)=7+5-x
  • xor: f(x)=7^5^x
  • multiplication: f(x)=7*5/x
+71
Feb 24 '13 at 20:43
source share
 public int f(int x) { return x ^ 2; } 

In binary format:

 7 = 111 5 = 101 2 = 010 

XOR (^ in java) flips 2 bits if it is turned off and off if it is turned on.

+33
Feb 24 '13 at 20:41
source share

What about:

 public int q(int in) { static final int[] ret = {0, 0, 0, 0, 0, 7, 0, 5}; return ret[in]; } 
+14
Feb 25 '13 at 9:22
source share

If I were an interview, and you solved it only for entering numbers, my next question would be: "How would you solve this problem for non-numeric input?" because I would not be looking for mathematical tricks. Instead, how about this?

 List<String> options = new ArrayList<>(Arrays.asList("bob", "fred")); options.remove("bob"); System.out.println(options.get(0)); 

This, obviously, easily adapts to any type, including Object , as long as the equality of objects works correctly and as a bonus it can be expressed much more concisely in other languages ​​such as Groovy:

 println((["bob", "fred"] - "bob").first()) 

The output, in any case, is clearly "fred". If I were one of the interviews, this is the answer I would be looking for.

+10
Feb 24 '13 at 9:15
source share
 public int xyz(int x) { return 35 / x; } 
+8
Feb 25 '13 at 10:08
source share

How does xor work? [for the case f (x) = 7 ^ 5 ^ x]

XOR (^) is an exceptional OR and works this way

 a|b|a^b ------- 0|0| 0 0|1| 1 1|0| 1 1|1| 0 

So, XOR (^) can be used to change the bits of a certain number. For example, if we want to change the last two bits of any number (for example, from xxxx10 to xxxx01 ), we can do this with numbrer ^ 3 , since 3 is binary 00011.

Here are some facts about XOR

  • XOR is symmetric β†’ a^b = b^a
  • XOR is associative (a^b)^c = a^(b^c)
  • a^a = 0 (units in a will be replaced by zeros, and zeros will not be changed)

    example for a = 157 (binary code 010011101)

      010011101 ^ 010011101 ----------- 000000000 
  • 0^a = a (units in a can only change zeros so that they change them to units)

      000000000 ^ 010011101 ----------- 010011101 

therefore, using facts (1) and (2) 7^5^x == x^7^5 == x^5^7

Let's try to check how x^7^5 will work for x=7 .

 (x^7)^5 = (7^7)^5 = 0^5 = 5 

And the same thing happens for x=5

 (x^5)^7 = (5^5)^7 = 0^7 = 7 
+7
Feb 24 '13 at 9:22
source share



All Articles