Understanding Python bytecode by implementing tail call optimization (blog.fastforwardlabs.com) 12 points by mynameisfiber 11y ago ↗ HN
[–] hughdbrown 11y ago ↗ This code does not do what the author thinks it does: def factorial(N, result=1): if N == 1: return 1 return factorial(N-1, N*result) [–] mynameisfiber 11y ago ↗ No, but the time complexity is the same. [–] hughdbrown 11y ago ↗ Okay but if you are going to call the function factorial, why not have it actually calculate a factorial? This would have done the job: def factorial(N, result=1): if N == 1: return result return factorial(N-1, N*result) Assuming that you never call it with the result argument.As written, it just looks like the author never ran the code.
[–] mynameisfiber 11y ago ↗ No, but the time complexity is the same. [–] hughdbrown 11y ago ↗ Okay but if you are going to call the function factorial, why not have it actually calculate a factorial? This would have done the job: def factorial(N, result=1): if N == 1: return result return factorial(N-1, N*result) Assuming that you never call it with the result argument.As written, it just looks like the author never ran the code.
[–] hughdbrown 11y ago ↗ Okay but if you are going to call the function factorial, why not have it actually calculate a factorial? This would have done the job: def factorial(N, result=1): if N == 1: return result return factorial(N-1, N*result) Assuming that you never call it with the result argument.As written, it just looks like the author never ran the code.
3 comments
[ 4.9 ms ] story [ 39.1 ms ] threadAs written, it just looks like the author never ran the code.