3 ms·
I would be embarrassed to admit the amount of times I have discovered this in my Python code for foo in ... if i in foo: creating something of quadratic
by harikb 6y ago
I would be embarrassed to admit the amount of times I have discovered this in my Python code
for foo in ...
if i in foo:
creating something of quadratic complexity!
Yes, I would rather take the explicitly verbose Go code
To be clear: If foo is a dict, we have one loop. If foo is a list, we have two loops. If accidentally happens to be a string, even then it is nested loops
- setr 6y agoIsn't this just an issue of having types, versus not? Although I do remember trying racket, where they solved the same problem by naming functions dict-remove set-remove hash-remove vector-drop etc, which does the job but never have I been more offended thinking I was going in for its abstraction capabilities
- kixiQu 6y agoThis seems like a misunderstanding of quadratic complexity. If every i in every foo is checked/operated upon once, that's still linear, no matter if the nesting structure requires nesting flow control.