Ad
  • Custom User Avatar

    I believe len() is not calculated when you ask for it, but when the string changes. Don't change the string and ther is no charge. Strings are immutable, therefore str(value) will have the same id() no matter where you use it. As both loops are executed the same number of times, they will have the same number of conversions to int. The for loop may be slightly faster, and possibly easier to read.

    Someone please correct me if I'm wrong!

  • Custom User Avatar

    Incredibly slow with large value arrays - this method for finding primes takes forever. If anyone sees this, this is very bad practice. He's doing n-complexity calls multiple times in each loop. I'd have to totally deconstruct his solution to guess at the complexity - but it's bad.

  • Custom User Avatar

    It is not optimally efficient, sure. But not O(n^2) - string gets shorter on each iteration.

  • Custom User Avatar

    Insanely slow if you have very large strings - clever but very, very slow. For example, running this "("*100000 + ")"*100000 takes 30 seconds, another magnitude and you're looking at hours. This is due to n calls of replace within the loop making this this algorithm O(n^2).

  • Custom User Avatar

    Repeatedly converts value to str in loop and calculates the link - better to sacrifice the one-liner and store the conversions in variables outside teh generator.