r/HelpMeFindThis • u/Timely_Succotash8754 • 3d ago
this image
i don't know how to code in python so i just bullshitted the code based on what i remember but the joke was individually returning "true" or "false" for every number to check if it was even
38
u/SovaSperyshkom 3d ago
13
1
1
25
u/Head_Holiday9519 3d ago
As someone who just finished a small ish project in python, this is absolutely horrifying
16
u/MagicMushShrooms 3d ago
can someone drop the knowledge of how it's actually done pls?
26
u/Wooden-Paper-872 3d ago
Im pretty sure you allow the user to input a number, and then you get the modulo 2 of that number, aka getting its remainder when divided by 2.
If the result is 0, aka no remainder, its even. But if you get 1, its odd
5
u/MagicMushShrooms 3d ago
surely there's an easier method than modulo in coding?
14
u/Wooden-Paper-872 3d ago
well thats kind of the easiest way already
Like compared to the long code you see in the post image and the original, it would be something like
if(number%2==0) return even
else return odd
3
u/MagicMushShrooms 3d ago
thanks man. it makes sense. i have used modulo but in 3d geo nodes in blender and made my own work around in maya as well. i just thought coding had an even easier way to deal with it but it makes sense that this is the bar
3
u/forestwolf42 3d ago
If you're checking for even/odd a lot you could even make a function.
func iseven(int) -> bool: If int % 2 = 0 return true Else return false
Then you can just ask iseven anywhere the function is declared.
1
u/Tide__Hunter 2d ago
You can get it even shorter with just "return number%2==0" in the case of an "is_even" function.
1
u/gameryesyt 3d ago
If (x % 2 == 1); Y=true; Else if (x % 2 == 0); Y=false;
It doesn't get much simpler than that
2
u/BagelMakesDev 3d ago edited 2d ago
This is false. The second modulo is redundant and a waste of CPU cycles. this can be simplified to
if (x % 2 == 0) { return EVEN; } else { return ODD; }This uses less CPU cycles.
1
u/Vivid-Key-9032 3d ago
i understand the redundant if statement but your initial claim of "other" remainders i dont get that. there can only be a remainder of either 0 or 1 , there are no other remainders possible when dividing a number by 2 or am i missing something?
1
1
1
1
u/HostOfTheCamp2 3d ago
If its less than 1 add 2 until its equal to 1 or 2 if its greater than 2 then subtract until its equal to 1 or 2
1
1
u/0tter501 3d ago
you can do bitwise AND 1 with any number, returns the exact same result just more likely to be faster
i could say with zero doubt that every modern language, compiler, hell, every language compiler with any optimizations will automatically convert a % 2 into a AND 1
1
1
u/IcyEffort2841 3d ago
This comment pissed me off. Have a horrible day.
1
u/MagicMushShrooms 3d ago
didnt mean to piss you off. i used modulo often in blender (3d software) for shading. just thought that behind it, there was an easier code/way to do this. i was wrong. this is the bar
1
u/jpk64- 3d ago
Basically just:
bool isEven(int value) { return !(value & 1); }
The rightmost bit (so the 1 bit) is only set dor odd numbers. So invert the result, because if it is not odd, then it is even.
Most compilers are smart enough to optimize your code for you, though, and turn the following, more readable approach into the function above for you:
bool isEven(int value) { return value % 2 == 0; }
Do value mod 2. If it is 0 (so dividing value by 2 does not leave a rest), then value is even.
1
u/scallywag_software 2d ago
return (number & 1) == 0; // bitwise and. If the bottom-most bit is 0, it's even.
1
u/I_Exist_Let_Me_Alone 2d ago
In python you can divide the number by 2, check if it's an int(whole number) or float (not whole number)
1
u/L30N1337 1d ago
that's... an interesting solution.
the default solution is modulus (since that's literally what it's for)
and I guess you could compare the last bit
1
u/I_Exist_Let_Me_Alone 1d ago
... I forgot about modulus. I mean sure, it's been a year since I've actually used the thing but damn.
1
1
36
u/mrbeck1 3d ago
For starters, just declare it false and then list the even numbers which change it to true. Cut 50% of your code for you. You’re welcome.
4
u/behemothtyphoon 3d ago
if someone were to do it like that thatd be like barely 1% less embarrassing icl
1
u/GreatValueBradPitt 3d ago
I’d say equally embarrassing if not more so. Imagine your galaxy-brain optimization of determining whether or not something is even or odd being “just manually test half of an infinite number sequence”.
1
u/TheDarkNerd 2d ago
You can easily do this with a "while" loop that starts with a variable of the value you want to return as "true", then for each iteration of the loop, increments a variable counting which iteration of the loop you're on, checks if the variable is equal to the parameter value you're testing against, then goes onto the next iteration of the loop if it isn't.
Doing it this way keeps you from having to list every possible value.
3
u/According-Object-521 3d ago
If number % 2 == 0 {return true} ; return false
1
u/NaboriRuta 3d ago
return num % 2 == 0;
1
1
u/TheDarkNerd 2d ago
return !(num%2)
(No idea if this works, since I'm supposing it would depend on if the ! would negate the num%2 as a bool)
1
u/EmeraldMan25 2d ago
Yeah it would work. Bool is really just 0 and 1, so doing num%2 and then negating the output would give the right value
1
u/According-Object-521 2d ago
I don’t think so modulus operator returns an Int which you can’t negate
1
u/EmeraldMan25 2d ago
Actually, you can! In every language I've used, anyway. Bits are bits. Boolean is essentially just the negation of the zero flag in most implementations. That means anything can be a boolean. The key is that any bit string that equals 0 is false, anything else is true
1
u/According-Object-521 2d ago
Which languages does that work in? I’m pretty sure you usually can’t negate an Int
1
u/L30N1337 1d ago
depends on the language.
C# is gonna scream at you.
C would probably allow it (I remember "while (1)" from Arduino classes)
2
1
1
u/Neki_Chan_1 1d ago
Beyond the meme, what was the easiest way to do it?
1
u/Ineedlasagnajon 1d ago
I am only somewhat acquainted with Python, so I came up with this
def evenchecker(x)
-----return ".0" in str(x/2)
That's three steps, but I've been shown faster ways by others
def evenchecker(x)
-----return x % 2 == 0
Only two steps, so it's more optimal
1
1
u/Creative-Leg2607 1d ago
I wouldnt even define it as a function. Hell in some languages you could cast to boolean and just say ! bool(x%2), but at that point its getting a bit silly.
1
1
1
u/NightDragoOfficial 17h ago
Python with type hints
def is_even(num: int) -> bool: return (num % 2) == 0
1
0
u/tottalynotpineaple12 3d ago
The original wasn't even python code OP, why mention that you don't know python when you don't even know the programming language in question bruh
1
1
u/astolfoballsHD 3d ago
I was also just looking to see what language that was since it definitely isnt python lol. I guess C# makes the most sense, otherwise Java
1
u/tottalynotpineaple12 3d ago
Yeah my first thought was Java, then C#, their syntax is quite similar
1
1



323
u/Totaly__a_human 3d ago