383

Dont you challenge me

Comments
  • 13
    the power of tuples
  • 100
    How much C code do you think it took to give you that simplicity in python? 😂
  • 35
    Instead of additions you can
    a = a xor b
    b = a xor b
    a = a xor b
    It should be the more efficient way and it's quite obscure the first time you read it
  • 0
    Yeah Cython depends on both
  • 1
    Welcome to devRant!
  • 6
    Can do that in Ruby too!
  • 6
    Thats Python baby and ruby too
  • 6
    Python and Ruby will never be better than C because without C they would not exist however.... I use python to generate C code from an xml schema
  • 6
    @jonjo I doubt C would ever be such a horrible parent.

    Surly it would want it's children to exceed and become the best they possibly could.

    Why would C hold them back.

    😁😁😁😁😁
  • 0
    @querellaMMXII speak brother speak that's the truth
  • 4
    @querellaMMXII you can not exceed something that you rely on to live... C is god
  • 16
    [b, a] = [a, b];

    JavaScript is better than it used to be
  • 1
    (a,b) = (b,a) in swift
  • 0
    @m93mark hell of your xoring - mark it in asm xD
  • 2
    @m93mark Hacker's delight? That book it's full of beautiful ingenious hacks!
  • 3
    I believe the whole point isn't to use a "third party" piece of code.

    I can write my own C function that'll just be
    Swap(a, b);
    Which is basically what the standard Python library did, and gave you it as a standard, but behind the scenes, it was very likely done the very same way it was done here in C.
  • 2
    @MrJDev @apex actually I saw it in my x86 assembly course cause the exchange op was too mainstream u.u
  • 8
    b =(a+b)-(b=a)
  • 1
    Can't you do this in most modern languages?
  • 3
    @m93mark Well, anyway, you should check out that book. I discovered it because I was solving a "bit counting" problem, so I went to Java source to see how they did it. There it was, a comment and a reference to the book and chapter that describes a way to do it, complexity time O(1), it's amazing.
  • 1
    @apex thanks I'll look into it
  • 0
    @m93mark it also prevents overflow :)
  • 1
    @m93mark I was about to comment the same
  • 1
    Who wants to write the Assembly generated from that python :D
  • 1
    I can't because I code in erlang
  • 1
    @B45i typo, right? I don't see the variable a getting the value set anywhere?
    But nice oneliner. Shouldn't it be
    a=(a+b)-(b=a)
  • 2
    @siljamicke ma bad, made a typo there.
  • 2
    (((((((((a,b) = (b,a))))))))))
    Lisp here
  • 1
    @balambuc that is wrong on so many levels of parenths
  • 0
    I know the above was a joke but FYI this is the (common) lisp way of doing it

    (cl-rotatef a b)
  • 2
    Actually...

    In C++ you'd have to check whether the numbers won't overflow. Python doesn't give a fuck and handles arbitrarily large numbers.
  • 2
    why would you ever need to do that? I have never needed to do a value swap.
  • 2
    In js: [a,b] = [b,a]
  • 1
    @nicholai I beat you to it ;)
  • 3
    @Hedgepig noooo. Haha. Tbh the thread was tl;dr
  • 1
    @nicholai yeah definitely, you're forgiven :p
  • 1
    @m93mark that's clever. Makes total sense too if you think about it for a minute.
  • 0
    @BartBB no! Swap(&a, &b) :)
Add Comment