【Udemy高分付費課程】Python數(shù)據(jù)結構與算法 - 終極 Python 編

more code:
https://github.com/arthur1573/DataStructures-Algorithms-Python
// O(n)
def print_items(n): for i in range(n): print(i) print_items(10)
// drop constants
// O(n) + O(n) != O(2n) == O(n)
def print_items(n): for i in range(n): print(i) for j in range(n): print(j) print_items(10)
// O(n^2)
def print_items(n): for i in range(n): for j in range(n): print(i,j) print_items(10)
// O(n^2)
// O(n) * O(n) * O(n) != O(n^3) == O(n^2)
def print_items(n): for i in range(n): for j in range(n): for k in range(n): print(i,j,k) print_items(10)
?
// O(n^2) + O(n) = O(n^2 + n) = O(n^2)
02.06 Big O_ Drop Non-Dominants P9 - 00:02
?def print_items(n): for i in range(n): for j in range(n): print(i,j) for k in range(n): print(k) print_items(10)
// O(1)
def add_items(n): return n
// O(1)
def add_items(n): return n + n
// O(1)
def add_items(n): return n + n + n
// O(log n)
// log2_8 = 3
// O(a) + O(b) = O(a + b)
def print_items(a,b): for i in range(a): print(i) for j in range(b): print(j)
// O(a * b)
def print_items(a,b): for i in range(a): for j in range(b): print(i,j)
class Cookie: def __init__(self, color): self.color = color cookie_one = Cookie('green') cookie_two = Cookie('blue')
class Cookie: def __init__(self, color): self.color = color def get_color(self): return self.color def set_color(self, color): self.color = color cookie_one = Cookie('green') cookie_two = Cookie('blue') print('Cookie one is', cookie_one.get_color()) print('Cookie two is', cookie_two.get_color()) cookie_one.set_color('yellow') print('\nRight now,cookie one is', cookie_one.get_color()) print('Still, cookie two is', cookie_two.get_color())
class LinkedList: def __init__(self, value): def append(self, value): def pop(self): def prepend(self, value): def insert(self, index, value): def remove(self, index):
num1 = 11 num2 = num1 print("Before value is updated:") print("num1 =", num1) print("num2 =", num2) num1 = 22 # add this ↓, result is different # num2 = num1 print("\nAfter value is updated:") print("num1 =", num1) print("num2 =", num2)
dict1 = { 'value':11 } dict2 = dict1 print('Before value is updated:') print("dict1 =", dict1) print("dict2 =", dict2) dict1['value'] = 22 # add this ↓, result is same # dict2 = dict1 print('\nAfter value is updated:') print("dict1 =", dict1) print("dict2 =", dict2)
?
// head has a value
04.03 LL_ Under the Hood P19 - 01:48
?head: { ? ? "value": 11, ? ? "next": { ? ? ? ? "value": 3, ? ? ? ? "next": { ? ? ? ? ? ? "value": 23, ? ? ? ? ? ? "next": { ? ? ? ? ? ? ? ? "value": 7, ? ? ? ? ? ? ? ? "next": None ? ? ? ? ? ? } ? ? ? ? } ? ? } } print(head['next']['next']['value']) # output: 23 print(head['value']) # output: 11 print(head['next']) # outlput: {'value': 3, 'next': {'value': 23, 'next': {'value': 7, 'next': None}}}
# a Node
class Node: def __init__(self, value): self.value = value self.next = None
# create new Node
class LinkedList: def __init__(self, value): create new Node def append(self, value): create new Node add Node to end # def pop(self): def prepend(self, value): create new Node add Node to beginning def insert(self, index, value): create new Node insert Node def remove(self, index):
?
# head is the first node
04.04 LL_ Constructor P20 - 04:30
?class Node: def __init__(self, value): self.value = value self.next = None class LinkedList: def __init__(self, value): new_node = Node(value) self.head = new_node self.tail = new_node self.length = 1 my_linked_list = LinkedList(4) print(my_linked_list.head.value) # output: 4
def print_list(self): temp = self.head while temp is not None: print(temp.value) temp = temp.next
# head does not have a value
head = { "value": None "next": None } print(head['value']) # output: None print(head['next']) # output: None
標簽: