Convert 0 to 1 and Vice Versa
19,255
Solution 1
A few obvious possibilities:
!n
1-n
n^1
n==0
n!=1
n<1
Solution 2
Simple arithmetic:
x = 1 - x;
Actually, there are an infinite number of polynomials that will map 1 to 0 and vice versa. For example:
x = x * x * x * x * x - x * x * x * x + x * x - 2 * x + 1;
Solution 3
Lookup table:
int[] swap = { 1, 0 };
And later:
x = swap[x];
Solution 4
Take a paper clip. Straighten it out. It's a 1. Bend it to meet its ends. It's a 0. To make it a 1, straighten it.
Solution 5
Some Trig: COS(PI * N)^2
In python
import math
math.cos(math.pi * n) ** 2
I can't believe people forgot modulus:
(3 + n) % 2
Related videos on Youtube
Author by
javaguy
Updated on April 21, 2022Comments
-
javaguy about 2 years
I was asked in an interview : how to convert 0 to 1 and 1 to 0. I answered :
- Simple
if
andswitch
- Bit flipping.
Are there any other approach?
-
BalusC about 14 yearsDidn't knew that Java is language agnostic. Cool.
-
Ken about 14 yearsdrelihan: It's a test to see if they're an architecture astronaut, or a programmer. They're looking to see if you draw a UML diagram and start talking about WSDL.
- Simple
-
T.J. Crowder about 14 years
!n
isn't reliably 1 => 0, 0 => 1 in a language agnostic way (in some languages, !0 = -1); in Java, it doesn't even work ("operator ! cannot be applied to int"). -
Jerry Coffin about 14 years@GregS:good point...I've removed that one, and added a couple of others.
-
Rubys about 14 years1-n and -n + 1 are exactly the same in different ordering, kind of redundant.
-
Vlad Gudim about 14 yearsT.J. Crowder, if ! meant to be bitwise NOT then in Java it's represented by ~ (tilda)
-
Brandon Bodnar about 14 years@Totophil No, 0 ^ 1 = 1 and 0 ^ 0 = 0, so it does not flip it correctly, whereas 1 ^ 1 = 0 and 0 ^ 1 = 1, so n ^ 1 is correct.
-
Jerry Coffin about 14 years@T.J.Crowder:quite true -- almost no notation you can use is truly language agnostic. I don't think any of these is legal Scheme -- so of course I wrote in the one true language and ignored the "Java" tag as an obvious mistake. :-)
-
Vlad Gudim about 14 yearsBrandon Bodnár, that's not true. Please use a calculator if you must. Any number in power of 0 gives one, any number in the power of 1 gives itself.
-
Brandon Bodnar about 14 years@Totophil , ^ is the xor operator in most languages.
-
Vlad Gudim about 14 years@Brandon, ok I can see now, I owe you an apology: sorry. Then we really have two ways of flipping the number here: (n XOR 1) and (0 in power of n).
-
Schedler about 14 yearsOf these, only
1-n
andn xor 1
are language agnostic in the sense that they deal with integer values. All others mix integers with boolean results, making them unsuitable for strongly-typed languages. -
Ponkadoodle about 14 yearsWhat's wrong with this? The OP asked for any alternative methods.
-
Greg Hewgill about 14 years@Totophil: if ^ is the power operator, then 0 ^ 0 is undefined (mathematically, see mathworld.wolfram.com/Power.html).
-
Vlad Gudim about 14 years@Greg, true in theory, nonetheless for most practical purposes absolute majority of implementations resolve 0 ^ 0 = 1. On the other hand n==0, n!=1 and n<1 are relational operations and in most modern languages will result in boolean value, not flip the 0 to 1 and back. This and the fact that boolean true and false won't always be represented in memory by 1 and 0 might lead to a very intresting discussion during the interview. The expressions would work in C because language creators took a shortcut in representing boolean values to simplify language syntax for compiler builders.
-
Adam Gent about 14 yearsI don't think Java is powerful enough to do that.
-
Adam Gent about 14 yearsYou forgot modulus: (3 + n) % 2
-
Paul Nathan about 14 years@Adam: You probably need to find an embedded robot lisp to really do the job right. Only Embedded Robot Lisps have the power to truly transform 0s into 1s and vice-versa.
-
Adam Gent about 14 yearsThis is a totally awesome answer. I like it because its the least mathematical. Also unlike the other answers this will fail if the domain and range are outside of 0-1 ala fail fast although not gracefully. +1
-
pkr over 8 yearsSimplifies to
x^5 - x^4 + x^2 - 2x + 1
. Butx^n = x
forx = 0 or 1
, so it further simplifies tox - x + x -2x + 1
which is just1 - x
. (In case anyone was unsure if they could actually use the second polynomial.) -
fmnijk over 4 yearsWhich one is the best?
-
Jerry Coffin over 4 years@蔡易翔 lacking specific reason to do otherwise, I'd probably use 1-n.
-
TheEagle over 2 yearsARGH - why didn't I think of this ? +(1 - 0)