-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBig-O.py
72 lines (55 loc) · 1.48 KB
/
Big-O.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/usr/bin/env python
# Question: Given the following code fragment, what is its Big-O running time? ❓
"""
1. O(n)
2. O(n**2) ✔️
3. O(log n)
4. O(n**3)
"""
""" Review 📝
We have one for insede in that other(nested for loops). So that means
for each loop in the first for loop, we'll have the range of n.
"""
def question(n):
test = 0 # Initial value
for i in range(n): # run for the range n. e.g is n=5, the loop will run 1,2,3,4,5
for j in range(n):
print(i, '', j)
test = test + i * j
return test
# Question 2: Given the following code fragment, what is its Big-O running time? ❓
""" Review 📝
In this case, whe have two for loops that does not have nesteding.
However, are running in the same index position. I think that it's lineal time complexity algorithm.
"""
def question_2(n):
test = 0
for i in range(n):
test = test + 1
for j in range(n):
test = test - 1
print(i, '', j)
"""
1. O(n) ✔️
2. O(n**2)
3. O(log n)
4. O(n**3)
"""
# Question 3: Given the following code fragment, what is its Big-O running time? ❓
""" Review 📝
Here we have while loop. First, i is iqual to n and while i are greater that 0
i will be floor devided of 2. In addiction, does not exists a correlation between k and i.
"""
def question_3(n):
i = n
while i > 0:
k = 2 + 2
i = i // 2
print(i)
question_3(100)
"""
1. O(n) ✔️
2. O(n**2)
3. O(log n)
4. O(n**3)
"""