You could do this in one line…
By removing all the linebreaks.
deleted by creator
i think it should one giant ternary expression composition
I love this thread 🫠
Of course there’s an easier way. Just integrate the state of the art API dedicated for this exact problem. https://isevenapi.xyz/
This is confusing. I’m already using the iSeven API to determine if a number is 7. I’m getting a namespace collision error when I try to load this new API. Bug report filed.
Back when I was learning programming a lot of lessons would make you do something like this, and then show you the real way to do it in the next lesson. My reaction was always “why didn’t you lead with this?”.
You must see the pain before you confront it.
Because the point of the lesson is to demonstrate that you can solve the same problem multiple ways where some paths are more efficient than others.
Bad programmers are the ones that find the first solution and implement it no matter how inefficient it is.
Good programmers spend time on figuring out the solution with the least amount broken or inefficient code. You don’t learn this by jumping straight to the best answer every time.
I know how to fix this!
bool IsEven(int number) { bool even = true; for (int i = 0; i < number; ++i) { if (even == true) { even = false; } else if (even == false) { even = true; } else { throw RuntimeException("Could not determine whether even is true or false."); } } if (even == true) { return even ? true : false; } else if (even == false) { return (!even) ? false : true; } else { throw RuntimeException("Could not determine whether even is true or false."); } }
Have you tried seeing if the recursive approach runs faster?
I know an even better way. We can make it run in O(1) by using a lookup table. We only need to store 2^64 booleans in an array first.
My solution in perl back in the day when I was a teenage hobbyist who didn’t know about the modulus operator: Divide by 2 and use regex to check for a decimal point.
if ($num / 2 =~ /\./) { return “odd” }
else { return “even” }Divide by 2 and check for a decimal point.
I mean, it ain’t wrong.
You know, I was going to let this slide under the notion that we’re just ignoring the limited precision of floating point numbers… But then I thought about it and it’s probably not right even if you were computing with real numbers! The decimal representation of real numbers isn’t unique, so this could tell me that “2 = 1.9999…” is odd. Maybe your string coercion is guaranteed to return the finite decimal representation, but I think that would be undecidable.
Ackchyually-- IEEE 754 guarantees any integer with absolute value less than 2^24 to be exactly representable as a single precision float. So, the “divide by 2, check for decimals” should be safe as long as the origin of the number being checked is somewhat reasonable.
Of course, but it’s somewhat nasty when all of a sudden
is_even
doesn’t do what you expect :).
The decimal representation of real numbers isn’t unique, so this could tell me that “2 = 1.9999…” is odd.
I don’t think your belief holds water. By definition an even number, once divided by 2, maps to an integer. In binary representations, this is equivalent to a right shift. You do not get a rounding error or decimal parts.
But this is nitpicking a tongue-in-cheek comment.
“1.99999…” is an integer, though! If you’re computing with arbitrary real numbers and serializing it to a string, how do you know to print “2” instead of “1.9999…”? This shouldn’t be decidable, naively if you have a program that prints “1.” and then repeatedly runs a step of an arbitrary Turing machine and then prints “9” if it did not terminate and stops printing otherwise, determining if the number being printed would be equal to 2 would solve the halting problem.
Arbitrary precision real numbers are not represented by finite binary integers. Also a right shift on a normal binary integer cannot tell you if the number is even. A right shift is only division by 2 on even numbers, otherwise it’s division by 2 rounded down to the nearest integer. But if you have a binary integer and you want to know if it’s even you can just check the least significant bit.
Programming humor on reddit used to be excellent bits like this but then it devolved into new learners jumping straight to the irony they didn’t understand and flooded the sub with nonsense.
I miss these bits.
btw it does get easier
import math def is_even(num): if num in [i for i in range(1000) if float(i)/2.0 == math.floor(float(i)/2.0)]: print("true") else: print("false")
Obviously one would need to increase the range for bigger numbers but this code is optimized.
for i in itertools.count(): ...
will count to infinity.Better make it into a dictionary so it’s O(1) complexity instead of O(n) while you’re at it.
def is_even(n): match n: case 1: return False case 0: return True # fix No1 case n < 0: return is_even(-1*n) case _: return is_even(n-2)
Python added match/case?! Bunch of mypy issues have been closed too. Maybe its time to dust off some old projects.
It was added in 3.10 and is surprisingly complete. The tutorial pep is a good starting point to see what it can accomplish
Well… At least it’s tail recursive.
What if the input is negative
Fixed!
modulo
pseudocode:
if number % 2 == 0 return "number is even" (is_num_even = 1 or true) else return "number is odd" (is_num_even = 0 or false)
plus you’d want an input validation beforehand
who needs modulo when you can get less characters out of
while (number > 1) { number -= 2; } return number;
very efficient
edit: or theres the trusty iseven api
here is somewhat less:
return (number % 2) == 0;
return !(number & 1);
This is the way. Modulo takes too long to compute, bitwise compare should be a lot faster.
return !(number & 0x1);
oh shit yo
this comment chain is pretty awesome, I learned a lot from this thanks!
just check the last bit jesus christ, what is it with these expensive modulo operations?!
return !(n&1);
are the negative numbers all even?
Yes
🤦
#You are an input. You have value! You matter! if number % 2 == 0 return "number is even" (is_num_even = 1 or true) else return "number is odd" (is_num_even = 0 or false)
Am I doing it right? /S.
Don’t put nbsps in code blocks, they show up literally.
Name doesn’t check out.
This code is terrible. If you input 10.66 it returns "number is odd
It should be:
if number % 2 == 0 return "number is even" (is_num_even = 1 or true) else return "number is not even" (is_num_even = 0 or false)
are u a wizard?
John carmak posting
This could be optimized by using a recursive function.
This could be made more servicavle by using a switch case
what’s the big O of an even number
deleted by creator
I would replace each if/else with a while.
deleted by creator
God, this must go on for, like, twenty or so lines.
…btw a switch statement is better in this case(get it?)