• 0 Posts
  • 161 Comments
Joined 1 year ago
cake
Cake day: July 2nd, 2023

help-circle





  • affiliate@lemmy.worldto196@lemmy.blahaj.zoneRule
    link
    fedilink
    arrow-up
    22
    ·
    4 days ago

    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)