this post was submitted on 31 Oct 2024
386 points (98.5% liked)

196

16539 readers
2236 users here now

Be sure to follow the rule before you head out.

Rule: You must post before you leave.

^other^ ^rules^

founded 1 year ago
MODERATORS
 
you are viewing a single comment's thread
view the rest of the comments
[โ€“] affiliate@lemmy.world 21 points 3 weeks ago (1 children)

a wise programmer knows to always ask the question "can i solve this problem in python using metaprogramming?" in this instance, the answer is yes:

def is_even(n: int):
    s = "def is_even_helper(number: int):\n"
    b = True
    for i in range(0, abs(n)+2):
        s += f"\tif (abs(number) == {i}): return {b}\n"
        b = not b
    exec(s)
    return locals().get("is_even_helper")(n)
[โ€“] sepiroth154@feddit.nl 7 points 2 weeks ago

Gotta love how human readable Python always is!