• Euphoma@lemmy.ml
    link
    fedilink
    English
    arrow-up
    2
    ·
    40 minutes ago
    def even(n: int) -> bool:
        code = ""
        for i in range(0, n+1, 2):
            code += f"if {n} == {i}:\n out = True\n"
            j = i+1
            code += f"if {n} == {j}:\n out = False\n"
        local_vars = {}
        exec(code, {}, local_vars)
        return local_vars["out"]
    

    scalable version

  • Caveman@lemmy.world
    link
    fedilink
    arrow-up
    5
    ·
    edit-2
    2 hours ago

    I’ll join in

    const isEven = (n) 
      => !["1","3","5","7","9"]
        .includes(Math.round(n).toString().slice(-1)) 
    
  • pivot_root@lemmy.world
    link
    fedilink
    arrow-up
    24
    arrow-down
    2
    ·
    edit-2
    4 hours ago

    That code is so wrong. We’re talking about Jason “Thor” Hall here—that function should be returning 1 and 0, not booleans.

    If you don't get the joke...

    In the source code for his GameMaker game, he never uses true or false. It’s always comparing a number equal to 1.

    • Aqarius@lemmy.world
      link
      fedilink
      arrow-up
      1
      ·
      11 minutes ago

      Frankly, it’s what I did, too, after coming out of Uni-level C.

      My code was goddamn unreadable.

  • Aedis@lemmy.world
    link
    fedilink
    arrow-up
    20
    ·
    5 hours ago

    I’m partial to a recursive solution. Lol

    def is_even(number):
        if number < 0 or (number%1) > 0:
            raise ValueError("This impl requires positive integers only") 
        if number < 2:
            return number
        return is_even(number - 2)
    
    • tetris11@lemmy.ml
      link
      fedilink
      arrow-up
      8
      ·
      edit-2
      5 hours ago

      I prefer good ole regex test of a binary num

      function isEven(number){
         binary=$(echo "obase=2; $number" | bc)
         if [ "${binary:-1}" = "1" ]; then
               return 255
         fi
         return 0
      }
      
      • balsoft@lemmy.ml
        link
        fedilink
        arrow-up
        4
        ·
        edit-2
        4 hours ago

        Amateur! I can read and understand that almost right away. Now I present a better solution:

        even() ((($1+1)&1))
        

        (I mean, it’s funny cause it’s unreadable, but I suspect this is also one of the most efficient bash implementations possible)

        (Actually the obvious one is a slight bit faster. But this impl for odd is the fastest one as far as I can tell odd() (($1&1)))

        • tetris11@lemmy.ml
          link
          fedilink
          arrow-up
          1
          ·
          3 hours ago

          woah your bash is legit good. I thought numeric pretexts needed $(( blah )), but you’re ommiting the $ like an absolute madman. How is this wizardy possible

        • Chais@sh.itjust.works
          link
          fedilink
          arrow-up
          1
          ·
          edit-2
          2 hours ago

          If your codebase is closed source there’s no risk of that happening, if it’s open source there’s nothing you can do about it.
          Either way there’s no use worrying.

  • kryptonianCodeMonkey@lemmy.world
    link
    fedilink
    arrow-up
    12
    ·
    edit-2
    5 hours ago
    def is_even(num):
        if num == 1:
            return False
        if num == 2:
            return True
        raise ValueError(f'Value of {num} out of range. Literally impossible to tell if it is even.')
    • TimeSquirrel@kbin.melroy.org
      link
      fedilink
      arrow-up
      6
      arrow-down
      1
      ·
      5 hours ago

      Or literally just look at its binary representation. If the least significant digit is a “1”, it’s odd, if “0”, it’s even. Or you can divide by 2 and check for a remainder.

      Your method is just spending time grinding away CPU cycles for no reason.

      • webadict@lemmy.world
        link
        fedilink
        arrow-up
        11
        arrow-down
        1
        ·
        5 hours ago

        Sorry we’re not all fucking math nerds like you who knows words like “significant” or “binary” or “divide”, Poindexter. Some of us make do with whatever solution is available!

        • TimeSquirrel@kbin.melroy.org
          link
          fedilink
          arrow-up
          2
          ·
          3 hours ago

          Maybe. And I can’t blame it on not having had coffee when I made the comment. Just me being completely oblivious to a joke.

    • Destide@feddit.ukOP
      link
      fedilink
      English
      arrow-up
      6
      arrow-down
      1
      ·
      edit-2
      7 hours ago

      As we’re posting examples I’ll add how lovely it is in Elixir. Elixir def not putting the fun in programmer memes do. One reason I picked it because I can’t be trusted to not be the meme.

      def is_even?(n) do
        rem(n, 2) == 0
      end
      
      • balsoft@lemmy.ml
        link
        fedilink
        arrow-up
        4
        ·
        edit-2
        4 hours ago

        I mean, it would be almost this exact thing in almost any language.

        fn is_even(n: i64) -> bool {
            n % 2 == 0
        }
        
        even n = n `rem` 2 == 0
        
        def is_even(n):
            return n % 2 == 0
        

        etc

        • vinnymac@lemmy.world
          link
          fedilink
          arrow-up
          1
          ·
          2 hours ago

          Personal preference, but elixir just strikes a balance that doesn’t make me feel like I’m reading hieroglyphs so I’m actually happy to see it praised.

          • balsoft@lemmy.ml
            link
            fedilink
            arrow-up
            1
            ·
            1 hour ago

            Yeah, I agree that Elixir is a fine language for some tasks. I personally find the readability somewhat average, but it’s very maintainable (due to how it enables clear program structure), the error handling is great, and the lightweight process system is amazing.

  • QuazarOmega@lemy.lol
    link
    fedilink
    arrow-up
    36
    ·
    8 hours ago

    No, no, you should group the return false lines together 😤😤

    if (number == 1) return false;
    else if (number == 3) return false;
    else if (number == 5) return false;
    //...
    else if (number == 2) return true;
    else if (number == 4) return true;
    //...
    
    • ImplyingImplications@lemmy.ca
      link
      fedilink
      arrow-up
      14
      arrow-down
      18
      ·
      9 hours ago

      YanDev is a literal pedophile. It’s honestly mind boggling people care more about a guy who won’t sign a petition on preserving video games than pedophiles and bigots. I don’t get the hate.

      • shea@lemmy.blahaj.zone
        link
        fedilink
        arrow-up
        5
        ·
        5 hours ago

        it’s not that he “wont sign it”. lmao. its that he comoketely unprovoked started a hate campaign against it, literally on the spot hearing about it on stream, directed his viewers not to engage with the petition and started making up a bunch of reasons while talking in that confident-but-clulesss voice about how its destructive and awful and short sighted, making up a bunch of atuff about it that was immediately disproven, just spewing all this vitriol for no reason. Not engaging with it is one thing but actively fighting against a wonderul consumer rights campaign like this, not to mention how important iy is to gaming history to be able to preserve games, is so anti-gamer i dont understand how he ever got a following. Hes a dipsh who talks out of his butthole and he appeals to the kind of lobenly nerd that thinks being an asshole is cool

      • deur@feddit.nl
        link
        fedilink
        arrow-up
        27
        arrow-down
        3
        ·
        9 hours ago

        Because this dumbass has existed for a lot longer than the single moment you are using to construct the strawman of “the enraged internet user over nothing other than a ‘petition’ (HUGE mischaracterization, he’s not eligible to sign in anyway)” and just like when yanderedev was finally widely controversial, the “yanderedev code is bad lol” memes and jokes were very popular.

        Can you at least pretend you understand how “the continuous flow of time works” before you post the dumbest shit ever?