Brdsnest Lemmy
  • Communities
  • Create Post
  • Create Community
  • heart
    Support Lemmy
  • search
    Search
  • Login
  • Sign Up
andioop@programming.dev to Programming Horror@programming.devEnglish ·
edit-2
2 years ago

God I wish there was an easier way to do this

programming.dev

message-square
15
fedilink
3

God I wish there was an easier way to do this

programming.dev

andioop@programming.dev to Programming Horror@programming.devEnglish ·
edit-2
2 years ago
message-square
15
fedilink
alert-triangle
You must log in or register to comment.
  • Caveman@lemmy.world
    link
    fedilink
    arrow-up
    1
    ·
    2 years ago

    Of course there’s an easier way. Just integrate the state of the art API dedicated for this exact problem. https://isevenapi.xyz/

    • misophist@lemmy.world
      link
      fedilink
      arrow-up
      1
      ·
      2 years ago

      This is confusing. I’m already using the iSeven API to determine if a number is 7. I’m getting a namespace collision error when I try to load this new API. Bug report filed.

  • SpeakinTelnet@programming.dev
    link
    fedilink
    arrow-up
    0
    ·
    edit-2
    2 years ago
    def is_even(n):
        match n:
            case 1:
                return False
            case 0:
                return True
            # fix No1
            case n < 0:
                return is_even(-1*n)
            case _:
                return is_even(n-2)
    
    • sloppy_diffuser@sh.itjust.works
      link
      fedilink
      English
      arrow-up
      0
      ·
      2 years ago

      Python added match/case?! Bunch of mypy issues have been closed too. Maybe its time to dust off some old projects.

      • SpeakinTelnet@programming.dev
        link
        fedilink
        arrow-up
        1
        ·
        2 years ago

        It was added in 3.10 and is surprisingly complete. The tutorial pep is a good starting point to see what it can accomplish

  • Anticorp@lemmy.world
    link
    fedilink
    arrow-up
    0
    ·
    2 years ago

    Back when I was learning programming a lot of lessons would make you do something like this, and then show you the real way to do it in the next lesson. My reaction was always “why didn’t you lead with this?”.

    • Potatos_are_not_friends@lemmy.world
      link
      fedilink
      arrow-up
      1
      ·
      2 years ago

      You must see the pain before you confront it.

  • recursive_recursion [they/them]@programming.dev
    link
    fedilink
    English
    arrow-up
    0
    ·
    edit-2
    2 years ago

    modulo

    pseudocode:

    if number % 2 == 0
      return "number is even" (is_num_even = 1 or true)
    else
      return "number is odd" (is_num_even = 0 or false)
    

    plus you’d want an input validation beforehand

    • Vex_Detrause@lemmy.ca
      link
      fedilink
      arrow-up
      0
      ·
      2 years ago
      #You are an input. You have value! You matter!
      if number % 2 == 0
        return "number is even" (is_num_even = 1 or true)
      else
        return "number is odd" (is_num_even = 0 or false)
      

      Am I doing it right? /S.

      • PoolloverNathan@programming.dev
        link
        fedilink
        English
        arrow-up
        1
        ·
        2 years ago

        Don’t put nbsps in code blocks, they show up literally.

    • Mac@programming.dev
      link
      fedilink
      arrow-up
      0
      ·
      edit-2
      2 years ago

      who needs modulo when you can get less characters out of

      while (number > 1) {
        number -= 2;
      }
      return number;
      

      very efficient

      edit: or theres the trusty iseven api

      • NullPointer@programming.dev
        link
        fedilink
        arrow-up
        0
        ·
        2 years ago

        here is somewhat less:

        return (number % 2) == 0;

        • pivot_root@lemmy.world
          link
          fedilink
          arrow-up
          1
          ·
          2 years ago

          return !(number & 1);

          • MuchPineapples@lemmy.world
            link
            fedilink
            arrow-up
            1
            ·
            edit-2
            2 years ago

            This is the way. Modulo takes too long to compute, bitwise compare should be a lot faster.

            return !(number & 0x1);
            
            • recursive_recursion [they/them]@programming.dev
              link
              fedilink
              English
              arrow-up
              1
              ·
              edit-2
              2 years ago

              oh shit yo

              this comment chain is pretty awesome, I learned a lot from this thanks!

Programming Horror@programming.dev

programming_horror@programming.dev

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: !programming_horror@programming.dev

Welcome to Programming Horror!

This is a place to share strange or terrible code you come across.

For more general memes about programming there’s also Programmer Humor.

Looking for mods. If youre interested in moderating the community feel free to dm @Ategon@programming.dev

Rules

  • Keep content in english
  • No advertisements (this includes both code in advertisements and advertisement in posts)
  • No generated code (a person has to have made it)

Credits

  • Icon base made by Lorc under CC BY 3.0 with modifications to add a gradient
Visibility: Public
globe

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

  • 17 users / day
  • 18 users / week
  • 67 users / month
  • 107 users / 6 months
  • 1 local subscriber
  • 2.15K subscribers
  • 37 Posts
  • 148 Comments
  • Modlog
  • mods:
  • Vacant@programming.dev
  • BE: 0.19.5
  • Modlog
  • Instances
  • Docs
  • Code
  • join-lemmy.org