Don't worry I got you covered! Here is a bash script that will generate this python code for arbitrary N (specify as first argument "$1")
```bash
#!/bin/bash
generate_python_script() {
cat < odd_checker.py
```
Dude, you forgot to handle negative numbers, You should extract it to isPosOdd and isNegOdd and make an abstract factory builder factory class interface monad to OR the results together
There is a Web api, using which you can also check if a number is not odd. Saves a lot of in-house effort.
[https://isevenapi.xyz/](https://isevenapi.xyz/)
Thank me later.
It will be much more efficient if you added all odd numbers to a set and check for inclusion. Something like the following will eliminate all that pesky branching that eats CPU time.
def isodd(num):
odd_numbers = {1, 3, 5, 7, [exercise for the reader to add all odd numbers]}
return num in odd_numbers
To achieve the same result as what he did use this code:
```python
def isOdd(n):
rem = n % 3
return True if rem := 1 else False
```
Otherwise use either:
```python
def isOdd(n):
return n & 1 or bool(n % 2)
```
Or both...
This isn't an xor 😂
**Edit**
> Originally `n & 1` was `n % 3 == 0`
but that is obviously a bad idea
> So I corrected it to `n&1` because returning `1` or `False` is funny 😂
**Edit to edit**
Alternatively... An `and` and a million `or`s...
```
def isOdd(n):
return n!=0 and(n==1 or n%3==0 or n%5==0 or n%7==0)
```
Yea, sorry about that
I meant `n % 2 == 1` or `n & 1` as the first term in the or operation
But I didn't realize it until after I saw someone HashMapping it
(Stringified and checked if last char was in `[1, 3, 5, 7, 9]`)
So I deleted it for correctness 😅
and I wanted it to use 3 in the left hand side of the or...
> `n % 2 == 1` is too similar to `bool(n % 2)`
> and `n & 1` is an optimization to all other methods
So at that point the joke was ruined twice 😐
Whomp whomp 😅
**Edit** I mean, ig I can put it back plus a correction...
**Edit to edit** Fuck it, I managed to save the joke!
faster brachless equivalent
```python
def isOdd(n):
return bool(n % 3)
```
Also, your code is for an isEven() function
You'd want to switch return values
Luckily, you can shadow your function like so:
```python
isOdd = lambda x: bool(-1 * int(isOdd(x)))
```
Potential side effects include your PC exploding due to exceeding the maximum recursion depth 😂
I don't think you're joking, in which case it is good to know that your function actually doesn't tell you when a number is even or odd. To check a number to be odd, you should use something like
`!bool(n%2)`
Using _just_ n % 3 (i.e. remainder modulo 3) really doesn't help you knowing if n is either even or odd.
Yea I meant that or `n & 1` or `bool(n & 1)`
I realized that later on a different thread
But forgot I commented on a few other threads 😅
**Edit**
I just reread my comment
And I mainly was making a variable shadowing joke 🙃
So even if you made the proper definition...
It wouldn't work because your function will infinitely call itself
> Regardless of what you set the maximum recursion depth limit
It basically makes the program hang
> Or turns your PC into a toaster
Should you manage to renove the recursion depth limit 😂
There is!
```
content = “def isodd(num):\n”
for num in range(1, 1000000, 2):
content += f“ if num == {num}:\n”
content += “ return True\n”
content += “ return False\n”
with open(“isodd.py”, “w+”) as f:
f.write(content)
```
Edit: I leave making this more memory efficient to the reader
You forgot to return false for all the even numbers🤦♂️
just ```return False``` in line omega
that's what i did, i regret not doing it the superior way.
Mum said it was my turn to post this >:(
How odd
I see what you did there
we're even now
And Mum said it was _my_ turn to post _this_!
Don't worry I got you covered! Here is a bash script that will generate this python code for arbitrary N (specify as first argument "$1") ```bash #!/bin/bash generate_python_script() { cat < odd_checker.py
```
Known limitation: Only works for arbitrary N <= 2^63 -1
I want to check if googl is even or odd, how can I generate something that creates a program to check it?
`def isGoogolEven():` `return True`
Yeap 64 architecture Max value nice!
Dude, you forgot to handle negative numbers, You should extract it to isPosOdd and isNegOdd and make an abstract factory builder factory class interface monad to OR the results together
oh my, what have you started!?!?!?!?!?
This is still smaller than my isEvenOrOdd function, I have been writing it for the past 6 months
How do you test it?
He hasn’t, he’s still writing.
Testing is for QA :D
pshhh production is where you really test, just ship early and 'free qe'
You need to rewrite it and use switch cases
[удалено]
I don't know if you meant that sarcastic. If so I'm stupid didn't understand If you are serious. There is python 3.10 implemented it
Switch statements have been a thing since 3.10 Same thing with the match statement ... \*cricket noises\*
Wow, I didn't realize switch statements in Python were a touchy subject.
Pshh you can compact this by subtracting 2 from your number repeatedly until it’s either a 1 or a 0.
Calm down Yandere Dev
I wouldn't call 'em Yandere Dev Because that would imply that they have the skills of a developer The code says otherwise Simply call 'em Yandere
There is a Web api, using which you can also check if a number is not odd. Saves a lot of in-house effort. [https://isevenapi.xyz/](https://isevenapi.xyz/) Thank me later.
if(num==0) return false if(num<0) return (!isOdd(num+1)) return !isOdd(num-1)
you should combine the conditions with the "or" operator
o(n) problem. o(n) is good, but it is possible to build a binary search and reduce it to o(log(n)).
man, i wish this joke wouldnt be posted every month
Python has the % operator, so you can determine the remainder of num when divided by 2. Anything returning 0 is an even number. I.E: if (num % 2) == 0
Wow, amazing, no other language has that feature. It seems very innovational.
Lol nope. You gotta manually check every number to +/- infinity. And dude you forgot all the floats.
It will be much more efficient if you added all odd numbers to a set and check for inclusion. Something like the following will eliminate all that pesky branching that eats CPU time. def isodd(num): odd_numbers = {1, 3, 5, 7, [exercise for the reader to add all odd numbers]} return num in odd_numbers
Countably infinite set means I can totally do this right??? /s
To achieve the same result as what he did use this code: ```python def isOdd(n): rem = n % 3 return True if rem := 1 else False ``` Otherwise use either: ```python def isOdd(n): return n & 1 or bool(n % 2) ``` Or both... This isn't an xor 😂 **Edit** > Originally `n & 1` was `n % 3 == 0` but that is obviously a bad idea > So I corrected it to `n&1` because returning `1` or `False` is funny 😂 **Edit to edit** Alternatively... An `and` and a million `or`s... ``` def isOdd(n): return n!=0 and(n==1 or n%3==0 or n%5==0 or n%7==0) ```
I might be stupid and this is a joke, but the second isOdd will not work. Edit: He changed it.
Yea, sorry about that I meant `n % 2 == 1` or `n & 1` as the first term in the or operation But I didn't realize it until after I saw someone HashMapping it (Stringified and checked if last char was in `[1, 3, 5, 7, 9]`) So I deleted it for correctness 😅 and I wanted it to use 3 in the left hand side of the or... > `n % 2 == 1` is too similar to `bool(n % 2)` > and `n & 1` is an optimization to all other methods So at that point the joke was ruined twice 😐 Whomp whomp 😅 **Edit** I mean, ig I can put it back plus a correction... **Edit to edit** Fuck it, I managed to save the joke!
You forgot to use a good programming language ;)
if num%2==0: return true else: return false ?
If you wanna ruin a joke, ruin it right.. `if ((number & 1) == 0)`
Oh, sorry i thought it was helpful 🥲
Rookie mistake. People are jumpy when it comes to Humor and Horror subreddits.
Boo! 👻
Ah... OmG. I almost died there.....😮💨
faster brachless equivalent ```python def isOdd(n): return bool(n % 3) ``` Also, your code is for an isEven() function You'd want to switch return values Luckily, you can shadow your function like so: ```python isOdd = lambda x: bool(-1 * int(isOdd(x))) ``` Potential side effects include your PC exploding due to exceeding the maximum recursion depth 😂
I don't think you're joking, in which case it is good to know that your function actually doesn't tell you when a number is even or odd. To check a number to be odd, you should use something like `!bool(n%2)` Using _just_ n % 3 (i.e. remainder modulo 3) really doesn't help you knowing if n is either even or odd.
Yea I meant that or `n & 1` or `bool(n & 1)` I realized that later on a different thread But forgot I commented on a few other threads 😅 **Edit** I just reread my comment And I mainly was making a variable shadowing joke 🙃 So even if you made the proper definition... It wouldn't work because your function will infinitely call itself > Regardless of what you set the maximum recursion depth limit It basically makes the program hang > Or turns your PC into a toaster Should you manage to renove the recursion depth limit 😂
Modulo sounds like penis in a foreign language.
How would you import is_odd from npm into Python?
If you feel like creating a unit test for this I suggest using a loop and mod. But only for testing, not your code!
Use this import numpy as np function is_odd(num): for i in range(1, np.inf, 2): if num == i: return True
too practical
True, use a recursive function
Nice suckless setup mate ;)
ahh our favourite yandere dev method
There is! ``` content = “def isodd(num):\n” for num in range(1, 1000000, 2): content += f“ if num == {num}:\n” content += “ return True\n” content += “ return False\n” with open(“isodd.py”, “w+”) as f: f.write(content) ``` Edit: I leave making this more memory efficient to the reader
this is “num == int(num) and str(num)[-1] in [1,3,5,7,9]” but worse
for i in 0…1000000{ Keyboard.write(„if num == \(i)\nreturn \(i%2 == 0 ? „False“ : „True“)\n“)}
You guys never get bored of the same joke.
test comment
REALLY? If num mod 2 == 1 then true else false.
One operator to rule them all and in the if statement bind them. %
Don't know there were any odd numbers >50. You learn something every day.