r/shittyprogramming • u/GlueDonkey • Apr 19 '21
7 line "is even" function 🤯
I've attempted to solve the infamous "is even" problem, and I've come up with something that is both short (only 7 lines), and extremely fast (I tested with the number 99999999 and it only takes about 3 seconds)
behold:
def is_even(num):
s = "\\"
try:
eval(f"\"{s*num}\"")
return True
except SyntaxError:
return Falsehow did I make it so fast?
a few ways:
- it's written in python.
- it uses eval, one of python's fastest functions
- it uses strings, which are faster than numbers (the only number this code relies on is the input one...)
- it makes use of the little-known "\" operator, which is one of the fastest operators in python
190
Upvotes
View all comments
Show parent comment
-10
u/TheRandomR Apr 19 '21
This reminds of my first shitty programming in college, it was a simple and generic Python project, but with...
True = false
False = true
...at the beginning to confuse everyone that skips everything before the
mainsince I like using PascalCase instead of camelCase no matter what