1

If I would like to know other built-in function's time complexity, what are the ways to figure it out except for looking for cheatsheet

2
  • Think about what is the input. For a string - how would you check if it's a digit without looking at all N characters? And there's no reason to look at any more than once Commented Oct 10, 2020 at 23:38
  • This post stackoverflow.com/questions/11253955/… can tell you where to find the source code, and the source code for many python builtins lives there. As for isdigit specifically, you will find that it uses the isdigit function from the C standard library. So finding the time complexity of that function would answer your question. Commented Oct 11, 2020 at 0:02

1 Answer 1

3

Think about what you're actually trying to do, verifying that the string is a digit. The only way to be 100% sure about that is by making sure every character in the string is a digit, therefore it needs to evaluate every character.

Therefore it has a linear time complexity, O(n), where n is the length of the string

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.