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
Comments

deadlyRants436973d

kamen320573dNow that I checked, destructuring is also available in vanilla ES6 (though not in IE).

Parzi253673da = 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. 
Parzi253673d@LinusTorvald we're talking generally, not edge cases. However, if you have 2 variables with INT_MAX, why swap them?

Fabian115473dI'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. 
Rocket3G22073d@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=ab
a=ab
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 
PaperBag86773d@LinusTorvald Not accurate. If the sum overflows, then it will just overflow back when you subtract from it.
Let's say the range is 09 and a = 5, b = 7:
a = 5 + 7 = 2
b = 2  7 = 5
a = 2  5 = 7 
tokumei257273d@Rocket3G @PaperBag That's only true for Clike 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.

Parzi253673d@LinusTorvald the way you put it made it look like you were using an edge case where both a AND b were INT_MAX.


tokumei257269d@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 
Fabian115443d@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.
Deal with it bitych! ðŸ˜‚
joke/meme
python
programmer