Loading collection data...
Collections are a way for you to organize kata so that you can create your own training routines. Every collection you create is public and automatically sharable with other warriors. After you have added a few kata to a collection you and others can train on the kata contained within the collection.
Get started now by creating a new collection.
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 firstTrue
, andall
stops at the firstFalse
, 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
orFalse
, so one of theany
orall
functions is guaranteed to stop after checking it.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.
How exactly creating new structures in memory can be more efficient? This solution has O(1) space complexity.
i think counter method is more efficient space wise. however , it has same time complexity
Very smart!
Beautiful !
clean and easy to read
Creative!
Python's string objects are immutable, so each concatenation generates a new string instead of modifying the existing one in place. Using f-strings is twice as fast.
why do I need the length of the string?
Hey @FroginFog, I think that after line
word = list(word)
word is no longer string but a list of strings. So lets use word Apple as example. After mentioned line word is a list containing["A", "p", "p", " l", "e"]
. When we replace first item with ASCII code we end up with["65", "p", "p", " l", "e"]
.It's still O(n).
It's fine! Learning is what's important :)
Thanks @Kacarott, I really didn't know. Thanks for that lesson.
@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:
man, just read my comment again: there is no difference in the present context...
:/
Loading more items...