Docsity

Data Structures and Algorithms in Python: Key Concepts and Implementations, Exams of Advanced Education

Toggle sidebar
bg1
DATA STRUCTURES AND
ALGORITHMS PYTHON
Name Every Data Structure (There are 8 Main Ones) - Correct Answers -1. Array
2. Stack
3. Queue
4. Linked Lists
5. Hash Table
6. BST
7. B-Tree, Red Black Trees
8. Heaps
Name Every Python Super Basic Datastructure - Correct Answers -1. List
2. Tuples
3. Sets
4. Strings
5. Arrays (you must use numpy)
Strings - Correct Answers -1. They are immutable
2. Access characters in strings like you do for arrays/lists
3. empty strings are false, non empty are true.
4. | and & don't work on strings
Slicing - Correct Answers -string_name[beginning:end:step]
*end is not inclusive
*works for strings, lists, and tuples
Useful String Methods - Correct Answers -1. s[3]
2. s + t
3. len(s)
4. s in t
5. s.strip()
6. s.startswith(prefix)
7. s.endswith(suffix)
8. s.find(substring, beg, end) - returns the first position where substring is found.
9. s.rfind(substring, beg,end) - returns the position of the last occurrence of string
10. s.lower() - changes case
11. s.upper() - changes case
12. s.swapcase() - changes to other case
Python List - Correct Answers -1. not always homogenous
pf3
pf4
pf5

Partial preview of the text

Download Data Structures and Algorithms in Python: Key Concepts and Implementations and more Exams Advanced Education in PDF only on Docsity!

DATA STRUCTURES AND

ALGORITHMS PYTHON

Name Every Data Structure (There are 8 Main Ones) - Correct Answers -1. Array

  1. Stack
  2. Queue
  3. Linked Lists
  4. Hash Table
  5. BST
  6. B-Tree, Red Black Trees
  7. Heaps Name Every Python Super Basic Datastructure - Correct Answers -1. List
  8. Tuples
  9. Sets
  10. Strings
  11. Arrays (you must use numpy) Strings - Correct Answers -1. They are immutable
  12. Access characters in strings like you do for arrays/lists
  13. empty strings are false, non empty are true.
  14. | and & don't work on strings Slicing - Correct Answers -string_name[beginning:end:step] *end is not inclusive *works for strings, lists, and tuples Useful String Methods - Correct Answers -1. s[3]
  15. s + t
  16. len(s)
  17. s in t
  18. s.strip()
  19. s.startswith(prefix)
  20. s.endswith(suffix)
  21. s.find(substring, beg, end) - returns the first position where substring is found.
  22. s.rfind(substring, beg,end) - returns the position of the last occurrence of string
  23. s.lower() - changes case
  24. s.upper() - changes case
  25. s.swapcase() - changes to other case Python List - Correct Answers -1. not always homogenous
  1. mutable
  2. used to implement stacks and queues Runtime and Space-Time of Selection Sort - Correct Answers -Runtime: theta(N^2) Space-Time: Big O(1) Runtime and Space-Time of Heap Sort - Correct Answers -Runtime: Omega(N) if all elements are equal, Big O(NlogN) Space-Time: Theta(N) Runtime and Space-Time of In-place Heap Sort - Correct Answers -Runtime: O(NlogN) Space-Time: Theta(1) Python List Methods - Correct Answers -1. append(x)
  3. extend(x)
  4. insert(i, x)
  5. min(list)
  6. max(list)
  7. del a[i:j]
  8. a.reverse()
  9. a.sort()
  10. B = copy.deepcopy(A)
  11. b = copy.copy(A)
  12. remove(index) Tuples - Correct Answers -1. collection of python objects separated by commas
  13. immutable
  14. use () to create Tuples Methods - Correct Answers -1. len(tuple_name)
  15. tuple(list_name)
  16. cmp() - returns 0 if matched. 1 when not matched and longer, -1 when not matched and shorter
  17. max() - returns max el
  18. min() - returns min el Python Sets - Correct Answers -1. unordered collection data type that is iterable, mutable, and has no duplicate elements
  19. based on datastructure known as hash table
  20. highly optimized for checking contains
  21. to create a set you need to use set(list) *Only instances of immutable types can be added to python set Methods - Correct Answers -1. add(x)
  22. union(other)
  23. intersect(other)

