Ad
  • Default User Avatar

    As a noob, I was just trying to solve the problem as best as I could with what I know. But you're right, I didn't consider optimizing my code or think in terms of cost and efficiency. Thanks for the tips including the search strings. Your comments are definitely helpful and I'll take all of this into consideration as I continue to practice here. Thanks again!

  • Custom User Avatar

    This solution might have been acceptable on a lower level kata, but once you reach 5kyu and especially in the 4kyu+ range, it's important to have a solid understanding of optimization. I can't spell it out for you (since that won't help you learn), but to point you in the right direction, I'll tell you some problematic areas.

    When you convert the entire array into two seperate enumerates, you're creating a lot of unncessary overhead. Try to think of a way to acomplish the same thing without reconstructing the entire dataset. If that's too much to do at once, try to use one instead of two. Doing so correctly will almost double your efficiency, as you'll iterate through the data once instead of twice. Bonus tip: this problem can be solved in many ways without using enumerate, or keeping track of the index at all.

    Also, every time you try to get the index of an element or transform the array (appending or removing elements) there is a cost associated with doing so. Try to figure out a way to remove the excess directions without making so many .remove() and .index() calls.

    As a side note, the practice of excessively using try-except blocks can also cause unneeded overhead and in general is not a very optimal programming practice. In some cases, try-except is necessary, but in this case and many others it's better to figure out a way to solve the problem without having to resort to exception handling.

    I can understand your frustration, because I was in your shoes not too long ago. You come up with a solution that "works" but is incredibly slow in comparison to what it could be. A lot of programming is not about creating a rough draft that barely functions, but creating an optimized solution that performs efficiently. Efficiency is very relevant in the professional world as well, imagine the level of optimization a company like google has to do in order for them to index through millions of search results for countless customers at the same time. They're a high end example, but that happens to a smaller degree even in small-medium sized businesses.

    Edit: if you are looking for any other ways to improve or hit a wall, be sure to look up "python time complexity" or "python optimization" and "dynamic programming". There are also countless tools out there to assess your code and determine which parts are taking up the most time. It's a great idea to run the code in an IDE without a time limiter, and slowly work on reducing the run time (rather than just trying to get it to pass the CodeWars tests). Optimization is one of the biggest hurdles that people have along the road to becoming a competent developer. But once you figure it out properly, you'll significantly improve as a programmer.

  • Custom User Avatar
    1. spoiler flag! x/
    2. errr... it's not optimization that you need but a complete change of algorithm. Try your code on a random list of 100000 elements on your IDE, you'll understand better. For now, it's like if you were trying to move from Paris to London, doing like 8 times the travel before actually stopping at London :o x)

    Might be time for you to read a bit about code/algorithm time complexity.

  • Default User Avatar

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

  • Default User Avatar

    I got tripped up on "www.xakep.ru"... the test returned "None is not equal to xakep". What the heck? Is it expecting "xakep" or None? I tried both but it gives me the same error regardless. I wish I could view the actual test to determine what it is trying to do.

  • Default User Avatar

    OMG! I spent a couple of hours solving this problem. Missed the mathematical connection. Excellent solution man!