• WraithGear@lemmy.world
    link
    fedilink
    English
    arrow-up
    0
    ·
    edit-2
    7 months ago

    Would this be a case of modulo saving the day?

    Like: If Number modulo 2 = 0, true

    This has to be taken out of context

  • Aedis@lemmy.world
    link
    fedilink
    arrow-up
    0
    ·
    7 months 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
      0
      ·
      7 months 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
        0
        ·
        7 months 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)))

          • balsoft@lemmy.ml
            link
            fedilink
            arrow-up
            0
            ·
            7 months ago

            I don’t think there’s much to codegolf. The “obvious” solution (even() (($1%2))) is both shorter and faster. Don’t think it can be optimized much more.

        • tetris11@lemmy.ml
          link
          fedilink
          arrow-up
          0
          ·
          7 months 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

            • tetris11@lemmy.ml
              link
              fedilink
              arrow-up
              0
              ·
              7 months ago

              Oh I see it, but for some reason I was taught to always use $(( arith )) instead of (( arith )) and I guess I’m just wondering what the difference is

              • balsoft@lemmy.ml
                link
                fedilink
                arrow-up
                0
                ·
                7 months ago

                The difference is that (( is a “compound command”, similar to [[ (evaluate conditional expression), while $(( )) is “aritmetic expansion”. They behave in almost exactly the same way but are used in different contexts - the former uses “exit codes” while the latter returns a string, so the former would be used where you would expect a command, while the latter would be used where you expect an expression.

                This is similar to how there is ( compound command (run in a subshell), and $( ) (command substitution). You can actually use the former to define a function too (as it’s a compound command):

                real_exit() { exit 1; }
                fake_exit() ( exit 1 )
                

                Calling real_exit will exit from the shell, while calling fake_exit will do nothing as the exit 1 command is executed in a separate subshell. Notice how you can also do the same in a command substition (because it runs in a subshell):

                echo $(echo foo; exit 1)
                

                Will run successfully and output foo.

                It is another one of those unknown, very rarely useful features of bash.

  • AlyxMS [he/him]@hexbear.net
    link
    fedilink
    English
    arrow-up
    0
    ·
    7 months ago

    What you do is use a for loop to generate a million lines for you, then paste it in. Writing it manually is moronic. You can easily make it support numbers above 1,000,000 too this way, talking about scalable

    • balsoft@lemmy.ml
      link
      fedilink
      arrow-up
      0
      ·
      7 months ago

      No, silly. You ask chatgpt to write (steal) a loop that would generate all those lines. Haven’t you heard about meta-vibe template-programming?

      • AlyxMS [he/him]@hexbear.net
        link
        fedilink
        English
        arrow-up
        0
        ·
        7 months ago

        I once saw someone unironically sharing their new trick of “if you need to write a bunch of really similar lines just with different numbers, chatGPT does it really well”.

        Kind of blew my mind (and everyone else that saw the thread)

    • Destide@feddit.ukOP
      link
      fedilink
      English
      arrow-up
      0
      ·
      7 months 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
        0
        ·
        7 months 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
          0
          ·
          7 months 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.

          • fushuan [he/him]@piefed.blahaj.zone
            link
            fedilink
            English
            arrow-up
            0
            ·
            7 months ago

            I would have preferred for the function to be called mod, since it’s the modulo operation, which in math is represented with a percentage or “mod”. Most programming languages use a percentage because of that, so do a lot of calculators.

          • balsoft@lemmy.ml
            link
            fedilink
            arrow-up
            0
            ·
            7 months 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.

  • 4am@lemmy.zip
    link
    fedilink
    arrow-up
    0
    ·
    7 months ago

    Fucking rent free. Jesus Christ you clowns, I almost want them to take away all your video games now

    • Ambii [she/her]@hexbear.net
      link
      fedilink
      English
      arrow-up
      0
      ·
      7 months ago

      oh no not my toys, like all good consoomers i put 99% of my self worth and identity into the things i consume mindlessly so this is devastating.

      • 4am@lemmy.zip
        link
        fedilink
        arrow-up
        0
        ·
        7 months ago

        I think you got wooshed here

        The guy in the picture is PirateSoftware, accused of sabotaging the Stop Killing Games initiative in the EU.

        This post is part of a coordinated harassment campaign by big angy gamers because bad nerd man say companies won’t listen to their little lib demands to the EU

        So instead of regrouping their efforts (which, I might add, they did, and they got their day in parliament) they (started off by) basically trashing this dude until he lost a large portion of his community. Because like they didn’t like how he defended his position. About video games. And now the fucking internet won’t shut up about it even though they got their way again through toxic bullying. And it annoys me. So I shitpost about it wherever I can because I hope it annoys them too.

        • Oriel Jutty :hhHHHAAAH:@infosec.exchange
          link
          fedilink
          arrow-up
          0
          ·
          7 months ago

          There’s a lot of distorted facts here, but the weirdest one to me is “instead of regrouping their efforts (which, I might add, they did, and they got their day in parliament)”. The first half just contradicts itself (“instead of doing X, which they did, …”???) and the second half (“they got their day in parliament”) is verifiably, obviously false: The EU petition is still ongoing and collecting signatures. The deadline is July 31.

      • 4am@lemmy.zip
        link
        fedilink
        arrow-up
        0
        ·
        7 months ago

        I’ve never met anyone cool who cooks up strawmen memes because they’re exhausted all their actual criticisms.

        The piratesoftware shit is so tiresome. Who the fuck cares? What is this going to accomplish? He’s not even popular anymore

        • TrickDacy@lemmy.world
          link
          fedilink
          arrow-up
          0
          ·
          7 months ago

          Who cares? You. You’re the one who knows what this even is. ReNt FrEe is always a sign of having no argument

  • FunkyStuff [he/him]@hexbear.net
    link
    fedilink
    English
    arrow-up
    0
    ·
    7 months ago

    Throwback to when someone shared the OG version of this meme to my uni chat, I replied with "Oh you can simply do

    def is_even(n: int) -> boolean:
        if n > 0 return not is_even(n - 1)
        elif n < 0 return not is_even(n + 1)
        else return True
    

    And instead of laughing at the joke the TA in the chat said “When you start getting internships you’ll do n % 2” like I was being serious.

  • 6stringringer@lemmy.zip
    link
    fedilink
    arrow-up
    0
    ·
    7 months ago

    Thanks to goodness, finally. A (giggle & snort) solid algorithm. There ya’s go set yer clocks & go get a haircut.