• Euphoma@lemmy.ml
    link
    fedilink
    English
    arrow-up
    22
    ·
    18 hours ago
    def even(n: int) -> bool:
        code = ""
        for i in range(0, n+1, 2):
            code += f"if {n} == {i}:\n out = True\n"
            j = i+1
            code += f"if {n} == {j}:\n out = False\n"
        local_vars = {}
        exec(code, {}, local_vars)
        return local_vars["out"]
    

    scalable version

    • xthexder@l.sw0.com
      link
      fedilink
      arrow-up
      5
      ·
      15 hours ago

      Not even else if? Damn, I guess we’re checking all the numbers every time then. This is what peak performance looks like