Ad
  • Custom User Avatar

    First, you should never claim something is faster without actually benchmarking it. Python has many hidden optimizations in built-in functions.

    Second, I guess my solution would be faster in most cases because any stops at the first True, and all stops at the first False, so it would only reach the end of the sequence if it consists of all equal elements. Your algorithm will reach the end in any case. Consider the situation [True, False, ...(10000 elements more)]. My algorithm will stop after checking the first two elements; yours will check all the sequence in any case.

    On the second thought, it would be faster in any case except, maybe, some very short sequences, because the first value of predicate would be either True or False, so one of the any or all functions is guaranteed to stop after checking it.

  • Custom User Avatar

    For your solution, I missed the iterator part in map. yes, the space complexity will be O(1) but then it is a slower iteration over sequence. a simple loop should be faster. Mind me I understand that they both are O(n) time complexity but simple loop should be a faster O(n) out of those two. let me know what you think.

  • Custom User Avatar

    How exactly creating new structures in memory can be more efficient? This solution has O(1) space complexity.

  • Custom User Avatar

    i think counter method is more efficient space wise. however , it has same time complexity

  • Default User Avatar

    I got the same problem.
    Make sure to implement the __eq__(equal) method correctly, the v1==v2 method.

  • Custom User Avatar
  • Custom User Avatar

    It's fine! Learning is what's important :)

  • Custom User Avatar

    Thanks @Kacarott, I really didn't know. Thanks for that lesson.

  • Custom User Avatar

    @geans You are overlooking one important thing. map does NOT go through the whole list. It creates a generator, which produces values one at a time, which feeds into the other functions, which short circuit.

    Here is proof:

    def infinite():
        while True: yield 1
        
    a = map(lambda x: x*2, infinite()) # Works instantly
    b = [x*2 for x in infinite()] # Loops forever (timeout)
    
  • Custom User Avatar

    man, just read my comment again: there is no difference in the present context...

    :/

  • Custom User Avatar

    I believe that what @Ivry wrote is still valid, as this solution goes through the list twice because of the map (imagine a big list), however the problem can be solved in the first two items in case: one is True and the other False.
    So it's not "the best" practice. I mark as cleaver.

  • Default User Avatar

    Okay, so I am going to take a chance here and ask, does this method have any advantage... or the opposite?

  • Default User Avatar

    oh, yes :) thx, I got your point

  • Custom User Avatar

    @Ivry: you're comparing O(2*n) and O(n*2), saying they are different. They aren't.

  • Custom User Avatar
  • Loading more items...