Ad
  • Custom User Avatar
  • Default 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

    Oh man... I wish I'd spotted this earlier! My solution was a lot more convoluted spotting a pattern and connection between the supplied row number and the first number on the row rather than the simplest solution of "what is the answer in comparison to the row number"...

    Nice work!

  • Default 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.

  • Default User Avatar

    I understand what do you mean. I am not presenting well. I agree that it is the best way for the computer to run.
    I just want to say that not all know this method. It also require your math skill.
    Not everyone has good math skill, but this method is the most fast way and clever.

  • Custom User Avatar

    I'm going to have to completely disagree with you banny127: this is the best answer because it was calculated before coding.

    Math first; code later. Your CPU will thank you.

    Programmers need to have a ton of tools in their toolbox, and your casual dismissal of "algebra" is, frankly, terrifying--given that all programming boils down to logical/mathematical computations.

  • Custom User Avatar

    That depends on the view of what is the best solution. However, in most computer algorithms the best solution (out of all that give correct results) the best is where is minimum used memory & CPU time. This is a solution in constant time because there was a previous analysis with pen & paper. That is what I call a real optimization - the author found a way of different solving of the same problem using less resources.

  • Default User Avatar

    This comment is hidden because it contains spoiler information about the solution

  • Default User Avatar
  • Custom User Avatar

    Good work!

  • Default User Avatar

    After few days that code has been submited.

  • Custom User Avatar

    As far as I know there are no errors in the Python tests; nearly 50 Codewarriors got the Python kata.

  • Default User Avatar

    This comment is hidden because it contains spoiler information about the solution