5 ms·
How do you flip two variable without a third one ?
by siscia 11y ago
How do you flip two variable without a third one ?
- sllabres 11y agosearch XOR (X=X XOR Y; Y=X XOR Y; X=X XOR Y)
- pdiddy 11y agoAnother way is to just use subtraction: x = x + y y = x - y x = x - y I originally learned about this while preparing for some interviews. The funny thing, I find, is that in Python you can simply do this: x, y = 5, 10 x, y = y, x I was asked this question in an interview and I responded that you can do this. His response: "Python is weird."
- deleted 11y ago[deleted]