Search code examples
pythoncode-complexity

Complexity class of a function


I apologize if my question isn't appropriate for this website, but this is the only place I know that can answer computer science questions.

For my quiz, we were told to calculate and simplify the complexity class of a function. I understand most of the concepts and everything, but I cannot understand why O(1)is incorrect for the line aset = set(alist). The correct answer is supposed to be O(N), but I don't see why this is.

Here is the complete function:

def sum_to_b(alist,asum):
    aset = set(alist)
    for v in alist:
        if asum-v in aset:
            return (v,asum-v)
    return None 

Solution

  • You need to iterate over each element of 'alist' exactly one time (assuming it is regular iterable) to build 'aset' set.