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)); }
Damn that’s some solid optimization.
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!
…a recursive is-even
wow
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.
In prod??
Listen up folks. This is why we do code reviews. This right here.
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.
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.
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
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.
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.
Only if your line is not long enough…
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?
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)
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).
Nice, how about bitwise & operator?
// n&1 is 1, then odd, else even return (!(n & 1));
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.
TCO baby
Since when did Python have tail call optimization?
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.
deleted by creator
Just do
npm install isEven
and don’t worry about it.Removed by mod
Extract an interface and let the consumer supply the implementation.
Still some of YandereDev’s best code
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
I haven’t ever seen a GitHub file that big before in my life
Would be easier to make a script to write that script honestly
imagine generating this at runtime
It’s a re-attribution of a joke tweet made by someone else.
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:
#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:
#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; }
i think you forgot an include there also a goto statement would work better
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
Oh fuck, gonna refactor asap!
I always forget if is even requires is odd or the other way around.
I would never touch js, so idk convention, but this has to be a joke right?
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.
Ohh. JS needs you to check the variable during runtime??? That’s… something. I guess that’s what you get for using dynamic typing everywhere. I still bet it’d be faster to do the function by hand 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