diff --git a/Chapter01/chapter1.py b/Chapter01/chapter1.py index 2631bf5..9d9d1be 100644 --- a/Chapter01/chapter1.py +++ b/Chapter01/chapter1.py @@ -9,14 +9,14 @@ ############### var = 13.2 -print(var) +print(type(var)) #13.2 type (var) # var = "Now the type is string" -print(var) +print(type(var)) type(var) # @@ -36,6 +36,7 @@ ''' bool(False) +print(bool(False)) #False va1 = 0 print(bool(va1)) @@ -50,9 +51,11 @@ #True #### Strings -Str1 = 'Hello how are you' +str1 = 'Hello how are you' str2 = "Hello how are you" str3 = 'multiline'+'string'; +print(str1) +print(str2) print(str3) f = 'data' @@ -69,6 +72,22 @@ print(3 * st) #'data.data.data.' +###### Range ###### + +print(list(range(10))) +print(range(10)) +print(list(range(10))) +print(range(1,10,2)) +print(list(range(1,10,2))) +print(list(range(20,10,-2))) + +#[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] +#range(0, 10) +#[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] +#range(1, 10, 2) +#[1, 3, 5, 7, 9] +#[20, 18, 16, 14, 12] + ###### Lists ###### @@ -209,7 +228,7 @@ if thirdList is Secondlist: print("Both are pointing to the same object") else: - print("Both are not pointing to the same object ") + print("Both are not pointing to the same object") ''' Output: Both are equal @@ -223,12 +242,12 @@ Firstlist = [] Secondlist = [] if Firstlist is not Secondlist: - print("Both are pointing to the different object") + print("Both Firstlist and Secondlist variables are the same object") else: - print("Both are not pointing to the different object ") + print("Both Firstlist and Secondlist variables are not the same object") #Output: -# Both are pointing to the different object +# Both Firstlist and Secondlist variables are the same object @@ -461,6 +480,7 @@ from collections import deque s = deque() # Creates an empty deque +print(s) my_queue = deque([1, 2, 'Name']) print(my_queue) #deque([1, 2, 'Name']) @@ -506,9 +526,9 @@ print(chain) #ChainMap({'data': 1, 'structure': 2}, {'python': 3, 'language': 4}) -print (list(chain.keys())) +print(list(chain.keys())) #['python', 'language', 'data', 'structure'] -print (list(chain.values())) +print(list(chain.values())) #[3, 4, 1, 2] print(chain["data"]) #1 diff --git a/Chapter02/matrix_chain.py b/Chapter02/matrix_chain.py index 2d30a87..55b6eed 100644 --- a/Chapter02/matrix_chain.py +++ b/Chapter02/matrix_chain.py @@ -5,10 +5,10 @@ def matrix_chain(mat, i, j): return 0 minimum_computations = sys.maxsize for k in range(i, j): - count = (MatrixChain(mat, i, k) + MatrixChain(mat, k+1, j)+ mat[i-1] * mat[k] * mat[j]) + count = (matrix_chain(mat, i, k) + matrix_chain(mat, k+1, j)+ mat[i-1] * mat[k] * mat[j]) if count < minimum_computations: minimum_computations = count return minimum_computations matrix_sizes = [20, 30, 45, 50] -print("Minimum multiplications are", MatrixChain(matrix_sizes , 1, len(matrix_sizes)-1)) +print("Minimum multiplications are", matrix_chain(matrix_sizes , 1, len(matrix_sizes)-1)) diff --git a/Chapter03/binary_search.py b/Chapter03/binary_search.py index dac62d4..09a6444 100644 --- a/Chapter03/binary_search.py +++ b/Chapter03/binary_search.py @@ -1,16 +1,18 @@ -def binary_search(arr, low, high, key): - while low <= high: - mid = int(low + (high - low)/2) +def binary_search(arr, start, end, key): + while start <= end: + mid = start + (end - start)//2 if arr[mid] == key: return mid elif arr[mid] < key: - low = mid + 1 + start = mid + 1 else: - high = mid - 1 + end = mid - 1 return -1 -arr = [ 2, 3, 4, 2, 10, 40] -x = 10 +arr = [ 4, 6, 9, 13, 14, 18, 21, 24, 38] +x = 13 result = binary_search(arr, 0, len(arr)-1, x) print(result) + + diff --git a/Chapter03/dyna_fib.py b/Chapter03/dyna_fib.py index 5ba97f6..9c81b91 100644 --- a/Chapter03/dyna_fib.py +++ b/Chapter03/dyna_fib.py @@ -1,13 +1,17 @@ -def dyna_fib(n, lookup): - if n == 0: - return 0 - if n <= 2: - lookup[n] = 1 - if lookup[n] is None: - lookup[n] = dyna_fib(n-1, lookup) + dyna_fib(n-2, lookup) +def dyna_fib(n): + if n == 0: + return 0 + if n == 1: + return 1 + if lookup[n] is not None: + return lookup[n] + + lookup[n] = dyna_fib(n-1) + dyna_fib(n-2) return lookup[n] -lookup = [None]*(1000) + +lookup = [None]*(1000) -for i in range(6): - print(dyna_fib(i, lookup)) + +for i in range(6): + print(dyna_fib(i)) diff --git a/Chapter03/factorial.py b/Chapter03/factorial.py index 3469d23..861221a 100644 --- a/Chapter03/factorial.py +++ b/Chapter03/factorial.py @@ -3,8 +3,8 @@ def factorial(n): if n == 0: return 1 else: - return n*factorial(n-1) # make a calculation and a recursive call + return n*factorial(n-1) # Do the calculation and a recursive call - + print(factorial(4)) diff --git a/Chapter03/merge_sort.py b/Chapter03/merge_sort.py index e546be9..d678a03 100644 --- a/Chapter03/merge_sort.py +++ b/Chapter03/merge_sort.py @@ -1,6 +1,6 @@ def merge_sort(unsorted_list): if len(unsorted_list) == 1: - return unsorted_list + return unsorted_list mid_point = int(len(unsorted_list)/2) first_half = unsorted_list[:mid_point] second_half = unsorted_list[mid_point:] diff --git a/Chapter04/CircularList.py b/Chapter04/CircularList.py index 16eed02..e179121 100644 --- a/Chapter04/CircularList.py +++ b/Chapter04/CircularList.py @@ -28,17 +28,26 @@ def append(self, data): def delete(self, data): current = self.head prev = self.head + flag = False while prev == current or prev != self.tail: if current.data == data: if current == self.head: + #item to be deleted is head node self.head = current.next self.tail.next = self.head + elif current == self.tail: + #item to be deleted is tail node + self.tail = prev + prev.next = self.head else: + #item to be deleted is an intermediate node prev.next = current.next self.size -= 1 return prev = current current = current.next + if flag is False: + print("Item not present in the list") def iter(self): @@ -93,11 +102,3 @@ def iter(self): counter += 1 if counter > 2: break - - - - - - - - diff --git a/Chapter04/append_at_any_location_singly_linkedlist.py b/Chapter04/append_at_any_location_singly_linkedlist.py index b5e8f80..c2e1f2f 100644 --- a/Chapter04/append_at_any_location_singly_linkedlist.py +++ b/Chapter04/append_at_any_location_singly_linkedlist.py @@ -7,6 +7,7 @@ def __init__(self, data=None): class SinglyLinkedList: def __init__ (self): + self.tail = None self.head = None self.size = 0 diff --git a/Chapter04/delete_operation_singly_linkedlist.py b/Chapter04/delete_operation_singly_linkedlist.py index be5c04c..c181c0c 100644 --- a/Chapter04/delete_operation_singly_linkedlist.py +++ b/Chapter04/delete_operation_singly_linkedlist.py @@ -6,16 +6,16 @@ def __init__(self, data=None): class SinglyLinkedList: def __init__ (self): - self.tail = None + self.head = None self.size = 0 def append(self, data): # Encapsulate the data in a Node node = Node(data) - if self.tail is None: - self.tail = node + if self.head is None: + self.head = node else: - current = self.tail + current = self.head while current.next: current = current.next current.next = node @@ -29,8 +29,8 @@ def delete_first_node (self): def delete_last_node (self): - current = self.tail - prev = self.tail + current = self.head + prev = self.head while current: if current.next is None: prev.next = current.next @@ -40,12 +40,12 @@ def delete_last_node (self): def delete(self, data): - current = self.tail - prev = self.tail + current = self.head + prev = self.head while current: if current.data == data: - if current == self.tail: - self.tail = current.next + if current == self.head: + self.head = current.next else: prev.next = current.next self.size -= 1 @@ -59,10 +59,18 @@ def delete(self, data): words.append('ham') words.append('spam') +words.delete_first_node() +current = words.head +while current: + print(current.data) + current = current.next + + + words.delete_last_node() -current = words.tail +current = words.head while current: print(current.data) current = current.next @@ -70,7 +78,7 @@ def delete(self, data): words.delete('ham') -current = words.tail +current = words.head while current: print(current.data) current = current.next diff --git a/Chapter04/doubly_linked_list.py b/Chapter04/doubly_linked_list.py index 44a9511..4eb2f94 100644 --- a/Chapter04/doubly_linked_list.py +++ b/Chapter04/doubly_linked_list.py @@ -1,17 +1,17 @@ -class Node(object): +class Node: def __init__ (self, data = None, next = None, prev = None): self.data = data self.next = next self.prev = prev -class DoublyLinkedList(object): +class DoublyLinkedList: def __init__ (self): self.head = None self.tail = None self.count = 0 def append(self, data): - #Append an item to the list. + #Append an item at the end of the list. new_node = Node(data, None, None) if self.head is None: self.head = new_node @@ -49,6 +49,23 @@ def append_at_a_location(self, data): prev = current current = current.next + def iter(self): + current = self.head + while current: + val = current.data + current = current.next + yield val + + + def contains(self, data): + for node_data in self.iter(): + if data == node_data: + print(" Data item is present in the list. ") + return + print(" Data item is not present in the list. ") + return + + words = DoublyLinkedList() @@ -56,14 +73,44 @@ def append_at_a_location(self, data): words.append('ham') words.append('spam') +print("Items in doubly linked list before append") current = words.head while current: print(current.data) current = current.next +words.append_at_start('book') + +print("Items in doubly linked list after append") +current = words.head +while current: + print(current.data) + current = current.next + + +words.append('book') + +print("Items in doubly linked list after adding element at end.") +current = words.head +while current: + print(current.data) + current = current.next + + words.append_at_a_location('ham') +print("Doubly linked list after adding an element after word \"ham\" in the list.") current = words.head while current: print(current.data) current = current.next + + +words = DoublyLinkedList() +words.append('egg') +words.append('ham') +words.append('spam') + + +words.contains("ham") +words.contains("ham2") diff --git a/Chapter05/List_based_queue.py b/Chapter05/List_based_queue.py index a9f72e2..bf1a9ce 100644 --- a/Chapter05/List_based_queue.py +++ b/Chapter05/List_based_queue.py @@ -23,12 +23,22 @@ def dequeue(self): -q= ListQueue() -q.enqueue(4) -q.enqueue('dog') -q.enqueue('cat') -q.enqueue('monday') +q = ListQueue() +q.enqueue(20) +q.enqueue(30) +q.enqueue(40) +q.enqueue(50) +print(q.items) +#Queue is full +#[20, 30, 40] -a= q.size1() +data = q.dequeue() +print(data) +print(q.items) +#20 +#[30, 40] + + +a = q.size1() print(a) diff --git a/Chapter05/Stack.py b/Chapter05/Stack.py index 0c1db3d..3e01863 100644 --- a/Chapter05/Stack.py +++ b/Chapter05/Stack.py @@ -43,11 +43,10 @@ def peek(self): -words = stack() -words.push('4') -words.push('5') -words.push('6') -words.push('7') +words = Stack() +words.push('egg') +words.push('ham') +words.push('spam') #print the stack elements. current = words.top diff --git a/Chapter06/binary_search_tree.py b/Chapter06/binary_search_tree.py index 1487152..c9ebdbd 100644 --- a/Chapter06/binary_search_tree.py +++ b/Chapter06/binary_search_tree.py @@ -12,7 +12,7 @@ def insert(self, data): node = Node(data) if self.root_node is None: self.root_node = node - return + return self.root_node else: current = self.root_node parent = None @@ -22,20 +22,20 @@ def insert(self, data): current = current.left_child if current is None: parent.left_child = node - return + return self.root_node else: current = current.right_child if current is None: parent.right_child = node - return + return self.root_node - def inorder(self): - current = self.root_node + def inorder(self, root_node): + current = root_node if current is None: return - inorder(current.left_child) + self.inorder(current.left_child) print(current.data) - inorder(current.right_child) + self.inorder(current.right_child) def get_node_with_parent(self, data): @@ -112,8 +112,10 @@ def search(self, data): current = self.root_node while True: if current is None: + print("Item not found") return None elif current.data is data: + print("Item found", data) return data elif current.data > data: current = current.left_child @@ -136,6 +138,23 @@ def find_max(self): +tree = Tree() +r = tree.insert(5) +r = tree.insert(2) +r = tree.insert(7) +r = tree.insert(9) +r = tree.insert(1) + +tree.inorder(r) + + + +tree.search(9) + + +tree.remove(9) +tree.search(9) + tree = Tree() tree.insert(5) tree.insert(2) @@ -143,14 +162,5 @@ def find_max(self): tree.insert(9) tree.insert(1) -tree.inorder() - -for i in range(1, 10): - found = tree.search(i) - print("{}: {}".format(i, found)) - - -print(tree.find_min()) - -print(tree.find_max()) - +print(tree.find_min()) +print(tree.find_max()) diff --git a/Chapter06/tree_traversal.py b/Chapter06/tree_traversal.py index cf3d41d..7314801 100644 --- a/Chapter06/tree_traversal.py +++ b/Chapter06/tree_traversal.py @@ -1,8 +1,8 @@ class Node: - def __init__(self, data): - self.data = data - self.right_child = None - self.left_child = None + def __init__(self, data): + self.data = data + self.right_child = None + self.left_child = None n1 = Node("root node") @@ -46,8 +46,8 @@ def postorder(root_node): print(current.data) -inorder( n1) +inorder(n1) print("\n" ) -preorder( n1) +preorder(n1) print("\n" ) postorder(n1) diff --git a/Chapter07/heap.py b/Chapter07/heap.py index 4d9b8f2..ee0aa33 100644 --- a/Chapter07/heap.py +++ b/Chapter07/heap.py @@ -51,7 +51,6 @@ def delete_at_location(self, location): h = MinHeap() for i in ( 4, 8, 7, 2, 9, 10, 5, 1, 3, 6): h.insert(i) - print(h.heap) n = h.delete_at_root() diff --git a/Chapter07/heap_sort.py b/Chapter07/heap_sort.py index 085f18a..39f7a92 100644 --- a/Chapter07/heap_sort.py +++ b/Chapter07/heap_sort.py @@ -1,4 +1,4 @@ -class minHeap: +class MinHeap: def __init__(self): self.heap = [0] self.size = 0 @@ -55,36 +55,28 @@ def heap_sort(self): -h = minHeap() +h = MinHeap() for i in ( 4, 8, 7, 2, 9, 10, 5, 1, 3, 6): h.insert(i) - print(h.heap) -h = minHeap() +h = MinHeap() unsorted_list = [4, 8, 7, 2, 9, 10, 5, 1, 3, 6] - - for i in unsorted_list: h.insert(i) - print("Unsorted list: {}".format(unsorted_list)) -h = minHeap() +h = MinHeap() unsorted_list = [4, 8, 7, 2, 9, 10, 5, 1, 3, 6] - - for i in unsorted_list: h.insert(i) - print("Unsorted list: {}".format(unsorted_list)) - print("Sorted list: {}".format(h.heap_sort())) diff --git a/Chapter08/hashtable.py b/Chapter08/hashtable.py index 2429567..d6f986a 100644 --- a/Chapter08/hashtable.py +++ b/Chapter08/hashtable.py @@ -10,7 +10,7 @@ def __init__(self): self.slots = [None for i in range(self.size)] self.count = 0 self.MAXLOADFACTOR = 0.65 - + self.prime_num = 5 def check_growth(self): loadfactor = self.count / self.size @@ -97,7 +97,7 @@ def h2(self, key): return hv - def put_doubleHashing(self, key, value): + def put_double_hashing(self, key, value): item = HashItem(key, value) h = self._hash(key) j = 1 @@ -111,7 +111,7 @@ def put_doubleHashing(self, key, value): self.slots[h] = item self.check_growth() - def get_doubleHashing(self, key): + def get_double_hashing(self, key): h = self._hash(key) j = 1 while self.slots[h] != None: @@ -129,6 +129,15 @@ def __setitem__(self, key, value): def __getitem__(self, key): return self.get(key) + + + +ht = HashTable() +ht.put_quadratic(“good”, “eggs”) +ht.put_quadratic(“ad”, “packt”) +ht.put_quadratic(“ga”, “books”) +v = ht.get_quadratic(“ga”) +print(v) ht = HashTable() @@ -136,13 +145,28 @@ def __getitem__(self, key): ht.put("better", "ham") ht.put("best", "spam") ht.put("ad", "do not") -ht.put("ga", "collide") +ht.put("ga", "collide") +ht.put(“awd”, “do not”) +ht.put(“add”, “do not”) +ht.checkGrow() for key in ("good", "better", "best", "worst", "ad", "ga"): v = ht.get(key) print(v) - + +ht = HashTable() +ht.put_double_hashing(“good”, “eggs”) +ht.put_double_hashing(“better”, “spam”) +ht.put_double_hashing(“best”, “cool”) +ht.put_double_hashing(“ad”, “donot”) +ht.put_double_hashing(“ga”, “collide”) +ht.put_double_hashing(“awd”, “hello”) +ht.put_double_hashing(“addition”, “ok”) +for key in (“good”, “better”, “best”, “worst”, “ad”, “ga”): +v = ht.get_double_hashing(key) +print(v) +print(“The number of elements is: {}”.format(ht.count)) ht = HashTable() diff --git a/Chapter08/hashtable_chaining.py b/Chapter08/hashtable_chaining.py index 96dcc12..0f0d006 100644 --- a/Chapter08/hashtable_chaining.py +++ b/Chapter08/hashtable_chaining.py @@ -34,14 +34,14 @@ def search(self, key): current = self.head while current: if current.key == key: - print("list ",current.key, current.value) + print("\"Record found:", current.key, "-", current.value, "\"") return True current = current.next return False -class HashTable: +class HashTableChaining: def __init__(self): self.size = 6 self.slots = [None for i in range(self.size)] @@ -79,7 +79,7 @@ def printHashTable(self) : -ht = HashTable() +ht = HashTableChaining() ht.put("good", "eggs") ht.put("better", "ham") ht.put("best", "spam") @@ -99,4 +99,4 @@ def printHashTable(self) : print(v) -ht.printHashTable() +ht.printHashTable() diff --git a/Chapter10/binary_search_iterative.py b/Chapter10/binary_search_iterative.py index 718d4d4..c20a523 100644 --- a/Chapter10/binary_search_iterative.py +++ b/Chapter10/binary_search_iterative.py @@ -4,7 +4,7 @@ def binary_search_iterative(ordered_list, term): index_of_first_element = 0 index_of_last_element = size_of_list while index_of_first_element <= index_of_last_element: - mid_point = (index_of_first_element + index_of_last_element)//2 + mid_point = (index_of_first_element + index_of_last_element)/2 if ordered_list[mid_point] == term: return mid_point if term > ordered_list[mid_point]: @@ -27,7 +27,7 @@ def binary_search_iterative(ordered_list, term): list1 = [10, 30, 100, 120, 500] search_term = 10 -index_position1 = binary_search(list1, search_term) +index_position1 = binary_search_iterative(list1, search_term) if index_position1 is None: print("The data item {} is not found".format(search_term)) else: @@ -37,7 +37,7 @@ def binary_search_iterative(ordered_list, term): list2 = ['book','data','packt', 'structure'] search_term2 = 'structure' -index_position2 = search_ordered(list2, search_term2) +index_position2 = binary_search_iterative(list2, search_term2) if index_position2 is None: print("The data item {} is not found".format(search_term2)) else: diff --git a/Chapter10/binary_search_recursive.py b/Chapter10/binary_search_recursive.py index 33c7e2a..8e6d7b2 100644 --- a/Chapter10/binary_search_recursive.py +++ b/Chapter10/binary_search_recursive.py @@ -12,8 +12,7 @@ def binary_search_recursive(ordered_list, first_element_index, last_element_inde return mid_point -store = [2, 4, 5, 12, 43, 54, 60, 77] -print(binary_search_recursive(store, 0, 7, 12)) + diff --git a/Chapter10/exponential_search.py b/Chapter10/exponential_search.py index 870728b..da87ab8 100644 --- a/Chapter10/exponential_search.py +++ b/Chapter10/exponential_search.py @@ -14,7 +14,7 @@ def binary_search_recursive(ordered_list, first_element_index, last_element_inde -def exponentialSearch(A, search_value): +def exponential_search(A, search_value): if (A[0] == search_value): return 0 index = 1 @@ -25,4 +25,4 @@ def exponentialSearch(A, search_value): -print(exponentialSearch([1,2,3,4,5,6,7,8,9, 10, 11, 12, 34, 40], 34)) +print(exponential_search([1,2,3,4,5,6,7,8,9,10,11,12,34,40],34)) diff --git a/Chapter10/interpolation_search.py b/Chapter10/interpolation_search.py index 8bec8be..16b80f8 100644 --- a/Chapter10/interpolation_search.py +++ b/Chapter10/interpolation_search.py @@ -22,8 +22,8 @@ def interpolation_search(ordered_list, search_value): -store = [44, 60, 75, 100, 120, 230, 250] -a = interpolation_search(store, 120) +list1 = [44, 60, 75, 100, 120, 230, 250] +a = interpolation_search(list1, 120) print("Index position of value 2 is ", a) -print(nearest_mid(store, 0, 6, 120)) +print(nearest_mid(list1, 0, 6, 120)) diff --git a/Chapter10/jump_search.py b/Chapter10/jump_search.py index 71a218f..3393126 100644 --- a/Chapter10/jump_search.py +++ b/Chapter10/jump_search.py @@ -10,33 +10,34 @@ def search_ordered(ordered_list, term): -def jump_search(A, item): +def jump_search(ordered_list, item): + import math print("Entering Jump Search") - n = len(A) - m = int(math.sqrt(n)) + list_size = len(ordered_list) + block_size = int(math.sqrt(list_size)) i = 0 - while i != len(A)-1 and A[i] <= item: - print("Block under consideration - {}".format(A[i: i+m])) - if i+m > len(A): - m = len(A) - i - B = A[i: i+m] - j = search_ordered(B, item) + while i != len(ordered_list)-1 and ordered_list[i] <= item: + print("Block under consideration - {}".format(ordered_list[i: i+block_size])) + if i+ block_size > len(ordered_list): + block_size = len(ordered_list) - i + block_list = ordered_list[i: i+block_size] + j = search_ordered(block_list, item) if j == -1: print("Element not found") return return i + j - if A[i+m-1] == item: - return i+m-1 - elif A[i+m-1] > item: - B = A[i: i+m-1] - j = search_ordered(B, item) + if ordered_list[i + block_size -1] == item: + return i+block_size-1 + elif ordered_list[i + block_size - 1] > item: + block_array = ordered_list[i: i + block_size - 1] + j = search_ordered(block_array, item) if j == -1: print("Element not found") - return + return return i + j - i += m + i += block_size -print(jump_search([1,2,3,4,5,6,7,8,9, 10, 11], 10)) +print(jump_search([1,2,3,4,5,6,7,8,9, 10, 11], 8)) diff --git a/Chapter11/Insertion_sort.py b/Chapter11/Insertion_sort.py index b1ad40d..b4b1305 100644 --- a/Chapter11/Insertion_sort.py +++ b/Chapter11/Insertion_sort.py @@ -13,7 +13,7 @@ def insertion_sort(unsorted_list): -my_list = [10, 11, 12, 1, 2, 3] -print("List before sorting", my_list) +my_list = [5, 1, 100, 2, 10] +print("Original ist", my_list) insertion_sort(my_list) -print("List before sorting", my_list) +print("Sorted list", my_list) diff --git a/Chapter11/Tim_sort.py b/Chapter11/Tim_sort.py index 7646fa6..f42cfb8 100644 --- a/Chapter11/Tim_sort.py +++ b/Chapter11/Tim_sort.py @@ -29,9 +29,9 @@ def Merge(first_sublist, second_sublist): return merged_list -def Tim_Sort(arr,run): +def Tim_Sort(arr, run): for x in range(0, len(arr), run): - arr[x : x + run] = InsertionSort(arr[x : x + run]) + arr[x : x + run] = Insertion_Sort(arr[x : x + run]) runSize = run while runSize < len(arr): @@ -44,12 +44,5 @@ def Tim_Sort(arr,run): run = 2 -Tim_Sort(arr,run) +Tim_Sort(arr, run) print(arr) - - -import random -array = [random.randint(0, 1000) for i in range(1000)] - -Tim_Sort(array,run) -print(array) diff --git a/Chapter12/deterministic_selection.py b/Chapter12/deterministic_selection.py index 8258e3b..0d5336d 100644 --- a/Chapter12/deterministic_selection.py +++ b/Chapter12/deterministic_selection.py @@ -56,10 +56,10 @@ def get_index_of_nearest_median(array_list, first, second, median): -def swap(array_list, first, second): +def swap(array_list, first, index_of_nearest_median): temp = array_list[first] - array_list[first] = array_list[second] - array_list[second] = temp + array_list[first] = array_list[index_of_nearest_median] + array_list[index_of_nearest_median] = temp diff --git a/README.md b/README.md index 12ec142..94bd315 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,9 @@ + + + # Hands-On Data-Structures-and-Algorithms-with-Python-Third-Edition-published-by-Packt Hands-On Data Structures and Algorithms with Python – Third Edition +### Download a free PDF + + If you have already purchased a print or Kindle version of this book, you can get a DRM-free PDF version at no cost.
Simply click on the link to claim your free PDF.
+

