Ad
  • Custom User Avatar

    You are absolutely right. Don't know what I was thinking hahaha

  • Default User Avatar

    would be best practice if documented & explained accordingly; after all, green computing and suitable for use in library code

  • Custom User Avatar

    The "else" does nothing in this code. Otherwise, great!

  • Custom User Avatar

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

  • Default User Avatar

    While I'll agree that "best practice" isn't necessarily the best tag, I think your critique here is flawed.

    This solution is O(n) time complexity in best, average, and worst cases. There are solutions that are O(1) in the best case (no solution is better than O(n) in the worst case), but whether they are going to be faster or not is largely going to depend on the actual data they receive in theory.

    In practice, I've yet to see any solution in pure python that has tested faster than this solution with the exception of Blind4Basics' forks of this solution, which do greatly narrow down the second step.

    Part of the reason this solution is fast, is because set is a bit of a cheat in that it uses a lot of C code under the hood. Assuming your unique number is not within 0.32% of the beginning of the array (a pretty safe bet), it's going to be faster than just iterating over the array in python.

    That the code is compact is merely a nice bonus.

    In any case, I'm vigorously against downvoting. Having a meaningful discussion about disagreements (as has happened here, repeatedly, even if people don't always read the other comments) is far more useful for everyone than a -1.

  • Custom User Avatar

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

  • Custom User Avatar
  • Custom User Avatar

    Can't believe this is highest for best practice and clever.

    You can do this:

    • without iterating over both lists twice
    • without finding mod 2 of every list item twice (only have to find it 3 times, total)
    • without calculating the length of both arrays (if one of them is 1, clearly it's element 0 from that one, or the other if not)

    Simply:

    • Check if there are more odds or evens in the first 3 list items to determine the list orientation
    • iterate over list until you've found the item that is opposite of the list orientation

    O(n) instead of O(n*2)

  • Custom User Avatar

    this is a minor task i think, so way he solved it is the best instead of split toString and etc etc etc. I im not really sure that his solution can be useful in commercial development, but in this case - good solution, very clever!

  • Custom User Avatar

    Clever and best performance by far, but if your average developer has to stop and think too much about how it works, it's probably not a best practice. Yet... this is the highest rated best practice. Why?

  • Custom User Avatar

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

  • Custom User Avatar

    There are submissions which fail the description. I've seen a couple that would fail on an array of [-2], just because if there's only 1 element, they return that element. The details clearly state for all negative numbers, return 0. Basically, you cannot return a negative and the tests do not explicitly include an array with only negative results, even though that's explicitly stated in the details.