I'm fairly new to Python, so I'm still not that good at determining Big O time complexities of some programs. I would like to ask for some help. Here are some code snippets that I had a hard time knowing what the Big O is, and what I think the Big O is:
Code 1: O(n)
def foo(n):
i = 0
k = 0
for i in range(0,len(n)):
for j in range(0,k):
n[i] = k + 1
k = 0
k += 1
Code 2: I actually have no idea here
def bar(x,i,j,y):
if j >= i:
foo = (i+j) // 2
if x[foo] == y:
return foo
elif x[foo] > y:
return bar(x,i,foo-1,y)
else:
return bar(x,foo+1,j,y)
else:
return -1
Code 3: O(m)
def count(a,b):
# Let len(a) = m
# Let len(b) = n
count = 0
for char in a:
if char in b:
count += 1
return count
And if you would, kindly drop some tips on finding the Big O of programs similar to these code snippets. Any help would greatly be appreciated. Thank you!