Answer the question
In order to leave comments, you need to log in
Is it necessary to increase the complexity?
Please tell me, I understand correctly that in order to determine the complexity of the following algorithm, you need O (n) * O (n) = O (n ^ 2)
#!/usr/bin/env python3
def reverse_letter(string):
return ''.join([l for l in reversed(string) if l.isalpha()])
Answer the question
In order to leave comments, you need to log in
No. Now, if you had two nested loops on string, then yes. And here the complexity "adds up", because first O (n) - reverse, then O (n) - creating a cycle, then O (n) - join. But since no one needs constants in complexity - it's only O (n)
But, of course, you won't believe it, so here's the proof for you. Time grows linearly with N
If I understand correctly, reversed will only happen once. In pseudocode, you can write it like this:
newStr = reversed(string);
for sym in newStr; // ..
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question