Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Brite
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Brand Logo

agnos.is Forums

  1. Home
  2. Programmer Humor
  3. Infallible Code

Infallible Code

Scheduled Pinned Locked Moved Programmer Humor
programmerhumor
52 Posts 40 Posters 3 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • _cnt0@sh.itjust.works_ [email protected]

    Though, obviously I had to come up with some ridiculous solutions:

    bool IsEven(int i) => ((Func<string, bool>)(s => s[^1] == 48))($"{i:B}");
    

    This one works without conditionals šŸ™‚

    bool IsEven(int i)
    {
        try
        {
            int _ = (i & 1) / (i & 1);
        }
        catch (Exception)
        {
            return true;
        }
    
        return false;
    }
    
    L This user is from outside of this forum
    L This user is from outside of this forum
    [email protected]
    wrote on last edited by
    #33

    s[^1]

    Ohh wow, I've been learning it casually for years, and I didn't know that existed in C#. I guess I should go back and hit the books some more.

    1 Reply Last reply
    0
    • P This user is from outside of this forum
      P This user is from outside of this forum
      [email protected]
      wrote on last edited by
      #34

      If small numbers are much more frequent, it's better to return early. Really, you should gather statistics about the numbers the function is called with, and put the most frequent ones at the top.

      1 Reply Last reply
      2
      • themoonisacheese@sh.itjust.worksT [email protected]

        Obligatory: https://codegolf.stackexchange.com/q/275739/88192

        L This user is from outside of this forum
        L This user is from outside of this forum
        [email protected]
        wrote on last edited by
        #35

        The fuck is that

        themoonisacheese@sh.itjust.worksT 1 Reply Last reply
        1
        • J [email protected]

          Granted, writing the boilerplate for all possible 64 bit integers is a bit laborious,

          I've been trying to figure out roughly how many lines of code that would equal out to but I've run out of fingers.

          xxce2aab@feddit.dkX This user is from outside of this forum
          xxce2aab@feddit.dkX This user is from outside of this forum
          [email protected]
          wrote on last edited by
          #36

          Ah, common issue. When that happens, you just start using your toes.

          1 Reply Last reply
          2
          • L [email protected]

            Yeah, but did your dad work at Blizzard??

            M This user is from outside of this forum
            M This user is from outside of this forum
            [email protected]
            wrote on last edited by
            #37

            I’m the first ever second generation Blizzard employee!

            S 1 Reply Last reply
            8
            • W [email protected]

              Definitely a vibe.

              V This user is from outside of this forum
              V This user is from outside of this forum
              [email protected]
              wrote on last edited by [email protected]
              #38

              The only way I could see that happening is if enough people poisoned the data to do this

              1 Reply Last reply
              1
              • E [email protected]
                assert IsEven(-2);
                
                lambda@programming.devL This user is from outside of this forum
                lambda@programming.devL This user is from outside of this forum
                [email protected]
                wrote on last edited by
                #39

                Now see, you need the other method. IsNegativeEven()

                S 1 Reply Last reply
                12
                • cm0002@lemmy.worldC [email protected]
                  This post did not contain any content.
                  B This user is from outside of this forum
                  B This user is from outside of this forum
                  [email protected]
                  wrote on last edited by
                  #40

                  Even plus even equals even.

                  Odd plus odd equals even.

                  Only odd plus even makes an odd.

                  Are there twice as many even numbers as there are odd numbers?

                  witchfire@lemmy.worldW 1 Reply Last reply
                  11
                  • L [email protected]

                    The fuck is that

                    themoonisacheese@sh.itjust.worksT This user is from outside of this forum
                    themoonisacheese@sh.itjust.worksT This user is from outside of this forum
                    [email protected]
                    wrote on last edited by
                    #41

                    Okay so basically this is saving bytes on a technicality but also good programming language design (for this specific purpose).

                    The first aspect is that since you're scored on bytes, it's not really to your advantage to use a language that uses ascii (or utf-8) for it's tokens, because a large part of it is unprintables like DEL or BELL. So people have designed specially crafted golfing programming languages that use a full 256 possible characters in order to pack as many features as possible in as few bytes as possible.

                    The good design part of it is that if you really think about it hard, there's really not that many things you expect a programming language to do. It turns out that 256 total different operands is about in the sweet spot, so each character that's available in the 1-byte code page is mapped to one command, and the languages are also designed to make as many things as possible implicit, both at the cost of readability. Remember, all that matters here is getting the lowest score, not code maintainability or anything else.

                    This leads to languages like japt (which is a terse form of JavaScript, I'm pretty sure) or pyth (same for python) or Vyxal (my personal favorite, used to be python based but is now bespoke) that look like this but absolutely own at getting a task out in as few bytes as possible.

                    1 Reply Last reply
                    2
                    • lambda@programming.devL [email protected]

                      Now see, you need the other method. IsNegativeEven()

                      S This user is from outside of this forum
                      S This user is from outside of this forum
                      [email protected]
                      wrote on last edited by
                      #42

                      We can avoid expensive branches (gasp) by using some bitwise arithmetic to achieve the so-called "absolute value", an advanced hacker technique I learnt at Blizzard. Also unlike c, c# is not enlightened enough to understand that my code is perfect so it complains about "not all code paths returning a value".

                      private bool IsEven(int number)
                      {
                          number *= 1 - 2*(int)(((uint)number & 2147483648) >> 31);
                          if (number > 1) return IsEven(number - 2);
                          if (number == 0) return true;
                          if (number == 1) return false;
                          throw new Exception();
                      }
                      
                      U 1 Reply Last reply
                      8
                      • B [email protected]

                        Even plus even equals even.

                        Odd plus odd equals even.

                        Only odd plus even makes an odd.

                        Are there twice as many even numbers as there are odd numbers?

                        witchfire@lemmy.worldW This user is from outside of this forum
                        witchfire@lemmy.worldW This user is from outside of this forum
                        [email protected]
                        wrote on last edited by
                        #43

                        Even plus even

                        Odd plus odd

                        Even plus odd

                        Odd plus even

                        N 1 Reply Last reply
                        6
                        • witchfire@lemmy.worldW [email protected]

                          Even plus even

                          Odd plus odd

                          Even plus odd

                          Odd plus even

                          N This user is from outside of this forum
                          N This user is from outside of this forum
                          [email protected]
                          wrote on last edited by
                          #44

                          Are we working with a non-abelian group? Other wise this would work.

                          1 Reply Last reply
                          0
                          • M [email protected]

                            I’m the first ever second generation Blizzard employee!

                            S This user is from outside of this forum
                            S This user is from outside of this forum
                            [email protected]
                            wrote on last edited by
                            #45

                            This is such an unhinged thing to say, it had me rolling.

                            1 Reply Last reply
                            2
                            • mossyfeathers@pawb.socialM [email protected]

                              Did you know that he worked for Blizzard for seven years? Not only that, but he was Blizzard's first second-generation employee. He grew up in Blizzard. An extreme accomplishment to be certain. Thank you based and blizzpilled Pirate Software.

                              I wonder if he was the one stealing the breast milk. After all, I've heard he can be really childish.

                              S This user is from outside of this forum
                              S This user is from outside of this forum
                              [email protected]
                              wrote on last edited by
                              #46

                              The funniest thing about his blizzard obsession and the fact that his dad is like a first generation blizzard employee is such a weird glex these days.

                              So, did you get to harass any women yourself, or did you get your dad watch doing it? Were you a high enough employee that they gave you the Bill Cosby room to rape someone?

                              1 Reply Last reply
                              1
                              • E [email protected]

                                I removed the tail recursion for you:

                                private book IsEven(int number) {
                                    if(number > 1) return IsEven(number - 2) == true;
                                    if(number == 0) return true; 
                                    if(number == 2) return false;
                                }
                                
                                F This user is from outside of this forum
                                F This user is from outside of this forum
                                [email protected]
                                wrote on last edited by
                                #47

                                What'd be the result for IsEven(1)?

                                E 1 Reply Last reply
                                2
                                • lefrog@discuss.tchncs.deL [email protected]

                                  This noob even forgot zero

                                  P This user is from outside of this forum
                                  P This user is from outside of this forum
                                  [email protected]
                                  wrote on last edited by
                                  #48

                                  That and all negative values

                                  0 1 Reply Last reply
                                  1
                                  • F [email protected]

                                    What'd be the result for IsEven(1)?

                                    E This user is from outside of this forum
                                    E This user is from outside of this forum
                                    [email protected]
                                    wrote on last edited by
                                    #49

                                    Stack overflow

                                    1 Reply Last reply
                                    4
                                    • xxce2aab@feddit.dkX [email protected]

                                      Pah, mathematicians and their generally applicable pure approach to solutions and fancy modulus operations, who needs 'em? Computing is applied and we always work with well-defined finite precision. Granted, writing the boilerplate for all possible 64 bit integers is a bit laborious, but we're programmers! That's what code generation is for.

                                      B This user is from outside of this forum
                                      B This user is from outside of this forum
                                      [email protected]
                                      wrote on last edited by
                                      #50

                                      You can generate the code with a simple macro.

                                      1 Reply Last reply
                                      1
                                      • P [email protected]

                                        That and all negative values

                                        0 This user is from outside of this forum
                                        0 This user is from outside of this forum
                                        [email protected]
                                        wrote on last edited by
                                        #51

                                        Patience guys, I'm sure they'll get to supporting zero and negative integers as soon as they finish all positive integers

                                        1 Reply Last reply
                                        0
                                        • S [email protected]

                                          We can avoid expensive branches (gasp) by using some bitwise arithmetic to achieve the so-called "absolute value", an advanced hacker technique I learnt at Blizzard. Also unlike c, c# is not enlightened enough to understand that my code is perfect so it complains about "not all code paths returning a value".

                                          private bool IsEven(int number)
                                          {
                                              number *= 1 - 2*(int)(((uint)number & 2147483648) >> 31);
                                              if (number > 1) return IsEven(number - 2);
                                              if (number == 0) return true;
                                              if (number == 1) return false;
                                              throw new Exception();
                                          }
                                          
                                          U This user is from outside of this forum
                                          U This user is from outside of this forum
                                          [email protected]
                                          wrote on last edited by
                                          #52

                                          Nice, now we just need another function to find odd numbers, cause not all numbers are even you know.

                                          1 Reply Last reply
                                          1
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          • Login

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • World
                                          • Users
                                          • Groups