• edinbruh@feddit.it
    link
    fedilink
    English
    arrow-up
    40
    ·
    6 months ago

    For a time on Reddit (some years ago when I still used it) there was a trend of finding the worst way of implementing is_even(x: int) -> bool. My contribution to that was a function that ran Ackerman(x,x) flipping a Boolean at every iteration, and check if it was true or false at the end.

    It works btw, I will find the proof later

      • edinbruh@feddit.it
        link
        fedilink
        English
        arrow-up
        19
        ·
        6 months ago

        The implementation is not very exciting, I capture a variable in python. It could have been done more cleanly.

        1000041934

        The proof is this. But, I could have made mistakes, it was many years ago.

        1000041935

        Note that in python you’ll never be able to run is_even(5) the stack cannot handle it

        Edit: daaaamn, that variable is ugly as hell. I would never do things like that now.

        • boonhet@sopuli.xyz
          link
          fedilink
          arrow-up
          1
          ·
          6 months ago

          That’s , uh…

          Yeah. Cooler than anything I could’ve achieved for purposefully bad is_even

          My first idea of a purposefully bad is_even is this:

          def is_even(i):
              return True if i == 0 else not is_even(abs(i)-1)
          

          But I’m sure I could come up with worse given enough time.

          • edinbruh@feddit.it
            link
            fedilink
            English
            arrow-up
            1
            ·
            6 months ago

            That’s also slower than most of the stuff you could come up with. There were other approaches that were almost worse though, like “the function is a switch-case that returns false by default. As complaint tickets are opened, more cases get added to the switch-case”

            • boonhet@sopuli.xyz
              link
              fedilink
              arrow-up
              1
              ·
              6 months ago

              the function is a switch-case that returns false by default. As complaint tickets are opened, more cases get added to the switch-case”

              Oh if that is acceptable, then my secondary idea of using an API call for this should work too. I thought that it would have to be guaranteed to be correct (as long as you don’t reach a stack overflow or something)

        • squaresinger@lemmy.world
          link
          fedilink
          arrow-up
          1
          ·
          6 months ago

          It never occurred to me that you could assign fields to a function. I mean, it totally makes sense considering that functions are objects in Python. It just never occurred to me that this is a thing one can do. Crazy.

          • edinbruh@feddit.it
            link
            fedilink
            English
            arrow-up
            1
            ·
            6 months ago

            Please don’t do that, I was stupid when I wrote that. But still, in very dynamic languages like python or js everything is an object, including functions, so you can just do object stuff on them.

            • squaresinger@lemmy.world
              link
              fedilink
              arrow-up
              1
              ·
              6 months ago

              I wasn’t going to, and after I saw it it totally makes sense that it’s possible, it just never occurred to me.

              I guess this could be used like static variables inside functions in c. So scope-limited global variables. Not a good design choice in most cases.