Ranter
Join devRant
Do all the things like
++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatar
Sign Up
Pipeless API
From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple API
Learn More
Comments
-
-
kamen69256yNow that I checked, destructuring is also available in vanilla ES6 (though not in IE).
-
Parzi86636ya = str(a)+str(b)
b = int(a[:len(b)-1])
a = int(a[len(b)-1:])
Technically this would work in Python but the original Python method would be easier. -
Parzi86636y@LinusTorvald we're talking generally, not edge cases. However, if you have 2 variables with INT_MAX, why swap them?
-
Fabian10136yI've once written a Fibonacchi program in Java using only two variables. Without recursion!
https://codegolf.stackexchange.com/...
With recursion, it would be easy with one variable, but also horribly slow. My version has basically normal runtime. -
Rocket3G2006y@LinusTorvald in most programming languages, INT_MAX+1 = INT_MIN, because the way overflowing works.
Given the case of a=6 and b=7 and INT_MIN=-10 and INT_MAX = 10:
a=a+b
b=a-b
a=a-b
a = 6+7 = 13 = -7
b= -7 - 7 = -14 = 6
a=-7 - 6 = -13 = 7
Therefore a and b are indeed swapped. The only case where it doesn't work if either a or b are larger than INT_MAX or less than INT_MIN -
PaperBag8606y@LinusTorvald Not accurate. If the sum overflows, then it will just overflow back when you subtract from it.
Let's say the range is 0-9 and a = 5, b = 7:
a = 5 + 7 = 2
b = 2 - 7 = 5
a = 2 - 5 = 7 -
tokumei21256y@Rocket3G @PaperBag That's only true for C-like languages though, and only in architectures where signed numbers are either twos complement or biased. Technically speaking, implicitly overflowing signed integers is undefined behavior because of the varying implementations in the general case.
-
Parzi86636y@LinusTorvald the way you put it made it look like you were using an edge case where both a AND b were INT_MAX.
-
-
tokumei21256y@Gregozor2121
a = 0xf00d
b = 0xf00d
a ^= b // 0xf00d ^ 0xf00d == 0
b ^= a // 0xf00d ^ 0 == 0xf00d
a ^= b // 0 ^ 0xf00d == 0xf00d -
@tokumei
Well Cnotesforprofessionals have said otherwise that is why i had doubts -
Fabian10136y@GodlikeBlock Java: a=new int[]{b,b=a}[0];
Works! But technically it does use a third variable, the array. It just has no name.
This is pretty similar to my awesomely horrible Fibonacchi codegolf solution: https://codegolf.stackexchange.com/...
Also, this post doesn't appear at all in the list when searching for "python", despite having the tag. Great website, DevRant team, works flawlessly.
Related Rants
Deal with it bitych! 😂
joke/meme
python
programmer