-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathexample_16.py
56 lines (50 loc) · 1 KB
/
example_16.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
def do_a_bunch_of_stuff(items):
last_idx = len(items) - 1
print(items[last_idx])
middle_idx = len(items) / 2
idx = 0
while idx < middle_idx:
print(items[idx])
idx = idx + 1
for num in range(20):
print(num)
do_a_bunch_of_stuff(['pizza', 'toothbrush',
'bring me home, country toad', 'spamwich', 'alien Dustin', 'the cake is a lie', 'Gatorade'])
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
# Time complexity: O(n)
# The first print is constant time O(1) b/c it doesn't change as the input changes.
# The while loop that prints up to the middle index is O(n/2), which simplifies to O(n).
# The last print will print the same number of times, no matter the input size, so it is O(1).
# So O(1 + n/2 + 20) simplifies to O(n) because we drop all of the constants.
# Because, as the input sizes gets huge, dividing by 2 or printing something a constant number of times
# has minimal effect on the performance of this algorithm.
# Space O(1)