Why is Function 1 False?

Constant Time Complexity — Constant Time Complexity In Python | Dataquest


def function1(N):
    for i in range(N):
        print(i)

def function2():
    for i in range(100000):
        print(i)

def function3(N):
    for i in range(100000):
        print(i)
constant1=False
constant2=True
constant3=True

From the link you posted:

In terms of Python, these are functions whose execution time does not depend on the input.

function1’s execution time is going to depend on the size of N because it will print N number of times.

Hope this helps!

1 Like

Hi @hliu19922019 ,

Just adding a few points to the answer provided by Johnsonk51502

The function isn’t really False. But the question given to you was

Three functions are provided for you. For each of them, identify whether or not it has constant time complexity.

And from the learning screen you have learned that

constant-time algorithm is an algorithm whose time complexity does not depend on the data.

So if you take the Function 1, if you give the value of N as 1, it will print 1 value (0). If you given the value 10 to N it will print 10 times (0 to 9). So you can see the with different values of N, the number of lines of code executed (in this case printed) varies with the data.

But in Function 2 you don’t require to provide a data as function2() doesn’t have any parameters. And the function will run 100000 times.

In Function3 even if you provide a value for N, it is not used anywhere within the loop. So if you give N a value of 1 or 100, the function will run 100000 times.

Hence function 2 and 3 are not depended on data while function1 is. So that is why
constant1=False
constant2=True
constant3=True

I hope this clarifies your doubt.