Destide to Programmer Humor@lemmy.mlEnglish • 1 month agoInfallible Codelemmy.mlimagemessage-square162fedilinkarrow-up1491arrow-down124
arrow-up1467arrow-down1imageInfallible Codelemmy.mlDestide to Programmer Humor@lemmy.mlEnglish • 1 month agomessage-square162fedilink
minus-square@Euphoma@lemmy.mllinkfedilinkEnglish25•1 month agodef 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
minus-square@xthexder@l.sw0.comlinkfedilink6•1 month agoNot even else if? Damn, I guess we’re checking all the numbers every time then. This is what peak performance looks like
minus-squarelime!linkfedilinkEnglish7•1 month agoO(1) means worst and best case performance are the same.
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
Not even else if? Damn, I guess we’re checking all the numbers every time then. This is what peak performance looks like
O(1) means worst and best case performance are the same.