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); }
bool isEven(int num) { return num == 0 || !isEven(num - (num > 0 ? 1 : -1)); }
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
Man I love how horrible this is!
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.
In prod??
Listen up folks. This is why we do code reviews. This right here.
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.
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.
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: ...
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
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;
Obviously you couldn’t account for every number with only two lines.
Only if your line is not long enough…
Maybe you couldn’t
Eventually, it would wrap onto a second line, wouldn’t it?
When you run out of space, you just add a monitor to the right side. Honestly, it’s like you guys aren’t real developers.
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?
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.
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.
It’s only the wrong solution if you’re writing something where every operation needs to be accounted for. Modulo is a great, easy, readable method otherwise.
Not too certain on C++, but I think this would be the cleanest implementation that still somewhat optimizes itself:
private bool IsEven(int number){ return !(number % 2) }
You call it premature optimization. I call it obvious.
You use a flat head as a Phillip’s.
You can call it whatever you like, the fact of the matter remains - code readibility is more important than most optimizations you can ever hope to make.
Bad programmers optimize everything and produce code that is not understandabe and 0.001% “faster”
Okay wow no need to get personal.
I call it making assumptions that may be incorrect, and do you know if the compiler will do the optimization anyway in this case?
What statement do you flag as assumption? Yes, I do. The modulo operator is only a subset of bit masks. It is more explicit to write:
if ( (variable &EVEN_MASK) == 0) …
To act upon even numbers then:
if ( (variable %2) == 0) …
How would you name the 2 in the above statement for more expressive power?
EVEN_MODULO_OP ? That may throw more people off imo.
You shouldn’t rename 2 at all. “Even” has a commonly understood meaning that is instantly recognizable from
(variable %2) == 0
. The bitmask is an overgeneralization.Psh screw those people, they need to see my massive coder wang
if(!i&1){ ... }
I give up, I was wrong to even think about the modulo operator, you are clearly the master programmer. 🥇
This reminds me of a discussion about the ternary operator
? :
, some people think its the one true way of writing code because its just so clear what it does. And I say please use it sparingly because if you start doing nested ternary operators its very hard to unpack what your code does, and I prefer readability over compact code, especially with today’s compilers.I feel you said about JavaScript as a whole.
Wrong means that it doesn’t produce the right output.
How is the modulo operator the wrong solution?
You are right. I have been biased.
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.
Not neccessarily wrong, but you could also check the first bit. If it’s 1 the number is uneven, if it’s 0 the number is even. That seems to be more efficient.
That’s what I was thinking too… Although, I wouldn’t be surprised if most languages convert modulo 2 to this automatically.
Modern compilers and interpreters are smart enough to figure out what you’re trying to do and automatically do that for you.
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
Why even do that, just check if this outputs false https://www.npmjs.com/package/is-is-is-is-is-is-odd
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.
Plot twist: it’s generated code for the purpose of the joke.
Being yandere dev, that’s likely the actual code
Insert Pikachu meme
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/
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)
no way this is real
From what I’ve heard about yanderedev… it’s possible this is actually real.
It is and it has become a meme since then. Just search for Yandere simulator / Yanderedev.
I’ve heard about his notoriously bad code, but I didn’t think it was to this extent…
It gets worse. I am not kidding.
Isn’t there like one script with an Update function that runs 900 times every frame thats like 18 if-statements deep?
You only need to do the comparison on the last digit.
couldn’t you just check the numbers in binary for the ones place
Yeah that should be last bit, not last digit lol.
For another convoluted impl you could do some kind of string format print into a string. Then do your if/else comparing that string containing the last digit. Maybe create a hash set collection of all single digit even strings, then you’ve got O(1) performance using a contains() method.
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.
Except maybe in C++ where the makers must have found some bit-fucking method that saves 0.02ms
for an unsigned int, do
(myNum & 1) == 0;
I haven’t done binary in years so ill just trust you this works
Checks if least significant bit is equal to zero
Binary reminder
1 - 1
2 - 10
3 - 11
4 - 100
5 - 101
I think you start to see a pattern
Ah yeah, I do remember my basics of hardware classes.
I’ll do you one better:
~n & 1
. If we’re doing bitwise ops we might as well go all in
Binary is indeed the easiest and most straightforward way to see number parity. You only need to check one bit.
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.
There is a simpler way: all the "else if"s can be replaced with simple "if"s
You mean make it even worse.(performance wise)? Interesting.
Please elaborate. When and why would his suggestion be worse?
It will check all of the if statements instead of stopping at the first match.
Each clause already returns
Yeah gotta store the return value in a variable!