I wish
I wish
I wish
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.
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.
I definitely have one of these.
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.
That's something I would do
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.
Don't forget that you can have fall-through cases, so you can simplify it even further:
switch (number) { case 1: case 3: case 5: case 7: case 9: ...
Teach me
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
The number of comments posting a better solution is funny and somewhat concerning.
Yeah, "just use modulo" - no shit, you must be some kind of master programmer
This is your brain on python:
def is_even (num): return num in [x*2 for x in range(sys.maxsize / 2)]
That won't work tho, you need to make it sys.maxsize//2 to coerce the output into int form
range()
accepts floats, does it not?
while (true){ if (number == 0) return true; if (number == 1) return false; number -= 2 }
return !(number % 2)
Setting number to -1 might cause you to wait a while.
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.
Because YandereDev is a legendary moron I can't even tell if this is a joke or not.
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
It's a re-attribution of a joke tweet made by someone else.
Just do npm install isEven
and don't worry about it.
looks like it depends on isOdd, which is unmaintained. I have a dependency conflict, what should I do?
Extract an interface and let the consumer supply the implementation.
Still some of YandereDev's best code
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.
There is absolutely no need to add a check for each individual number, just do this:
c++
#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"; }
I hate this
Sorry, let me try again. Here is a different attempt that uses modulo to determine if a number is odd or even:
c++
#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; }
Oh man, in js we have a package for this magic.
And it is so light, it only requires is-odd package!
left-pad PTSD intensifies
That one is bad, I use this one https://www.npmjs.com/package/is-is-is-even
I always forget if is even requires is odd or the other way around.
Look at the downloads though!
I would never touch js, so idk convention, but this has to be a joke right?
You’d think so but look at the number of active downloads 😅
Looking at their code, it's really just a bunch of checks to make sure the variable passed is actually an integer that it can work with, followed by the solution:
return (n % 2) === 1;
I can't think of a more efficient way to get the answer. It does seem like it'd take more time to download the package than to just write the function yourself, though.
It looks like this is a handlebars helper.
Handlebars is a temptating language.
I've never used handlebars but I'm guessing this is syntactic sugar for non-programmers. Like:
<div>{{if is-even myVariable}} it's even {{else}} it's odd {{endif}}</div>
Weekly Downloads: 293.319
Good job my young padawan, let me teach you about the modulo operator ...
Actually the modulo operator is the wrong solution.
No its not the wrong solution! Premature optimization is a waste of time.
Using if or case are not a solution because they are way too verbose and very easy to introduce an error.
Modulo is a solution, and using bit-wise and is another faster solution.
Wrong means that it doesn't produce the right output.
How is the modulo operator the wrong solution?
https://realpython.com/python-modulo-operator/#how-to-check-if-a-number-is-even-or-odd
I just wonder why module is the wrong solution.
number == 0
is not handled
Plot twist: it's generated code for the purpose of the joke.
Being yandere dev, that's likely the actual code
He wouldn't have the self-awareness to post it himself though, so this is likely a montage to put his name on it.
Insert Pikachu meme
And they're using the suggestions to efficiently generate even more code
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.
https://www.reddit.com/r/ProgrammerHumor/comments/i15h4d/iseven/
It looks like his code. Cheers for finding that.
Now make it a switch case
int is_even(int n) { int result = -1; char number[8]; //should be enough sprintf(number, "%d", n); // check the number // TODO: handle negative numbers for (char *p=number; *p; p++) { if (*p=='0' || *p=='2' || *p=='4' || *p=='6' || *p=='8') result = 1; else if (*p=='1' || *p=='3' || *p=='5' || *p=='7' || *p=='9') result = 0; else { fprintf(stderr, "Your number is wrong!\n"); exit(1); } } return result; }
This should be the accepted answer
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 similarly in most languages.
If the language you are using uses "real" integers, using a bit mask to get the least significant bit is probably a lot faster - assuming the compiler doesn't replace the operation for you, in which case it doesn't matter.
You only need to do the comparison on the last digit.
couldn't you just check the numbers in binary for the ones place
We're too swamped for that kind of thinking. Just keep typing or we'll never make our release window
Got it boss
(Quietly implements a modulo check but only for a range between the current endpoint of the if branches and the highest value I expect the product to ever encounter)
They really should mod the language to support this.
OMG they can’t even!
I did something like this in high school
I can't believe he needs that much code for this:
bool iseven(int number){
if (number % 2 == 0){
return true;
} else {
return false;
}
}
I like the example in the post better. It is more clear as to what is going on to an experienced dev like me. What's this 2 percent nonsense?
Readability over obscure hacks
I like the example in the comment better. It is more confusing as to what is going on to an experienced dev like me. iSeven is always odd tho right?
Explanation: the percent is modulus. Basically it's just divide the first number by the second and return the remainder. If you do number % 2, it will return 1 if it is odd and 0 if it is even. For example 4/2 has a remainder of 0 and therefore is even. 3/2 has a remainder of 1, and therefore is odd.
Whooosh
Whoosh
I, EvaX, humbly submit a toast to Nicholas Alexander for successfully managing to pirate WarCraft III so that he may play Defense of the Ancients.
Congratulations Nick. Enjoy your DotA!
(sips from milk goblet)
*cum chalice
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
as division is complicated and expensive depending on the size of the numbers you'd usually receive as an input, this could be the most efficient solution. Certainly could have the best worst case if we imagine some 128bit shenanigans.
Just realised i fucked up and am checking them as strings instead of chars ¯(ツ)/¯
Rip switch statements
number % 2 == 0
?
C'mon now, we don't all have quantum computers to do division
huh? that stupid
Modulus equals zero anyone?
That's the joke
but that operation is expensive
So what? It works and it's better than precompiling a list of all known even and odd numbers, and expecting a computer to iterate through a whole list every time it wants to check the value of something.
The stupid trig tables are just as problematic and it's why graphics chips use other geometric functions instead. It's just better to use a modulus.
Nah, it compiles down to an AND because of the constant 2
They call me a StackOverflow expert:
Damn that's some solid optimization.
StackoverflowException.
What do I do now?
Nvm. Got it.
Obviously we need to check each part of the division to make sure if they are even or not. /s
...a recursive is-even
wow