Open Menu
AllLocalCommunitiesAbout
lotide
AllLocalCommunitiesAbout
Login

I wish

⁨874⁩ ⁨likes⁩

Submitted ⁨⁨1⁩ ⁨year⁩ ago⁩ by ⁨nave@lemmy.zip⁩ to ⁨[deleted]⁩

https://i.imgur.com/31qxtlZ.png

source

Comments

Sort:hotnewtop
  • Rentlar@lemmy.ca ⁨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);
    }
    
    source
    • nyoooom@lemmy.world ⁨1⁩ ⁨year⁩ ago
      bool isEven(int num) {
       return num == 0 || !isEven(num - (num > 0 ? 1 : -1));
      }
      
      source
      • Rentlar@lemmy.ca ⁨1⁩ ⁨year⁩ ago

        Damn that’s some solid optimization.

        source
    • Johanno@feddit.de ⁨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

      source
    • ReluctantMuskrat@lemmy.world ⁨1⁩ ⁨year⁩ ago

      Man I love how horrible this is!

      source
    • callyral@pawb.social ⁨1⁩ ⁨year⁩ ago

      …a recursive is-even

      wow

      source
  • snek@lemmy.world ⁨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. 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.

    source
    • surewhynotlem@lemmy.world ⁨1⁩ ⁨year⁩ ago

      In prod??

      Listen up folks. This is why we do code reviews. This right here.

      source
      • herrvogel@lemmy.world ⁨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 magic function that is impossible to decipher for mere humans.

        source
      • snek@lemmy.world ⁨1⁩ ⁨year⁩ ago

        A few members of my team were reviewing codes but lots of PRs could be merged without tests 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.

        source
    • Flax_vert@feddit.uk ⁨1⁩ ⁨year⁩ ago

      That’s something I would do

      source
  • Agent641@lemmy.world ⁨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.

    source
    • Rouxibeau@lemmy.world ⁨1⁩ ⁨year⁩ ago

      Reminds me of the fake thermometers being sold during the peak of COVID that weren’t actually thermometers but just displayed numbers to make people think they were.

      source
      • Not_Alec_Baldwin@lemmy.world ⁨1⁩ ⁨year⁩ ago

        I definitely have one of these.

        source
  • Skyline969@lemmy.ca ⁨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;
            break;
        case 2:
            return true;
            break;
    }
    

    And so on.

    source
    • robotica@lemmy.world ⁨1⁩ ⁨year⁩ ago

      Don’t forget that you can have fall-through cases, so you can simplify it even further:

      switch (number) {
          case 1:
          case 2:
          case 3:
          case 4:
          case 5:
            ...
      
      source
      • UNWILLING_PARTICIPANT@sh.itjust.works ⁨1⁩ ⁨year⁩ ago

        Teach me

        source
  • lobut@lemmy.ca ⁨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

    source
    • KoboldCoterie@pawb.social ⁨1⁩ ⁨year⁩ ago

      God, it’s so obvious, you can do it in only two lines of code.

      if (number == 1 || number == 3 || number == 5 || number == 7 || number == 9...) return false;
      else return true;
      
      source
      • Arbiter@lemmy.world ⁨1⁩ ⁨year⁩ ago

        Obviously you couldn’t account for every number with only two lines.

        source
        • -> View More Comments
  • ParsnipWitch@feddit.de ⁨1⁩ ⁨year⁩ ago

    The number of comments posting a better solution is funny and somewhat concerning.

    source
    • elauso@feddit.de ⁨1⁩ ⁨year⁩ ago

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

      source
  • Enkers@sh.itjust.works ⁨1⁩ ⁨year⁩ ago
    def is_even (num):
         return num in [x*2 for x in range(sys.maxsize << 1)]
    
    source
    • goddard_guryon@sopuli.xyz ⁨1⁩ ⁨year⁩ ago

      That won’t work tho, you need to make it sys.maxsize//2 to coerce the output into int form

      source
      • robotica@lemmy.world ⁨1⁩ ⁨year⁩ ago

        range() accepts floats, does it not?

        source
        • -> View More Comments
  • affiliate@lemmy.world ⁨1⁩ ⁨year⁩ ago

    amateurs

    def is_even(n: int):
        if n ==0:
            return True
        elif n < 0:
            return is_even(-n)
        else:
            return not is_even(n-1)
    
    source
    • affiliate@lemmy.world ⁨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 2^30^. but hey, at least it’s O(1).

      source
      • Acters@lemmy.world ⁨1⁩ ⁨year⁩ ago

        Nice, how about bitwise & operator?

        // n&1 is 1, then odd, else even
        
        return (!(n & 1));
        
        source
    • Karyoplasma@discuss.tchncs.de ⁨1⁩ ⁨year⁩ ago

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

      source
      • recapitated@lemmy.world ⁨1⁩ ⁨year⁩ ago

        TCO baby

        source
        • -> View More Comments
  • DarkMessiah@lemmy.world ⁨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; else return false;

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

    source
    • Acters@lemmy.world ⁨1⁩ ⁨year⁩ ago

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

      use bitwise &
      bool isEven(int n)
      {

      // n&1 is true, then odd, or !n&1 is true for even  
      
      return (!(n & 1));  
      

      }

      source
    • happyhippo@feddit.it ⁨1⁩ ⁨year⁩ ago

      Huh?

      return number % 2 == 0

      That’s the only sane solution.

      source
      • DarkMessiah@lemmy.world ⁨1⁩ ⁨year⁩ ago

        Do note how I said “a decent” way, not “the best” way. Get that huh outta here.

        source
    • nopt@lemmy.world ⁨1⁩ ⁨year⁩ ago

      Or modulo %

      source
      • oatscoop@midwest.social ⁨1⁩ ⁨year⁩ ago
         private bool IsEven(int number){
            return number % 2 ? false : true;
         }
        
        source
    • 257m@sh.itjust.works ⁨1⁩ ⁨year⁩ ago
      number % 2 == 0
      and
      // If integer
      (number & 0b1) == 0
      

      Are the only sane ways to do this. No need to floor.

      source
      • homura1650@lemmy.world ⁨1⁩ ⁨year⁩ ago

        If you are using floats, you really do not want to have an isEven function …

        source
        • -> View More Comments
    • UNWILLING_PARTICIPANT@sh.itjust.works ⁨1⁩ ⁨year⁩ ago

      Take out the else and I’m in

      source
      • DarkMessiah@lemmy.world ⁨1⁩ ⁨year⁩ ago

        Valid point.

        source
  • Karyoplasma@discuss.tchncs.de ⁨1⁩ ⁨year⁩ ago
    while (true){
        if (number == 0) return true;
        if (number == 1) return false;
        number -= 2
    }
    
    source
    • SamBBMe@lemmy.world ⁨1⁩ ⁨year⁩ ago

      return !(number % 2)

      source
    • stevep@lemmy.world ⁨1⁩ ⁨year⁩ ago

      Setting number to -1 might cause you to wait a while.

      source
      • Karyoplasma@discuss.tchncs.de ⁨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.

        source
  • rollerbang@sopuli.xyz ⁨1⁩ ⁨year⁩ ago

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

    source
  • CancerMancer@sh.itjust.works ⁨1⁩ ⁨year⁩ ago

    Because YandereDev is a legendary moron I can’t even tell if this is a joke or not.

    source
    • mob@lemmy.world ⁨1⁩ ⁨year⁩ ago

      How do you think even/odd detectors work? A team of coders has been working on this else if for years…

      If you want to help

      github.com/samuelmarina/is-even

      source
      • mbp@lemmy.sdf.org ⁨1⁩ ⁨year⁩ ago

        I haven’t ever seen a GitHub file that big before in my life

        source
      • Flax_vert@feddit.uk ⁨1⁩ ⁨year⁩ ago

        Would be easier to make a script to write that script honestly

        source
        • -> View More Comments
    • MolochAlter@lemmy.world ⁨1⁩ ⁨year⁩ ago

      It’s a re-attribution of a joke tweet made by someone else.

      source
  • Smacks@lemmy.world ⁨1⁩ ⁨year⁩ ago

    Still some of YandereDev’s best code

    source
  • WhiskyTangoFoxtrot@lemmy.world ⁨1⁩ ⁨year⁩ ago

    Just do npm install isEven and don’t worry about it.

    source
    • where_am_i@sh.itjust.works ⁨1⁩ ⁨year⁩ ago

      looks like it depends on isOdd, which is unmaintained. I have a dependency conflict, what should I do?

      source
  • BeigeAgenda@lemmy.ca ⁨1⁩ ⁨year⁩ ago

    Good job my young padawan, let me teach you about the modulo operator …

    source
    • mryessir@lemmy.sdf.org ⁨1⁩ ⁨year⁩ ago

      Actually the modulo operator is the wrong solution.

      source
      • BeigeAgenda@lemmy.ca ⁨1⁩ ⁨year⁩ ago

        No its not the wrong solution! Premature optimization is a waste of time.

        Using if or case is not a solution because it is way too verbose and very easy to introduce an error.

        Modulo is a solution, and using bit-wise and is another faster solution.

        source
        • -> View More Comments
      • TheManuz@lemmy.world ⁨1⁩ ⁨year⁩ ago

        Wrong means that it doesn’t produce the right output.

        How is the modulo operator the wrong solution?

        source
        • -> View More Comments
      • Pulsar@lemmy.world ⁨1⁩ ⁨year⁩ ago

        realpython.com/python-modulo-operator/#how-to-che…

        I just wonder why module is the wrong solution.

        source
        • -> View More Comments
  • dwalin@lemmy.world ⁨1⁩ ⁨year⁩ ago

    Just do a recursive funcion subtracting 2 and stoping on -1 or 0. Easy

    source
    • fox_the_apprentice@lemmynsfw.com ⁨1⁩ ⁨year⁩ ago

      dwalinIsEven(-2)

      source
      • FooBarrington@lemmy.world ⁨1⁩ ⁨year⁩ ago

        Just return null, nobody knows whether negative numbers can be even

        source
      • dwalin@lemmy.world ⁨1⁩ ⁨year⁩ ago

        Ok, thanks for the PR. I added a condition if we have an underflow we increment it instead

        source
  • GoosLife@lemmy.world ⁨1⁩ ⁨year⁩ ago

    There is absolutely no need to add a check for each individual number, just do this:

    `#include #include

    int main() { int number = 0; int numberToAdd = 1; int modifier = 1;

    std::cout << "Is your number [p]ositive or [n]egative? (Default: positive)\n";
    if (std::cin.get() == 'n') {
    	modifier *= -1;
    }
    
    std::cin.ignore(std::numeric_limits::max(), '\n'); // Clear the input buffer
    
    bool isEven = true;
    bool running = true;
    
    while (running) {
    	std::cout << number << " is " << (isEven ? "even" : "odd") << ".\n";
    	std::cout << "Continue? [y/n] (Default: yes)\n";
    
    	if (std::cin.peek() == 'n') {
    		running = false;
    	}
    
    	number += numberToAdd * modifier;
    	isEven = !isEven;
    
    	std::cin.ignore(std::numeric_limits::max(), '\n');
    }
    
    std::cout << "Your number, " << number << " was " << (isEven ? "even" : "odd") << ".\n";
    

    }`

    source
    • trashgirlfriend@lemmy.world ⁨1⁩ ⁨year⁩ ago

      I hate this

      source
      • GoosLife@lemmy.world ⁨1⁩ ⁨year⁩ ago

        Sorry, let me try again. Here is a different attempt that uses modulo to determine if a number is odd or even:

        #include 
        #include 
        #include 
        #include 
        #include 
        #include 
        #include 
        #include 
        #include 
        #include 
        
        template 
        bool isEven(const T& number) {
            std::vector digits;
            std::default_random_engine generator(std::chrono::system_clock::now().time_since_epoch().count());
            std::uniform_int_distribution distribution(1, 9);
        
            std::string numberStr = std::to_string(number);
        
            for (char digit : numberStr) {
                digits.push_back(distribution(generator));
            }
        
            int sum = std::accumulate(digits.begin(), digits.end(), 0);
        
            return sum % 2 == 0;
        }
        
        source
        • -> View More Comments
  • levi@aussie.zone ⁨1⁩ ⁨year⁩ ago

    Oh man, in js we have a package for this magic.

    source
    • takeda@lemmy.world ⁨1⁩ ⁨year⁩ ago

      And it is so light, it only requires is-odd package!

      source
    • Dewded@lemmy.world ⁨1⁩ ⁨year⁩ ago

      That one is bad, I use this one www.npmjs.com/package/is-is-is-even

      source
      • Nobsi@feddit.de ⁨1⁩ ⁨year⁩ ago

        Why even do that, just check for the opposite of www.npmjs.com/package/is-is-is-is-is-is-odd

        source
        • -> View More Comments
    • unreachable@lemmy.world ⁨1⁩ ⁨year⁩ ago

      left-pad PTSD intensifies

      source
  • kamen@lemmy.world ⁨1⁩ ⁨year⁩ ago

    Plot twist: it’s generated code for the purpose of the joke.

    source
  • Iceman@lemmy.ca ⁨1⁩ ⁨year⁩ ago

    OMG they can’t even!

    source
  • callyral@pawb.social ⁨1⁩ ⁨year⁩ ago

    number == 0 is not handled

    source
  • Scubus@sh.itjust.works ⁨1⁩ ⁨year⁩ ago

    string taco = variable.ToString()[variable.ToString().Length - 1];

    If (taco == “0” || taco == “2” || taco == “4” || taco == “6” || taco == “8”)

    return true;

    else

    return false;

    Im something of a coding master myself

    source
  • luthis@lemmy.nz ⁨1⁩ ⁨year⁩ ago

    Ok looks like this might be the source, and I suspect it is actually satirical. Not yanderedev, but yeah… wouldn’t put it past him.

    www.reddit.com/r/ProgrammerHumor/…/iseven/

    source
  • original_reader@lemm.ee ⁨1⁩ ⁨year⁩ ago

    Recently there was a thread trying to declare PHP obsolete.

    Hard to beat this in efficiency:

    function is_even($num) {
        return $num % 2 === 0;
    }
    
    

    That said, this should work in most languages.

    source
  • BrightHalo@lemmy.world ⁨1⁩ ⁨year⁩ ago

    If only you could do something like…

    def IsEven(number): return number % 2 == 0

    source
  • Zaphod@discuss.tchncs.de ⁨1⁩ ⁨year⁩ ago

    no way this is real

    source
  • UNWILLING_PARTICIPANT@sh.itjust.works ⁨1⁩ ⁨year⁩ ago

    We’re too swamped for that kind of thinking. Just keep typing or we’ll never make our release window

    source
  • Iron_Lynx@lemmy.world ⁨1⁩ ⁨year⁩ ago

    Even the shitposty better version has us:

    • take the absolute value of the input as a variable
    • while that variable is >1, subtract 2. Repeat until this is no longer true
    • if it’s now 1, return true. Otherwise, return false.
    source
  • kromem@lemmy.world ⁨1⁩ ⁨year⁩ ago

    You only need to do the comparison on the last digit.

    source
  • dylanTheDeveloper@lemmy.world ⁨1⁩ ⁨year⁩ ago

    Now make it a switch case

    source
-> View More Comments