0

I have a function strip_punctuation(text) which takes in a string of text and removes all punctuation in it using a punctuation list. I am not sure about the time complexity whether it is O(N)*N or 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. 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
2
  • I do notice that string concatenation in iteration (stripped = stripped + i) is not recommended and is consider O(N^2) in some style guide. Commented May 3, 2018 at 4:12
  • O(n^2) is probably right. Look at the maketrans() and translate() functions in the standard library (exact details depend on the Python version you're using), they can almost certainly do this job faster (if only because the loop over characters is written in C instead of Python). Commented May 3, 2018 at 4:47

1 Answer 1

1

If N is len(text), then this is O(N):

for i in text

If M is len(punctuations), then this code is 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).

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).

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.