this post was submitted on 15 Jul 2025
448 points (95.0% liked)

Programmer Humor

37222 readers
285 users here now

Post funny things about programming here! (Or just rant about your favourite programming language.)

Rules:

founded 5 years ago
MODERATORS
you are viewing a single comment's thread
view the rest of the comments
[–] Euphoma@lemmy.ml 24 points 1 day ago (1 children)
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 6 points 1 day ago (1 children)

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

[–] lime@feddit.nu 7 points 1 day ago

O(1) means worst and best case performance are the same.