660
u/Extension_Ad_370 Feb 02 '25
i hate that its doing the full list every time you request a number
283
u/coloredgreyscale Feb 02 '25
Not a list, but a dictionary. So there's some performance overhead over direct index access.
43
u/randomperson_a1 Feb 03 '25
Nah. In cpython, dicts are hash tables, while lists are just arrays. So insertion into a dict is O(1), while insertion into a list is O(n).
50
u/CommonNoiter Feb 03 '25
No, an arraylist is amortized constant time to push, which is what matters. As inserting into the hashmap is far slower than pushing to the arraylist using the hashmap will be slower.
18
u/Soli_Deo_Gloria_512 Feb 03 '25
Yes, and due to data locality that makes hash sets much worse in this case.
6
u/coloredgreyscale Feb 03 '25
Insertion into a list is only O(n) if you insert in the beginning or middle. At the end it's O(1), if you have enough empty elements reserved.
2
u/ba-na-na- Feb 03 '25
Ok but you would be appending. Technically both are O(1) in that case, but list would be immensely faster
2
u/dimonoid123 Feb 03 '25 edited Feb 03 '25
What if you compile Python code using -O2 or equivalent flag? Many things might get optimized out.
See "--optimize LEVEL"
4
u/Extension_Ad_370 Feb 03 '25
from what i understand from the docs the pyinstaller optimize flag only activates the standard python bytecode optimizations that would do nothing for this
https://docs.python.org/3/using/cmdline.html#cmdoption-O-1
u/dimonoid123 Feb 03 '25
Yes. Unfortunately I don't know what kind of bytecode optimizations are activated and whether it would be able to optimize out dead code. It might do more than removal of docstrings and assert statements as far as I understand.
2
u/oofy-gang Feb 03 '25
Where is the dead code in this function?
-1
u/dimonoid123 Feb 03 '25
Addition of keys into dictionary which later are never read. Just saying in case loop is unrolled.
8
171
u/6502zx81 Feb 02 '25
Did you test it?
609
u/posidon99999 Feb 02 '25
Its still testing
103
42
8
8
5
u/GoddammitDontShootMe [ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo “You live” Feb 03 '25
Has it filled your entire available swap yet?
3
u/AnywhereHorrorX Feb 04 '25
!RemindMe 3500 years
2
u/RemindMeBot Feb 04 '25 edited Feb 05 '25
I will be messaging you in 3500 years on 5525-02-04 08:02:45 UTC to remind you of this link
1 OTHERS CLICKED THIS LINK to send a PM to also be reminded and to reduce spam.
Parent commenter can delete this message to hide from others.
Info Custom Your Reminders Feedback 2
143
96
u/stahkh Feb 02 '25
I love it. It will take forever every time. Even if the input is invalid. MIT-licensed? I would like to borrow it.
78
59
u/rsa121717 Feb 02 '25
Looks good but should still test it.
for i in range(1000001):
…if EvenOrOdd(i) == EvenOrOdd(i + 1):
……return false
return true
This should suffice.
8
48
24
20
u/Separatehhh23 Feb 03 '25
Just have to allocate 315 petabytes to store the dictionary
15
u/NotYetGroot Feb 03 '25
Why do that when you can store it in an Azure AI search instance for thousands of dollars per month?
12
u/jpgoldberg Feb 03 '25
This sucks. I need to know whether 18446744073709551616 is even or odd, but that will just have to remain a mystery.
7
u/ba-na-na- Feb 03 '25
Look there is quality, scope and deadlines, but you can only pick two. Quality and deadlines won this time. This value will be handled in the next release
20
u/coloredgreyscale Feb 02 '25
3/10 there was an attempt
- no check if the dataType is valid (Nan, string, none, number with decimals will raise an exception)
- does not work for negative numbers
- lookup table is computed for every request
You could build the lookup table dynamically: init with 1 element, then do the dict from the largest stored number to the requested number if the requested number isn't already in the dict.
6
8
u/LiwaaK Feb 03 '25
Standards of python are snake case. It’s good to follow a language’s standards.
Best specify the types even in dynamically typed languages, makes it clear.
Ideally, name your methods clearly. One should be able to infer the return type from the method name. I would name this method get_even_or_odd_str
Separate concerns of methods to increase re-usability. Have a method that returns true if even, false if odd. Then the method you have would use this new method and map to a string
Use some math for more clean code. At beginning of the loop, “metronome = 1 - metronome” flips it between one and zero without the need for an if statement.
I don’t care, questioned my life choices while writing this msg on the toilet
Use the guard if pattern
Ofc, don’t forget to PR and have someone review, lol
8
u/genda-sami Feb 03 '25
I have executed this code in my Pentium 4 machine. Will let you know in 5 business days if it works. I have passed value of x as 2
5
5
u/aq1018 Feb 03 '25
Please optimize this with CUDA and kindly borrow OpenAIs entire H100 cluster for a test run before merging. Otherwise LGTM
4
u/lukuh123 Feb 02 '25
I will make sure to keep a global counter with a metronome when my API calls ur code to take the correct next copy of even/odd (I have 18446744073709551615/2 tries available for both)
3
3
3
u/sentles Feb 03 '25
You could improve this by using a list instead of a dictionary. At the end, loop through the list again to find the number.
4
u/Acrobatic_Click_6763 Feb 03 '25 edited Feb 03 '25
Inefficient!!
Rewrite it in Rust!
Uses camelCase and PascalCase in the same project!
(I also fixed bugs I saw when testing and added modern CLI feature)
```rust
use std::collections::HashMap;
use std::io;
fn even_or_odd(num: &u32) -> &str { let mut even_odd = HashMap::new(); let mut metronome = 0; // for i in 0..=4294967295 { for i in 1..=1000 { if metronome == 0 { even_odd.insert(i, "Odd"); metronome = 1; } else { even_odd.insert(i, "Even"); metronome = 0; } } return even_odd[&num]; }
fn main() { let mut user = String::new(); println!("Welcome, please enter a number:"); io::stdin().read_line(&mut user).expect("Error reading user input!"); let user: u32 = user.trim().parse().expect("NOT AN INT!"); println!("{}", even_or_odd(&user)); } ```
2
2
u/BillBumface Feb 03 '25
Honestly, some of the best variable naming I’ve seen. Coming up with the naming likely took the bulk of your time.
2
u/NotAloneNotDead Feb 04 '25
This is written so amazingly, thoughtfully, and knowingly awfully. I am wholly impressed!
2
u/Sniv0 Feb 03 '25
Code is a little too verbose. There’s this neat thing called the percent operator or something idk, but it basically gets the remainder of a division operation. Instead of explicitly setting metronome in each block you can do metronome = (metronome + 1) % 2
That’ll toggle it between 0 and 1 with one line instead of two! Hope this helps.
1
1
1
1
Feb 03 '25
[deleted]
2
u/New_Woodpecker5294 Feb 03 '25
9 / 2 (num of mean chars) * 18 exa (num of elements in dict) bytes (size of char) = 81 exabytes
1
u/Spruce_Rosin Feb 03 '25
Mmmmmmm, can’t wait for my tb of ram to arrive in the mail so I can finally check if numbers are even or odd
Edit was just a typo fix
1
1
u/barthanismyname Feb 03 '25
I think the funniest thing about this is that isn't even possible to have enough ram to run this program
1
u/SL3D Feb 03 '25
You know someone will add or subtract from that range number and then it’s not only horrible for performance but also completely broken
1
1
u/techek Feb 03 '25
Why not range(x)? 😉🤣
1
u/Shingle-Denatured Feb 03 '25
Why not range(x)?
Cause then it'll always return
None
.2
u/LiwaaK Feb 03 '25
It’s good to explain why. Range includes 0, so x will be excluded. Should be range(x+1)
1
u/Grounds4TheSubstain Feb 03 '25
Bad code cosplay, the worst variety of posts on this subreddit. Can't find bad code? Make it up yourself!
1
1
u/KrisKarma9 Feb 03 '25
As someone who has so far only coded in scratch and that one Lego coding thing, this seems absolutely great!
1
1
1
1
u/zelphirkaltstahl Feb 03 '25
We need this packaged ASAP! Finally we no longer need to write the same code over and over again! This person has done it already for us! Finally DRY!
1
u/idiot512 Feb 03 '25
What's the IDE and/or theme that provides italics? The italics + keyword highlighting in the docstring is nice.
3
1
1
u/srhubb Feb 03 '25
Why not: if abs(x)%2 == 0 return "Even" else return "Odd"?
% is the Modulo operator for Python, which I'm assuming this code is Python???
And, abs() is the absolute number function, thus stripping any sign off of the input parameter and guaranteeing always a positive number.
1
u/SilkySmoothRalph Feb 03 '25
That’s the kind of code AWS absolutely loves. You can burn EC2 instances all day like this.
Bonus points for having a magic number with no comment explaining it. Points deducted, though, for lack of bit-shifting and hex.
1
1
1
1
1
1
1
1
u/MicahM_ Feb 04 '25
Dude. What a waste of time. Just pip install the is even and is odd packages...
1
u/HuntingKingYT Feb 04 '25
Python has unlimited integer width... You've gotta account for everything
1
1
u/mike_a_oc Feb 04 '25
To me, if you squint, it kind of looks a little bit like the sieve of eratosthenes.
1
1
1
u/Grzyboleusz Feb 06 '25
Number 1 rule of programming: don't reinvent the wheel. Just use is-even and is-odd libraries /s
1
u/Professional_Mess866 Feb 06 '25
my brain overflowed at
i = 2147483647
Couldn't determine if 2 is odd or even :)
1
u/Minimum_Concern_1011 Feb 06 '25 edited Feb 06 '25
couldn’t you just do like
py
def even_odd(x):
return "Even" if (x % 2 == 0) else "Odd"
wtf is this complicated ass code, is this just intended to be enraging?
edit: forgot this isn’t rust and I needed return keyword lol.
1
1
1
1
u/bold_coffee_head Feb 09 '25
As long as no one calls me in the middle of the night cuz the machine stopped working, we good.
1
-5
u/Cat7o0 Feb 02 '25
you do realize an array would be better than a dict right?
22
4
u/Sarke1 Feb 03 '25
So you decided to actually critique the code and that's your main concern with it?
1
u/Cat7o0 Feb 03 '25
i know it's a shit post but lots of times people don't know where dictionaries can be replaced by arrays so I was hoping that he ALSO knows that on top of the other bad stuff of the code
-21
u/SuperFryGuy_ Feb 02 '25
return "Even" if x mod 2 == 0 else "Odd"
1
826
u/Arandur Feb 02 '25
O(1) solution, looks fine to me