Queues using collections.deque() Class - Correct Answers -1. q.append(e) - pushes element to queue

  1. q[0] - retrieves but does not remove the first element in the queue
  2. q.popleft() - remove and return the first element in the queue Binary Trees - Correct Answers -1. one value for node, left child, right child
  3. children are trees themselves Binary Tree Implementation - Correct Answers -class BinaryTreeNode(): def init (self, data=None, left=None, right=None): self.data = data self.left = left self.right = right Heaps - Correct Answers -1. A heap is just a specialized binary tree.
  4. There are min heaps and max heaps.
  5. They are sometimes referred to as priority queues because they are in a particular order.
  6. Use heaps whenever you only care about the max or min values. AND when you don't care about fast lookup, deletion, and searches of arbitrary elements.
  7. Use the heapq library in Python *Note: heapq only has minheap functionality, so you need to insert the - of the number to get maxheap functionality. Functions from heapq module - Correct Answers -1. heapq.heapify(L) - transforms the elements of L into an in-place heap
  8. heapq.nLargest(k,L) - returns the k largest element in the heap
  9. heapq.nSmallest(k,L) - returns the k smallest element in the heap
  10. heapq.heappush(h,e) - pushes a new element on the heap
  11. heapq.heappop(h) - pops the smallest element from the heap
  12. heapq.heappushpop(h,a) - pushes a to the heap, pops the smallest element
  1. e = h[0] - returns the smallest element on the heap without popping it Binary Searching Specific to Python - Correct Answers -1. Use the Bisect module in python Binary Searching using bisect module - Correct Answers -1. bisect.bisect_left(a,x) - to find the first element in the list that is not less than x. Returns the index.
  2. bisect.bisect_right(a,x) - find the first element in the list that is not greater than x. Returns the index. Hash maps - Correct Answers -1. A hash map is a data structure used to store keys, optionally, with corresponding values. Inserts, deletes, and lookups take O(1) time on average.
  3. A key is stored in an array based on its hash code.
  4. When 2 keys map to the same hashcode, this is called a collision. This is handled by using linked lists or arrays.
  5. When the load (n/m) grows to large a new array with a larger number of locations is allocated and the objects are moved to the new array.
  6. Mutable containers are not hashable. How do you make hashmaps? - Correct Answers -1. collections.Counter
  7. set
  8. dict
  9. collections.defaultdict *Key difference between set and the rest: sets only store keys, the rest store key value pairs. Functions from Sets for Hashmaps - Correct Answers -1. s.add(x)
  10. s.remove(x)
  11. s.discard(x)
  12. x in s
  13. s <= t - this means "is s a subset of t"
  14. s - t - returns elements in s that are not in t Functions from other data types for hashmaps - Correct Answers -1. similar to those on set
  15. iteration over the data structure is actually iteration over the keys
  1. sort by ones place
  2. compare 10's places
  3. sort by 10's
  4. continue.. Runtime and Space-Time of Merge Sort - Correct Answers -Runtime: Theta(NlogN) Space-Time: Theta(N) Runtime and Space-Time of Insertion Sort - Correct Answers -Runtime: Omega(N) if already sorted, O(N^2) if fully unsorted Space-Time: Theta(N) Runtime and Space-Time of Quick Sort - Correct Answers -Runtime: Omega(N) O(N^2) if array already sorted and chose left most for pivot always Space-Time: O(log N) Runtime and Space-Time of Radix Sort - Correct Answers -Runtime: theta(Nk) Space-Time: O(N+k)