• Rentlar@lemmy.ca
    link
    fedilink
    arrow-up
    107
    ·
    edit-2
    1 year ago

    They call me a StackOverflow expert:

    private bool isEven(int num) {
    if (num == 0) return true;
    if (num == 1) return false;
    if (num < 0) return isEven(-1 * num);
    return isEven(num - 2);
    }
    
    • Johanno@feddit.de
      link
      fedilink
      arrow-up
      17
      ·
      edit-2
      1 year ago

      StackoverflowException.

      What do I do now?

      Nvm. Got it.

        if(num % 2 == 0){
             int num1 = num/2
             int num2 = num/2
             return isEven(num1) && isEven(num2)   
        } 
      
      if(num % 3 == 0){
            int num1 = num/3
            int num2 = num/3
            int num3 = num/3
            return isEven(num1) && isEven(num2) && isEven(num3) 
      }
      

      Obviously we need to check each part of the division to make sure if they are even or not. /s

  • ???@lemmy.world
    link
    fedilink
    arrow-up
    90
    arrow-down
    3
    ·
    edit-2
    1 year ago

    I shit you not but one coworker I had dared call himself a data scientist and did something really similar to this but in Python and in production code. He should never have been hired. Coding in python was a requirement. I spent a good year sorting out through his spaghetti code and eventually rebuilt everything he had been working on because it was so bad that it only worked on his computer and he always pip freezes all requirements, and since he never used a virtual environment that meant we got a list of ALL packages he had installed on pip for a project. Out of those 100, only about 20 were relevant to the project.

      • herrvogel@lemmy.world
        link
        fedilink
        arrow-up
        16
        ·
        edit-2
        1 year ago

        Code reviews mean fuck all when the “senior” developer doing the review is someone who implements an entire API endpoint group in one single thousand-something lines magic function that is impossible to decipher for mere humans.

      • ???@lemmy.world
        link
        fedilink
        arrow-up
        10
        arrow-down
        1
        ·
        edit-2
        1 year ago

        A few members of my team were reviewing codes but lots of PRs could be merged without tests or checks passing and only about 2 people before I joined understood what cicd is, no one else believed in its importance. They thought doing otherwise would “slow down the work precess and waste time, we know what we’re doing anyway!”.

        I learned a lot from having to implement best practices and introduce tests in teams that don’t give a fuck or were never required to do it. I’m amazed at the industry standards and fully understand why job ads keep listing git as a requirement.

  • Agent641@lemmy.world
    link
    fedilink
    arrow-up
    80
    ·
    edit-2
    1 year ago

    Just print True all the time. Half the time it will be correct and the client will be happy, and the other half the time, they will open a ticket that will be marked as duplicate and closed.

  • Skyline969@lemmy.ca
    link
    fedilink
    English
    arrow-up
    68
    ·
    edit-2
    1 year ago

    Wow. Amateur hour over here. There’s a much easier way to write this.

    A case select:

    select(number){
        case 1:
            return false;
        case 2:
            return true;
    }
    

    And so on.

  • lobut@lemmy.ca
    link
    fedilink
    arrow-up
    46
    ·
    1 year ago

    Just do a while loop and subtract 2 if it’s positive or plus 2 is it’s negative until it reaches 1 or 0 and that’s how you know, easy! /s

  • enkers@sh.itjust.works
    link
    fedilink
    arrow-up
    41
    ·
    edit-2
    1 year ago

    This is your brain on python:

    def is_even (num):
         return num in [x*2 for x in range(sys.maxsize / 2)]
    
    • elauso@feddit.de
      link
      fedilink
      arrow-up
      24
      ·
      1 year ago

      Yeah, “just use modulo” - no shit, you must be some kind of master programmer

  • affiliate@lemmy.world
    link
    fedilink
    arrow-up
    40
    ·
    1 year ago

    amateurs

    def is_even(n: int):
        if n ==0return True
        elif n < 0:
            return is_even(-n)
        else:
            return not is_even(n-1)
    
    • affiliate@lemmy.world
      link
      fedilink
      arrow-up
      28
      ·
      1 year ago

      here’s a constant time solution:

      def is_even(n: int):
          import math
          return sum(math.floor(abs(math.cos(math.pi/2 * n/i))) for i in range(1, 2 ** 63)) > 0
      
      spoiler

      i can’t imagine how long it’ll take to run, my computer took over 3 minutes to compute one value when the upper bound was replaced with 230. but hey, at least it’s O(1).

      • Acters@lemmy.world
        link
        fedilink
        arrow-up
        4
        ·
        1 year ago

        Nice, how about bitwise & operator?

        // n&1 is 1, then odd, else even
        
        return (!(n & 1));
        
    • Karyoplasma@discuss.tchncs.de
      link
      fedilink
      arrow-up
      8
      arrow-down
      3
      ·
      edit-2
      1 year ago

      Don’t use recursion. Each call will need to allocate a new stack frame which leads to a slower runtime than an iterative approach in pretty much any language.

  • rollerbang@sopuli.xyz
    link
    fedilink
    arrow-up
    33
    ·
    edit-2
    1 year ago

    You have to make it easy on yourself and just use a switch with default true for evens, then handle all the odd numbers in individual cases. There, cut your workload in half.

      • Karyoplasma@discuss.tchncs.de
        link
        fedilink
        arrow-up
        1
        ·
        edit-2
        1 year ago

        You know, shortly after posting this I did think about whether it’s still working if I just pass the underflow that will happen at some point or if I have to fix something in that case (like subtract 1 after the underflow). I deemed it “too complicated” and would just issue a warning that my code is only tested on positive numbers, but I think it will still work.

  • DarkMessiah@lemmy.world
    link
    fedilink
    arrow-up
    34
    arrow-down
    7
    ·
    edit-2
    1 year ago

    Just in case anyone was looking for a decent way to do it…

    if (((number/2) - round(number/2)) == 0) return true;
    
    return false;
    

    Or whatever the rounding function is in your language of choice.

    EDIT: removed unnecessary else.

    • Acters@lemmy.world
      link
      fedilink
      arrow-up
      16
      ·
      edit-2
      1 year ago

      Every bit aside for the ones bit is even. All you have to do is get the ones bit(the far right) for it being a 1 or 0. Which is the fastest and least amount of code needed.

      use bitwise &

      // n&1 is true, then odd, or !n&1 is true for even  
      
       return (!(n & 1));  
      
    • 257m@sh.itjust.works
      link
      fedilink
      arrow-up
      5
      ·
      edit-2
      1 year ago
      number % 2 == 0
      and
      (number & 0b1) == 0
      

      Are the only sane ways to do this. No need to floor. Although If its C and you can’t modulo floats then (number/2 == floor(number/2))

        • 257m@sh.itjust.works
          link
          fedilink
          arrow-up
          1
          ·
          1 year ago

          Whats the alternative a macro? An inline function is perfectly fine for checking if a nunber is even. Compiler will probably optimize it to a single and instruction.

          • homura1650@lemmy.world
            link
            fedilink
            arrow-up
            2
            ·
            edit-2
            1 year ago

            No. The alternative is to not use a float. Testing if a float is even simply does not make sense.

            Even testing two floats for equality rarely makes sense.

            What is the correct output of isEven((.2 + .4) ×10)

            Hint: (.2 + .4) x 10 != 6

            • 257m@sh.itjust.works
              link
              fedilink
              arrow-up
              1
              ·
              edit-2
              1 year ago

              It does if it dosen’t have a decimal. If it has decimal then it automatically isn’t and the function will return false. Are you talking about cases like 0.1 + 0.2 equaling 0.3000000004 because that is just due to the nature of floats and there is nothing a function can do other than use larger floats for more accuracy.