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

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

  • Custom User Avatar