programming.dev
  • Communities
  • Create Post
  • Create Community
  • heart
    Support Lemmy
  • search
    Search
  • Login
  • Sign Up
KHTangent@lemmy.world to Programmer Humor@lemmy.mlEnglish · 2 years ago

Swapping integers in Python

lemmy.world

message-square
7
link
fedilink
  • cross-posted to:
  • [email protected]
  • python
61

Swapping integers in Python

lemmy.world

KHTangent@lemmy.world to Programmer Humor@lemmy.mlEnglish · 2 years ago
message-square
7
link
fedilink
  • cross-posted to:
  • [email protected]
  • python
alert-triangle
You must log in or register to comment.
  • abclop99@beehaw.org
    link
    fedilink
    English
    arrow-up
    7
    ·
    2 years ago
    a = a ^ b
    b = a ^ b
    a = a ^ b
    
  • pewpew@feddit.it
    link
    fedilink
    English
    arrow-up
    3
    ·
    2 years ago

    So you are asking the user to do it

  • Jason Novinger
    link
    fedilink
    English
    arrow-up
    2
    ·
    2 years ago

    I raise you

    @lambda _: _()
    def result() -> int:
        global a; global b; a, b = b, a
    

    hat tip to @[email protected] via Cursed Python

    • 𝕊𝕚𝕤𝕪𝕡𝕙𝕖𝕒𝕟
      link
      fedilink
      English
      arrow-up
      2
      ·
      edit-2
      2 years ago

      Haha thanks, but the original idea is from @goodside on Twitter, I just screenshotted his tweet and harvested the upvotes :D

      • Jason Novinger
        link
        fedilink
        English
        arrow-up
        2
        ·
        2 years ago

        D’oh, you’re right! My bad and thanks for the catch!

  • bauklotz@feddit.de
    link
    fedilink
    English
    arrow-up
    1
    ·
    2 years ago

    Version 3 is disgusting.

    • 1stTime4MeInMCU@mander.xyz
      link
      fedilink
      English
      arrow-up
      1
      ·
      2 years ago

      That’s like the canonical “swap two pointers” trick

Programmer Humor@lemmy.ml

programmerhumor@lemmy.ml

Subscribe from Remote Instance

Create a post
You are not logged in. However you can subscribe from another Fediverse account, for example Lemmy or Mastodon. To do this, paste the following into the search field of your instance: [email protected]

Post funny things about programming here! (Or just rant about your favourite programming language.)

Rules:

  • Posts must be relevant to programming, programmers, or computer science.
  • No NSFW content.
  • Jokes must be in good taste. No hate speech, bigotry, etc.
Visibility: Public
globe

This community can be federated to other instances and be posted/commented in by their users.

  • 173 users / day
  • 780 users / week
  • 1.68K users / month
  • 9.9K users / 6 months
  • 725 local subscribers
  • 35.6K subscribers
  • 1.74K Posts
  • 38.5K Comments
  • Modlog
  • mods:
  • AgreeableLandscape@lemmy.ml
  • cat_programmer@lemmy.ml
  • BE: 0.19.11
  • Modlog
  • Legal
  • Instances
  • Docs
  • Code
  • join-lemmy.org