[英]Simple query about time complexity for Python “in”
I have a function strip_punctuation(text) which takes in a string of text and removes all punctuation in it using a punctuation list. 我有一个函数strip_punctuation(text),它接收一个文本字符串,并使用标点符号列表删除其中的所有标点符号。 I am not sure about the time complexity whether it is O(N)*N or O(N^2).
我不确定时间复杂度是O(N)* N还是O(N ^ 2)。 I think that it works where it is O(N) for N text length, and then O(N) for length of punctuation.
我认为这适用于N个文本长度为O(N),然后为标点符号长度为O(N)的情况。 May someone clarify the time complexity for this code?
有人可以澄清这段代码的时间复杂度吗?
def strip_punctuation(text):
punctuations = '''!()-[]{};:'"\,<>./?@#$%^&*_~'''
stripped = ""
for i in text:
if i not in punctuations:
stripped = stripped + i
return stripped
If N is len(text)
, then this is O(N): 如果N为
len(text)
,则为O(N):
for i in text
If M is len(punctuations)
, then this code is O(M^2): 如果M为
len(punctuations)
,则此代码为O(M ^ 2):
if i not in punctuations:
stripped = stripped + i
That is because the whole stripped
(which has length >= M) has to be copied M times ( stripped + i
makes a copy of stripped
). 这是因为整个
stripped
(长度> = M)必须被复制M次( stripped + i
构成stripped
的副本)。
So, if both text
and punctuations
were inputs, the complexity would be O(N) * O(M^2), but in this case, M is a constant, so the complexity is O(N) . 因此,如果同时输入了
text
和punctuations
,则复杂度为O(N)* O(M ^ 2),但是在这种情况下,M为常数,因此复杂度为O(N) 。
Note that if punctuations
was very very big, the function would be very very slow, but its complexity would still be just O(N), which only means that it is N times slower when the input is N times bigger. 请注意,如果
punctuations
非常大,该函数将非常慢,但其复杂度仍仅为O(N),这仅意味着当输入大N倍时,它的速度慢N倍。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.