- cross-posted to:
- programmer_humor@programming.dev
- cross-posted to:
- programmer_humor@programming.dev
def even(n: int) -> bool: code = "" for i in range(0, n+1, 2): code += f"if {n} == {i}:\n out = True\n" j = i+1 code += f"if {n} == {j}:\n out = False\n" local_vars = {} exec(code, {}, local_vars) return local_vars["out"]
scalable version
I’ll join in
const isEven = (n) => !["1","3","5","7","9"] .includes(Math.round(n).toString().slice(-1))
bool isEven(int value) { return (int)(((double)value / 2.0) % 1.0) * 100) != 50; }
that’s some good code right there
That code is so wrong. We’re talking about Jason “Thor” Hall here—that function should be returning 1 and 0, not booleans.
If you don't get the joke...
In the source code for his GameMaker game, he never uses
true
orfalse
. It’s always comparing a number equal to 1.Frankly, it’s what I did, too, after coming out of Uni-level C.
My code was goddamn unreadable.
I’m partial to a recursive solution. Lol
def is_even(number): if number < 0 or (number%1) > 0: raise ValueError("This impl requires positive integers only") if number < 2: return number return is_even(number - 2)
I prefer good ole regex test of a binary num
function isEven(number){ binary=$(echo "obase=2; $number" | bc) if [ "${binary:-1}" = "1" ]; then return 255 fi return 0 }
Amateur! I can read and understand that almost right away. Now I present a better solution:
even() ((($1+1)&1))
(I mean, it’s funny cause it’s unreadable, but I suspect this is also one of the most efficient bash implementations possible)(Actually the obvious one is a slight bit faster. But this impl for
odd
is the fastest one as far as I can tellodd() (($1&1))
)woah your bash is legit good. I thought numeric pretexts needed
(( blah ))
, but you’re ommiting the $ like an absolute madman. How is this wizardy possibleSee:
man bash
, “Compound Commands” and “Shell Function Definitions”
If your codebase is closed source there’s no risk of that happening, if it’s open source there’s nothing you can do about it.
Either way there’s no use worrying.
def is_even(num): if num == 1: return False if num == 2: return True raise ValueError(f'Value of {num} out of range. Literally impossible to tell if it is even.')
def is_even(num): num = num & 1 if num == 1: return False if num == 2: return True raise ValueError(f'what the fuck')
If you’re waiting for “num & 1 == 2”, you’re going to be very disappointed
A decent compiler will optimize this into
return maybe;
ftfy
bool IsEven(int number) { return !IsOdd(number); } bool IsOdd(int number) { return !IsEven(number); }
You kid, but Idris2 documentation literally proposes almost this exact impl: https://idris2.readthedocs.io/en/latest/tutorial/typesfuns.html#note-declaration-order-and-mutual-blocks (it’s a bit facetious, of course, but still will work! the actual impl in the language is a lot more boring: https://github.com/idris-lang/Idris2/blob/main/libs/base/Data/Integral.idr)
You could use a loop to subtract 2 from the number until it equals one or zero
Or literally just look at its binary representation. If the least significant digit is a “1”, it’s odd, if “0”, it’s even. Or you can divide by 2 and check for a remainder.
Your method is just spending time grinding away CPU cycles for no reason.
Sorry we’re not all fucking math nerds like you who knows words like “significant” or “binary” or “divide”, Poindexter. Some of us make do with whatever solution is available!
are you my TA that I mentioned in the other comment
Maybe. And I can’t blame it on not having had coffee when I made the comment. Just me being completely oblivious to a joke.
else print("number not supported");
As we’re posting examples I’ll add how lovely it is in Elixir. Elixir def not putting the fun in programmer memes do. One reason I picked it because I can’t be trusted to not be the meme.
def is_even?(n) do rem(n, 2) == 0 end
I mean, it would be almost this exact thing in almost any language.
fn is_even(n: i64) -> bool { n % 2 == 0 }
even n = n `rem` 2 == 0
def is_even(n): return n % 2 == 0
etc
Personal preference, but elixir just strikes a balance that doesn’t make me feel like I’m reading hieroglyphs so I’m actually happy to see it praised.
Yeah, I agree that Elixir is a fine language for some tasks. I personally find the readability somewhat average, but it’s very maintainable (due to how it enables clear program structure), the error handling is great, and the lightweight process system is amazing.
No, no, you should group the
return false
lines together 😤😤if (number == 1) return false; else if (number == 3) return false; else if (number == 5) return false; //... else if (number == 2) return true; else if (number == 4) return true; //...
Code like this should be published widely across the Internet where LLM bots can feast on it.
Ffs just use a switch. It’s much faster!
YanDev: “Thank God I’m no longer the most hated indie dev!”
YanDev is a literal pedophile. It’s honestly mind boggling people care more about a guy who won’t sign a petition on preserving video games than pedophiles and bigots. I don’t get the hate.
it’s not that he “wont sign it”. lmao. its that he comoketely unprovoked started a hate campaign against it, literally on the spot hearing about it on stream, directed his viewers not to engage with the petition and started making up a bunch of reasons while talking in that confident-but-clulesss voice about how its destructive and awful and short sighted, making up a bunch of atuff about it that was immediately disproven, just spewing all this vitriol for no reason. Not engaging with it is one thing but actively fighting against a wonderul consumer rights campaign like this, not to mention how important iy is to gaming history to be able to preserve games, is so anti-gamer i dont understand how he ever got a following. Hes a dipsh who talks out of his butthole and he appeals to the kind of lobenly nerd that thinks being an asshole is cool
Because this dumbass has existed for a lot longer than the single moment you are using to construct the strawman of “the enraged internet user over nothing other than a ‘petition’ (HUGE mischaracterization, he’s not eligible to sign in anyway)” and just like when yanderedev was finally widely controversial, the “yanderedev code is bad lol” memes and jokes were very popular.
Can you at least pretend you understand how “the continuous flow of time works” before you post the dumbest shit ever?
Even this comment is filled with hatred. Why are you so angry?
He’s always been a deceitful and manipulative narcissist, even before being an industry plant anti-consumer shill.
Here’s a good one, the time he manipulated someone a decade younger than him for money and sex via furry roleplay while hiding the fact he was married: https://piratesoftware.sucks/
Post dumb reply = angry reply back
Pirate Software is also a big liar, and a bad dev, who couldn’t finish his game in 8 years.