Destide@feddit.uk to Programmer Humor@lemmy.mlEnglish · 4 days agoInfallible Codelemmy.mlexternal-linkmessage-square142fedilinkarrow-up1462arrow-down127
arrow-up1435arrow-down1external-linkInfallible Codelemmy.mlDestide@feddit.uk to Programmer Humor@lemmy.mlEnglish · 4 days agomessage-square142fedilink
minus-squareEuphoma@lemmy.mllinkfedilinkEnglisharrow-up21·3 days 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-squarexthexder@l.sw0.comlinkfedilinkarrow-up6·3 days 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!@feddit.nulinkfedilinkEnglisharrow-up7·3 days 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.