lst1 = [i for i in range(10)]
lst = [[j,j**2] for j in lst1]
print(lst)
[[0, 0], [1, 1], [2, 4], [3, 9], [4, 16], [5, 25], [6, 36], [7, 49], [8, 64], [9, 81]]
n = 100
res = 0
for i in range(n):
for j in range(n):
for k in range(n):
res = i+j+k
print(res)
297
A) O(1) B) O(n) C) O(n^2) D) O(n^3)
def mystery(x):
for i in range(1, len(x)):
key = x[i]
j = i-1
while j >= 0 and key < x[j]:
x[j+1] = x[j]
j -= 1
x[j+1] = key
return x
mystery([5,6,1,2,3,9,7,5])
[1, 2, 3, 5, 5, 6, 7, 9]
What if I want to store all of the ages of the Abyssinia TAs?
ages_list = [['hailey',24],['taylor',24],['harris',19],['natnael',22],['stephen',25],['aku',20],['nebiyou',21],['yonathan',20],['bre',21],['yosef',21]]
ages_list
[['hailey', 24], ['taylor', 24], ['harris', 19], ['natnael', 22], ['stephen', 25], ['aku', 20], ['nebiyou', 21], ['yonathan', 20], ['bre', 21], ['yosef', 21]]
To find their age, I would have to search the the whole list...
for lst in range(len(ages_list)):
if lst[0] == 'stephen':
return lst[1]
File "<ipython-input-51-6da1e4ec7688>", line 3 return lst[1] ^ SyntaxError: 'return' outside function
To solve this problem, we can use a dictionary
A dictionary is like a list, but it has keys and values
ages_dict = {'hailey': 24,'taylor': 24,'harris': 19,'natnael':22,'stephen':25,'aku':20,'nebiyou':21, 'yonathan':20, 'bre':21,'yosef':21}
ages_dict
{'hailey': 24, 'taylor': 24, 'harris': 19, 'natnael': 22, 'stephen': 25, 'aku': 20, 'nebiyou': 21, 'yonathan': 20, 'bre': 21, 'yosef': 21}
How can I find the age of stephen
?
ages_dict['nebiyou']
21
How can I get all the names in the list?
ages_dict.keys()
dict_keys(['hailey', 'taylor', 'harris', 'natnael', 'stephen', 'aku', 'nebiyou', 'yonathan', 'bre', 'yosef'])
How can I get all the ages in the list?
ages_dict.values()
dict_values([24, 24, 19, 22, 25, 20, 21, 20, 21, 21])
How can I get my list from before?
ages_list = [[ta,ages_dict[ta]] for ta in ages_dict]
ages_list
[['hailey', 24], ['taylor', 24], ['harris', 19], ['natnael', 22], ['stephen', 25], ['aku', 20], ['nebiyou', 21], ['yonathan', 20], ['bre', 21], ['yosef', 21]]
FIFO: "FIRST IN FIRST OUT"
visual
def new_queue(n): # n: max no of items
return [None]*n
def add_queue(Q,item):
i = 0
while Q[i]: i+= 1
Q[i] = item
def get_queue(Q):
item = Q[0]
for i in range(0,len(Q)-1):
Q[i]=Q[i+1]
return item
my_queue = new_queue(5)
add_queue(my_queue,'theresa')
add_queue(my_queue,'darartu')
add_queue(my_queue,'aku')
add_queue(my_queue,'taylor')
add_queue(my_queue,'yosef')
get_queue(my_queue)
LIFO: "LAST IN FIRST OUT"
def new_stack(n): # n: max no of items
return [0]+[None]*n
def add_stack(S,item):
S[0] += 1
S[S[0]] = item
def get_stack(S):
item = S[S[0]]
S[0] -= 1
return item
my_stack = new_stack(5)
add_stack(my_stack,'theresa')
add_stack(my_stack,'darartu')
add_stack(my_stack,'aku')
add_stack(my_stack,'taylor')
add_stack(my_stack,'yosef')
get_stack(my_stack)
good for unordered lists
def linear_search(lst,item):
for i in range(len(lst)):
if lst[i] == item:
return i
return None
my_list = ['boston','cairo','dubai','addis ababa','london']
linear_search(my_list,'addis ababa')
3
bad for sorted lists
practice coding binary search
practice coding insertion sort
learn about graphs
learn about breadth first search (BFS)
learn about depth first search (DFS)