https://packt.link/free-ebook/9781801073448

\ No newline at end of file diff --git a/errata.md b/errata.md new file mode 100644 index 0000000..9cc9cd7 --- /dev/null +++ b/errata.md @@ -0,0 +1,52 @@ +# Errata, Corrections and Improvements +---------------------------------------------------- +If you find any mistakes in the third edition of Hands On Data Structures and Algorithms with Python book, or if you have suggestions for improvements, then please [raise an issue in this repository]([https://github.com/PacktPublishing/JavaScript-from-Beginner-to-Professional/issues](https://github.com/PacktPublishing/Hands-On-Data-Structures-and-Algorithms-with-Python-Third-Edition/issues)), or email to us. + +## Chapter 13, Page 403 - Fixing the reference to `ord` array + +There should be a reference calling to the `ord` array as `ord_text` in the `generate_hash` function. + +Incorrect code is: +``` +else: + hash_text[i] = ((hash_text[i-1] - ord_text[i-1]) + ord[i+len_pattern-1]) # calculating next hash value using previous value +``` +Correct code is: +``` +else: + hash_text[i] = ((hash_text[i-1] - ord_text[i-1]) + ord_text[i+len_pattern-1]) # calculating next hash value using previous value +``` + +## Chapter 3, Page 61 - Fixed the missing '/' in `binary search` code + +There should be `//` in place of `/` + +Incorrect code is: +```python +mid = start + (end - start)/2 +if arr[mid] == key: + return mid +``` +Correct code is: +```python +mid = start + (end - start)//2 +if arr[mid] == key: + return mid +``` + + +## Chapter 1, Page 23 - Fixed the missing outputs + +This is the actual dictionary +`mydict = {'a': 1, 'b': 2, 'c': 3}` + +|Function |Description |Example| +| :-------:| :-------: | :-------: | +| `mydict.pop()`| If a given key is present in the dictionary, then this function will remove the key and return the associated value. | `print(mydict.pop('b'))` should give you output `2` | +| `| | `print(mydict)` should give you output `{'a': 1, 'c': 3}` | +| `mydict.popitem()`| This method removes the last key-value pair added in the dictionary and returns it as a tuple. | `print(mydict.popitem())` should give you output `('c', 3)` | +| `| | `print(mydict)` should give you output `{'a': 1, 'b': 2}` | +| `mydict.update()`| Merges one dictionary with another. Firstly, it checks whether a key of the second dictionary is present in the fi rst dictionary; the corresponding value is then updated. If the key is not present in the fi rst dictionary, then the key-value pair is added. | It has two dictionaries as `d1 = {'a': 10, 'b': 20, 'c': 30}` and `d2 = {'b': 200, 'd': 400}`| +| | | `d1.update(d2)` should update values of `d2` on `d1` | +| | | `print(d1)` should give you `{'a': 10, 'b': 200, 'c': 30, 'd': 400}` | +