├── .vscode └── settings.json ├── Algorithms ├── Dynamic Programming │ ├── fibonnaci.py │ └── mazepaths.py ├── General │ ├── GraphSearchAlgorithms.md │ ├── SortingAlgorithms.md │ └── bitwise.py ├── Java │ ├── BreadthFirstSearch.java │ └── MergeSort.java ├── Python │ ├── DivideAndConquer.py │ └── GreedyGraphAlgorithms.py └── Useful_Algorithms.md ├── Data Structures ├── General │ ├── Arrays.md │ ├── HashTables.md │ ├── LinkedList.md │ ├── Stack&Queue.md │ └── Trees.md ├── Java │ ├── AVLTree.java │ ├── BinarySearchTree.java │ ├── DisjointSet.class │ ├── DisjointSet.java │ ├── DynamicArray.java │ ├── HashMap.java │ ├── LinkedList.java │ ├── PriorityQueue.class │ ├── PriorityQueue.java │ ├── Queue.class │ ├── Queue.java │ ├── Stack.class │ ├── Stack.java │ ├── Test.class │ └── Test.java ├── Python │ ├── Arrays.py │ ├── LinkedList.py │ ├── Queue.py │ ├── Stack.py │ └── Trie.py └── Useful_Data_Structures.md ├── Frameworks └── ReactJS │ ├── Introduction.md │ ├── jsxBasicOverview.jsx │ └── reactOverview.jsx ├── Object Oriented Programming ├── Abstraction.md ├── Automobile.class ├── Automobile.java ├── Car.class ├── Car.java ├── Encapsulation.md ├── Inheritance.md ├── Introduction.md ├── Polymorphism.md └── Test.java ├── Programming Languages └── Javascript │ ├── es6Overview.js │ └── jsOverview.js ├── README.md ├── System Design └── system-design-notes.md └── WebDevEncyclopedia.md /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "python.linting.pylintEnabled": false 3 | } -------------------------------------------------------------------------------- /Algorithms/Dynamic Programming/fibonnaci.py: -------------------------------------------------------------------------------- 1 | # Normal recursive function 2 | n = 40 3 | 4 | def fib_recursive(n): 5 | if (n == 1) or (n ==2): 6 | return 1 7 | else: 8 | return fib_recursive(n-1) + fib_recursive(n-2) 9 | 10 | #print(fib_recursive(n)) 11 | 12 | # Recursion with memoization 13 | def fib_memoize(n, memo): 14 | if (memo[n]): 15 | return memo[n] 16 | else: 17 | if (n == 1) or (n == 2): 18 | memo[n] = 1 19 | return 1 20 | else: 21 | memo[n] = fib_memoize(n-1, memo) + fib_memoize(n-2, memo) 22 | return memo[n] 23 | 24 | 25 | #print(fib_memoize(n, [0]*(n+1))) 26 | 27 | 28 | # Using a for loop 29 | 30 | def fib_loop(n): 31 | s1 = 1 32 | s2 = 1 33 | for i in range(2, n): 34 | temp = s2 35 | s2 = s2 + s1 36 | s1 = temp 37 | return s2 38 | 39 | print(fib_loop(n)) 40 | -------------------------------------------------------------------------------- /Algorithms/Dynamic Programming/mazepaths.py: -------------------------------------------------------------------------------- 1 | # Problem: given a NxN grid, and certain obstacles, give the total number of possible paths to solve that maze. 2 | 3 | # First implement naive recursive solution 4 | sampleMaze = [[0, 0, 0, 0, 0, 0], [0, 0, 1, 1, 0, 1], [0, 0, 0, 0, 0, 0], [0, 1, 0, 0, 1, 0], [0, 1, 1, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0]] 5 | 6 | def solveMazeRecursion(s, t): 7 | if (s[0] >= len(sampleMaze)) or (s[1] >= len(sampleMaze[s[0]])) or (sampleMaze[s[0]][s[1]] == 1): 8 | return 0 9 | elif (s[0] == t[0]) and (s[1] == t[1]): 10 | return 1 11 | else: 12 | return solveMazeRecursion((s[0], s[1]+1), t) + solveMazeRecursion((s[0]+1, s[1]), t) 13 | 14 | print(solveMazeRecursion((0, 0), (6, 5))) 15 | 16 | 17 | # More efficient recursive solution 18 | memoMaze = [] 19 | for i in range(len(sampleMaze)): 20 | tempList = [] 21 | for j in range(len(sampleMaze[i])): 22 | tempList.append(0) 23 | memoMaze.append(tempList) 24 | 25 | def solveMazeMemo(s, t): 26 | if (s[0] >= len(sampleMaze)) or (s[1] >= len(sampleMaze[s[0]])) or (sampleMaze[s[0]][s[1]] == 1): 27 | return 0 28 | elif (s[0] == t[0]) and (s[1] == t[1]): 29 | return 1 30 | else: 31 | memoMaze[s[0]][s[1]] = solveMazeRecursion((s[0], s[1]+1), t) + solveMazeRecursion((s[0]+1, s[1]), t) 32 | 33 | return memoMaze[0][0] 34 | 35 | print(solveMazeMemo((0, 0), (6, 5))) 36 | 37 | 38 | # Most efficient solution, Dynamic Programming with bottom up approach 39 | def solveMazeDP(): 40 | newMaze = [] 41 | for i in range(len(sampleMaze)): 42 | tempList = [] 43 | for j in range(len(sampleMaze[i])): 44 | tempList.append(0) 45 | newMaze.append(tempList) 46 | newMaze[len(sampleMaze)-1][len(sampleMaze[0])-1] = 1 47 | for i in range(len(sampleMaze)-1, -1, -1): 48 | for j in range(len(sampleMaze[i])-1, -1, -1): 49 | if (sampleMaze[i][j] != 1) and (newMaze[i][j] != 0): 50 | if (j != 0): 51 | newMaze[i][j-1] += newMaze[i][j] 52 | if (i != 0): 53 | newMaze[i-1][j] += newMaze[i][j] 54 | return newMaze[0][0] 55 | 56 | print(solveMazeDP()) 57 | 58 | 59 | -------------------------------------------------------------------------------- /Algorithms/General/GraphSearchAlgorithms.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/l1ghtspeed/Programming-Notes/0ed0df7382f30512254438c08ce1b3081a9d3252/Algorithms/General/GraphSearchAlgorithms.md -------------------------------------------------------------------------------- /Algorithms/General/SortingAlgorithms.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/l1ghtspeed/Programming-Notes/0ed0df7382f30512254438c08ce1b3081a9d3252/Algorithms/General/SortingAlgorithms.md -------------------------------------------------------------------------------- /Algorithms/General/bitwise.py: -------------------------------------------------------------------------------- 1 | def isPowof2(a): 2 | while a > 2: 3 | temp = bin(a) 4 | if temp[-1] == '1': 5 | return False 6 | else: 7 | a = a>>1 8 | return True 9 | 10 | def s(): 11 | for i in range(17): 12 | print(isPowof2(i)) 13 | s() 14 | -------------------------------------------------------------------------------- /Algorithms/Java/BreadthFirstSearch.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/l1ghtspeed/Programming-Notes/0ed0df7382f30512254438c08ce1b3081a9d3252/Algorithms/Java/BreadthFirstSearch.java -------------------------------------------------------------------------------- /Algorithms/Java/MergeSort.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/l1ghtspeed/Programming-Notes/0ed0df7382f30512254438c08ce1b3081a9d3252/Algorithms/Java/MergeSort.java -------------------------------------------------------------------------------- /Algorithms/Python/DivideAndConquer.py: -------------------------------------------------------------------------------- 1 | import random 2 | 3 | def mergeSort(a): 4 | newA = [] 5 | if len(a) > 2: 6 | s1 = mergeSort(a[:int(len(a)/2)]) 7 | s2 = mergeSort(a[int(len(a)/2):]) 8 | n = len(s1) + len(s2) 9 | i = 0 10 | j = 0 11 | for k in range(n): 12 | if j >= len(s2): 13 | newA.append(s1[i]) 14 | i += 1 15 | elif i >= len(s1): 16 | newA.append(s2[j]) 17 | j += 1 18 | else: 19 | if s1[i] < s2[j]: 20 | newA.append(s1[i]) 21 | i += 1 22 | else: 23 | newA.append(s2[j]) 24 | j += 1 25 | return newA 26 | elif len(a) == 2: 27 | if a[0] < a[1]: 28 | return [a[0], a[1]] 29 | else: 30 | return [a[1], a[0]] 31 | else: 32 | return a[0] 33 | 34 | print(mergeSort([3, 1, 2, 5, 2, 10, 2, 12])) 35 | 36 | 37 | count = 0 38 | def countInversions(a): 39 | global count 40 | newA = [] 41 | if len(a) > 2: 42 | s1 = countInversions(a[:int(len(a)/2)]) 43 | s2 = countInversions(a[int(len(a)/2):]) 44 | n = len(s1) + len(s2) 45 | i = 0 46 | j = 0 47 | for k in range(n): 48 | if j >= len(s2): 49 | newA.append(s1[i]) 50 | i += 1 51 | elif i >= len(s1): 52 | newA.append(s2[j]) 53 | j += 1 54 | else: 55 | if s1[i] <= s2[j]: 56 | newA.append(s1[i]) 57 | i += 1 58 | else: 59 | newA.append(s2[j]) 60 | j += 1 61 | count += len(s1) - i 62 | return newA 63 | elif len(a) == 2: 64 | if a[0] < a[1]: 65 | return [a[0], a[1]] 66 | else: 67 | return [a[1], a[0]] 68 | else: 69 | return [a[0]] 70 | 71 | countInversions([1, 3, 5, 2, 4, 6]) 72 | print(count) 73 | 74 | ''' 75 | def quickSort(a): 76 | ''' 77 | 78 | 79 | def partition(a): 80 | pivot = choosePivot(a) 81 | print(a) 82 | print(pivot) 83 | p = a[0] 84 | a[0] = a[pivot] 85 | a[pivot] = p 86 | print(a) 87 | if len(a) == 1: 88 | return a 89 | j = 1 90 | for i in range(1, len(a)): 91 | if a[i] <= a[0]: 92 | temp = a[i] 93 | a[i] = a[j] 94 | a[j] = temp 95 | j += 1 96 | num = a[0] 97 | a[0] = a[j-1] 98 | a[j-1] = num 99 | return a 100 | 101 | def choosePivot(a): 102 | return random.randint(0, len(a)-1) 103 | 104 | print(partition([3, 7, 1, 5, 2, 8, 6, 4])) -------------------------------------------------------------------------------- /Algorithms/Python/GreedyGraphAlgorithms.py: -------------------------------------------------------------------------------- 1 | # Graph type Greedy Algs 2 | 3 | # Classic implementation of depth first search 4 | def dfs(edges, n): 5 | ''' 6 | Create a depth first search algorithm that creates an array of connected components 7 | Inputs: 8 | edges: list of tuples representing non-directed edges from vertices (vertices will be presented in order) 9 | n: number of vertices 10 | Output: 11 | List of lists representing all connected vertices 12 | ''' 13 | 14 | # Initialize required elements 15 | stack = [] 16 | seen = [0]*(n+1) 17 | d = {} 18 | connectedComponents = [[]] 19 | 20 | # Set up dictionary by preprocessing edges: 21 | for i in edges: 22 | if i[0] not in d: 23 | d[i[0]] = [i[1]] 24 | else: 25 | d[i[0]].append(i[1]) 26 | if i[1] not in d: 27 | d[i[1]] = [i[0]] 28 | else: 29 | d[i[1]].append(i[0]) 30 | 31 | 32 | # Loop through keys that are not yet seen: 33 | iterator = 0 34 | for key in d: 35 | if seen[key] == 0: 36 | connectedComponents[iterator].append(key) 37 | stack.append(key) 38 | seen[key] = 1 39 | # The main DFS algorithm 40 | while stack: 41 | vertice = stack.pop() 42 | 43 | for neighbor in d[vertice]: 44 | if seen[neighbor] == 0: 45 | stack.append(neighbor) 46 | seen[neighbor] = 1 47 | connectedComponents[iterator].append(neighbor) 48 | iterator += 1 49 | connectedComponents.append([]) 50 | 51 | return connectedComponents 52 | 53 | print(dfs([(1, 7), (1, 3), (7, 9), (5, 9), (4, 5), (6, 8), (2, 6)], 9)) 54 | 55 | # Classic implementation of breadth first search 56 | from collections import deque 57 | def bfs(edges, n, a, b): 58 | ''' 59 | Uses BFS to find shortest unweighted path in graph 60 | Inputs: 61 | edges: list of tuples representing non-directed edges from vertices (vertices will be presented in order) 62 | n: integer number of vertices 63 | a: integer starting location 64 | b: integer end location 65 | Output: 66 | Integer denoting the minimum number of steps taken, returns -1 if cannot be reached 67 | ''' 68 | # Initialize required elements 69 | queue = deque() 70 | seen = [0]*(n+1) 71 | d = {} 72 | 73 | # Set up dictionary by preprocessing edges: 74 | for i in edges: 75 | if i[0] not in d: 76 | d[i[0]] = [i[1]] 77 | else: 78 | d[i[0]].append(i[1]) 79 | if i[1] not in d: 80 | d[i[1]] = [i[0]] 81 | else: 82 | d[i[1]].append(i[0]) 83 | 84 | # Add starting position to queue, track number of steps 85 | queue.append((a, 0)) 86 | 87 | # Main bfs function 88 | while queue: 89 | key = queue.popleft() 90 | seen[key[0]] = 1 91 | for neighbor in d[key[0]]: 92 | if neighbor == b: 93 | return key[1] + 1 94 | if seen[neighbor] == 0: 95 | queue.append((neighbor, key[1]+1)) 96 | seen[neighbor] = 1 97 | return -1 98 | 99 | print(bfs([(1, 7), (1, 3), (7, 9), (5, 9), (4, 5), (6, 8), (2, 6), (1, 10), (11, 10), (4, 11)], 11, 6, 4)) 100 | 101 | # Classic implementation of Dijkstras 102 | import heapq 103 | def dijkstras(edges, weights, n, a, b): 104 | ''' 105 | Uses dijkstras to find shortest unweighted path in graph 106 | Inputs: 107 | edges: list of tuples representing non-directed edges from vertices (vertices will be presented in order) 108 | n: integer number of vertices 109 | a: integer starting location 110 | b: integer end location 111 | Output: 112 | Integer denoting the minimum number of steps taken, returns -1 if cannot be reached 113 | ''' 114 | 115 | # Use priority que instead of normal que 116 | pq = [] 117 | seen = [0]*(n+1) 118 | d = {} 119 | 120 | # Set up dictionary 121 | for i in range(len(edges)): 122 | if edges[i][0] not in d: 123 | d[edges[i][0]] = [(edges[i][1], weights[i])] 124 | else: 125 | d[edges[i][0]].append((edges[i][1], weights[i])) 126 | if edges[i][1] not in d: 127 | d[edges[i][1]] = [(edges[i][0], weights[i])] 128 | else: 129 | d[edges[i][1]].append((edges[i][0], weights[i])) 130 | 131 | heapq.heappush(pq, (0, a)) 132 | 133 | # Main body of function 134 | while pq: 135 | node = heapq.heappop(pq) 136 | vertice = node[1] 137 | seen[node[1]] = 1 138 | print(node) 139 | if node[1] == b: 140 | return node[0] 141 | for i in d[vertice]: 142 | if seen[i[0]] == 0: 143 | heapq.heappush(pq, (node[0]+i[1], i[0])) 144 | return -1 145 | 146 | print(dijkstras([(1, 7), (1, 3), (3, 7), (7, 9), (5, 9), (4, 5), (6, 8), (2, 6), (1, 10), (10, 4)], [9, 2, 4, 3, 3, 6, 5, 5, 5, 20], 10, 1, 4)) 147 | 148 | # Finding strongly connected components in a directed graph (cycles graph) 149 | def scc(): 150 | return 1 151 | 152 | # Find whether it is possible to get from A to B using Union Find (Disjoint Set) 153 | def mazeSolve(): 154 | return 0 155 | 156 | # Classic implementation of Prim's MST 157 | def prims(): 158 | return 1 159 | 160 | # Classic implementation of Floyd Warshall Alg for finding All Pairs shortest path 161 | def fw(): 162 | return 0 163 | 164 | # Find "bridges" in graph - nodes that are connect to a graph by only one edge 165 | def bridge(): 166 | return 1 167 | 168 | 169 | 170 | -------------------------------------------------------------------------------- /Algorithms/Useful_Algorithms.md: -------------------------------------------------------------------------------- 1 | ## List of Useful Algorithms 2 | 3 | #### Breadth First Search 4 | #### Depth First Search 5 | #### Dijkstra's Algorithms / A * Algorithm 6 | #### Prim's Algorithm 7 | #### Kruskal's Algorithm 8 | #### Greedy Algorithms 9 | #### Dynamic Programming Algorithms 10 | #### Merge Sort and Quick Sort 11 | #### Recursion 12 | #### Union Find Algorithm 13 | #### Tree Travesrsal Algorithms -------------------------------------------------------------------------------- /Data Structures/General/Arrays.md: -------------------------------------------------------------------------------- 1 | ## Arrays 2 | 3 | ### Runtimes 4 | 5 | Insertion: 6 | 7 | Beginning: O(n) - Would have to shift everything back to make space for the new element 8 | 9 | Middle: O(n) - See above 10 | 11 | End: O(1) - Easy to insert element at the end 12 | 13 | 14 | Deletion: 15 | 16 | Beginning: O(n) - Would have to shift everything forward to remove the gap 17 | 18 | Middle: O(n) - See above 19 | 20 | End: O(1) - Easy to remove element at the end 21 | 22 | Access: 23 | 24 | Always O(1) - Random Access Memory 25 | 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /Data Structures/General/HashTables.md: -------------------------------------------------------------------------------- 1 | ## Hash Tables 2 | 3 | Definitions: 4 | 5 | Cardinality: The number of possible outputs from the hash function 6 | 7 | Load factor: The number of total inputs used, divided by the cardinality of the hash function. How full the array is with chains. 8 | 9 | Collision: When two keys get hashed to the same cell in an array. -------------------------------------------------------------------------------- /Data Structures/General/LinkedList.md: -------------------------------------------------------------------------------- 1 | ## Linked Lists & Doubly Linked Lists 2 | 3 | ### Runtimes 4 | 5 | All runtimes are given for Doubly Linked Lists (they are much more efficient, but requires more memory)! 6 | 7 | 8 | Insertions: 9 | 10 | PushFront O(1) - Adds a new node to the front of the linked list, as simple as setting the head to new element 11 | 12 | PushBack O(1) - Adds a new node to the back of the linked list, just connect the new node 13 | 14 | AddBefore O(1) - Inserts new node before another specified node, reattach pointers 15 | 16 | AddAfter O(1) - Inserts new node after another specified node, reattach pointers 17 | 18 | 19 | Deletions: 20 | 21 | PopFront O(1) - Removes the front node, just disconnect the node from the list 22 | 23 | PopBack O(1) - Removes last node, just assign the next pointer of the second last node to null 24 | 25 | Erase O(n) - Finds the first node with the given value, and removes that node. Needs to traverse down list 26 | 27 | 28 | Other: 29 | 30 | isEmpty O(1) - Check if list is empty, see if head pointer is tail, or null 31 | 32 | Find O(n) - Check if element is in list, needs to traverse down list 33 | 34 | 35 | 36 | 37 | 38 | -------------------------------------------------------------------------------- /Data Structures/General/Stack&Queue.md: -------------------------------------------------------------------------------- 1 | ## Stacks 2 | 3 | Can be implemented with either arrays or singular linked lists with head node (for O(n) runtime) 4 | 5 | ### Runtimes 6 | 7 | Push - O(1): Add element to the back of array (or head of linked list) 8 | 9 | Pop - O(1): Remove last element of array (or head of linked list) 10 | 11 | Top - O(1): Return last element of array (or head of linked list) 12 | 13 | isEmpty - O(1): Returns whether the counter is 0 or not 14 | 15 | 16 | ## Queues 17 | 18 | Can be implemented with circular arrays or singular linked lists with head and tail node 19 | 20 | ### Runtimes 21 | 22 | Enqueue - O(1): Add element to the tail pointer of array (or tail of linked list), move tail pointer up 1 23 | 24 | Dequeue - O(1): Remove and return head pointer element of array (or head of linked list), move head pointer up 1 25 | 26 | isEmpty - O(1): Returns whether the tail pointer and head pointer are equal -------------------------------------------------------------------------------- /Data Structures/General/Trees.md: -------------------------------------------------------------------------------- 1 | ## Trees 2 | 3 | ### General Terminology 4 | 5 | A tree is either empty, or a node with child nodes that are also trees (recursive) 6 | 7 | *Ancester*: a node's ancestors are their parent node and parent of parent nodes 8 | 9 | *Descendant*: a node's descendants are all of their child nodes and children of child nodes 10 | 11 | *Root*: The top most node, is an ancestor of all other nodes in the tree and also all other node's are descendants of the root node 12 | 13 | *Siblings*: Nodes that share the same parent nodes - would also have the same ancesters 14 | 15 | *Leaf*: Node with no child nodes (bottom of tree) 16 | 17 | *Interior Nodes*: Non-leaves, has child nodes 18 | 19 | *Level*: A node's level is 1 + number of edges to the root node (can also think of as number of ancestors) 20 | 21 | *Height*: A node's height is 1 + number of branches until lowest descendant leaf (kind of opposite of level) 22 | 23 | *Forest*: A collection of trees 24 | 25 | A node contains: a key, list containing some number of child nodes (can be 0), and a parent (unless it's the root node) 26 | 27 | *Walking a Tree*: Traversing through the nodes of a tree in a particular order 28 | 29 | *Breadth First Traversal*: Traversing by going through all the nodes in the same level before moving on to all the nodes in the next level. Can be implemented using Queues 30 | 31 | *Depth First Traversal*: Traversing by going down all the descendants before moving on to sibling nodes. Can be implemented using Stacks 32 | 33 | Ordering for Possible Depth First Search Traversal: 34 | 35 | *In Order Traversal*: Left node, parent node, right node 36 | 37 | *Pre Order Traversal*: Parent node, left node, right node 38 | 39 | *Post Order Traversal*: Left node, right node, parent node 40 | 41 | ### Types of Trees 42 | 43 | There's literally like a million different types of trees, but most commonly used tree structures can be split into four subcategories: 44 | 45 | 1. Binary Trees - trees in which every node has at most two child nodes. Examples (Binary search tree, AVL trees, Red-black trees) 46 | 47 | 2. Heaps - trees in which the parent node always contains a value that is greater than that of it's child nodes. 48 | 49 | 3. Multi Branch Trees - trees in which any node can have any number of child nodes. 50 | 51 | Trees are very flexible data structure in which their type and implementation can vary greatly depending on the question or problem they are used to solve. 52 | 53 | 54 | 55 | -------------------------------------------------------------------------------- /Data Structures/Java/AVLTree.java: -------------------------------------------------------------------------------- 1 | public class AVLTree { 2 | 3 | Node root; 4 | 5 | // Constructor 6 | public AVLTree(){ 7 | } 8 | 9 | // function to insert new node 10 | public void insert(float val){ 11 | if (!this.root){ 12 | this.root = new Node(val); 13 | this.root.parent = null; 14 | } 15 | } 16 | 17 | // function to remove a node 18 | public void remove(){ 19 | 20 | } 21 | 22 | // function to find and return a node 23 | public Node find(){ 24 | 25 | } 26 | 27 | // Checks for balance when node is inserted / deleted 28 | private boolean isBalanced(Node n){ 29 | return true; 30 | } 31 | 32 | // Custom node class 33 | private class Node { 34 | Node leftChild; 35 | Node rightChild; 36 | Node parent; 37 | boolean isLeaf; 38 | int leftChildCount = 0; 39 | int rightChildCount = 0; 40 | float val; 41 | public Node(float _val) { 42 | this.val = _val; 43 | } 44 | } 45 | 46 | } -------------------------------------------------------------------------------- /Data Structures/Java/BinarySearchTree.java: -------------------------------------------------------------------------------- 1 | public class BinarySearchTree{ 2 | 3 | private Node root = null; 4 | 5 | public BinarySearchTree(){ 6 | } 7 | 8 | public void insert(E value){ 9 | if (this.root == null){ 10 | this.root = new Node(value); 11 | } else { 12 | this.insert(value, this.root); 13 | } 14 | } 15 | 16 | public void insert(E value, Node curr){ 17 | if (value <= curr.value && curr.left == null){ 18 | curr.left = new Node(value); 19 | curr.left.parent = curr; 20 | } else if (value <= curr.value){ 21 | this.insert(value, curr.left); 22 | } else if (value > curr.value && curr.right == null){ 23 | curr.right = new Node(value); 24 | curr.right.parent = curr; 25 | } else { 26 | this.insert(value, curr.right); 27 | } 28 | } 29 | 30 | public void delete(E value){ 31 | this.delete(value, this.root); 32 | } 33 | 34 | public void delete(E value, Node curr){ 35 | if (curr.value == value){ 36 | this.findSuccessor(); 37 | System.out.println("Value removed, tree altered"); 38 | } else { 39 | if (value < curr.value && curr.left == null){ 40 | System.out.println("Value not found"); 41 | } else if (value < curr.value){ 42 | this.Find(value, curr.left); 43 | } else if (value > curr.value && curr.right == null){ 44 | System.out.println("Value not found"); 45 | } else { 46 | this.Find(value, curr.right); 47 | } 48 | } 49 | 50 | } 51 | 52 | public void findSuccessor(Node curr){ 53 | if (curr.isLeaf()){ 54 | if (curr.isLeaf()){ 55 | if (curr.parent != null && curr.parent.left == curr){ 56 | curr.parent.left = null; 57 | } else if (curr.parent != null){ 58 | curr.parent.right = null; 59 | } else { 60 | this.root = null; 61 | } 62 | } 63 | } else if (curr.right!=null && curr.left ==null){ 64 | if (curr = this.root){ 65 | this.root = curr.right; 66 | this.root.parent = null; 67 | } else { 68 | curr.parent.right = curr.right; 69 | curr.right.parent = curr.parent; 70 | } 71 | } else if (curr.right==null && curr.left!=null){ 72 | if (curr = this.root){ 73 | this.root = curr.left; 74 | this.root.parent = null; 75 | } else { 76 | curr.parent.left = curr.left; 77 | curr.left.parent = curr.parent; 78 | } 79 | } else { 80 | this.findSuccessor(curr.right); 81 | } 82 | } 83 | 84 | public boolean Find(E value){ 85 | return this.Find(value, this.root); 86 | } 87 | 88 | public boolean Find(E value, Node curr){ 89 | if (curr.value == value){ 90 | return true; 91 | } else { 92 | if (value < curr.value && curr.left == null){ 93 | return false; 94 | } else if (value < curr.value){ 95 | return this.Find(value, curr.left); 96 | } else if (value > curr.value && curr.right == null){ 97 | return false; 98 | } else { 99 | return this.Find(value, curr.right); 100 | } 101 | } 102 | } 103 | 104 | public String toString(){ 105 | String temp = ""; 106 | Node curr = this.root; 107 | temp = this.inOrderTraversal(curr); 108 | return temp; 109 | } 110 | 111 | private String inOrderTraversal(Node curr){ 112 | String temp = ""; 113 | if (curr.left != null){ 114 | this.inOrderTraversal(curr.left); 115 | } 116 | 117 | temp += curr.value; 118 | temp += " "; 119 | 120 | if (curr.right != null){ 121 | this.inOrderTraversal(curr.right); 122 | } 123 | return temp; 124 | } 125 | 126 | 127 | private class Node { 128 | public Node left; 129 | public Node right; 130 | public Node parent; 131 | public E value; 132 | 133 | public Node(E _value){ 134 | this.parent = null; 135 | this.left = null; 136 | this.right = null; 137 | this.value = _value; 138 | } 139 | 140 | public boolean isLeaf(){ 141 | return this.left != null && this.right != null; 142 | } 143 | } 144 | } -------------------------------------------------------------------------------- /Data Structures/Java/DisjointSet.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/l1ghtspeed/Programming-Notes/0ed0df7382f30512254438c08ce1b3081a9d3252/Data Structures/Java/DisjointSet.class -------------------------------------------------------------------------------- /Data Structures/Java/DisjointSet.java: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Disjoint set data structure using forest of tree data structures - makes it easy to keep id of head 4 | 5 | Tree data structure implemented using array to save time and space complexity 6 | 7 | One way to solve if you can get from point A to point B in a Maze. 8 | 9 | */ 10 | 11 | 12 | public class DisjointSet{ 13 | public int[] rank; 14 | public int[] forest; 15 | 16 | public DisjointSet(int size){ 17 | this.rank = new int[size]; 18 | this.forest = new int[size]; 19 | } 20 | 21 | public void Union(int t1, int t2){ 22 | int t1Parent = this.Find(t1); 23 | int t2Parent = this.Find(t2); 24 | if (t1Parent != t2Parent){ 25 | if (this.rank[t1Parent] > this.rank[t2Parent]){ 26 | this.forest[t2Parent] = t1Parent; 27 | } else { 28 | this.forest[t1Parent] = t2Parent; 29 | if (this.rank[t1Parent] == this.rank[t2Parent]){ 30 | this.rank[t2Parent] += 1; 31 | } 32 | } 33 | } 34 | } 35 | 36 | public void MakeSet(int value){ 37 | this.forest[value] = value; 38 | this.rank[value] = 1; 39 | } 40 | 41 | public int Find(int value){ 42 | if (this.forest[value] != value){ 43 | this.forest[value] = this.Find(this.forest[value]); 44 | } 45 | return this.forest[value]; 46 | } 47 | 48 | public String toString(){ 49 | String temp = ""; 50 | for (int i = 0; i < this.forest.length; i++){ 51 | temp += "(" + i + ", " + this.forest[i] + ") "; 52 | } 53 | return temp; 54 | } 55 | 56 | } 57 | 58 | -------------------------------------------------------------------------------- /Data Structures/Java/DynamicArray.java: -------------------------------------------------------------------------------- 1 | public class DynamicArray { 2 | 3 | private int[] arr; 4 | 5 | public DynamicArray(int size) { 6 | this.arr = new int[size]; 7 | } 8 | 9 | public void insert(int i, int value){ 10 | if (i >= this.arr.length) { 11 | this.increase(); 12 | } 13 | this.arr[i] = value; 14 | } 15 | 16 | public int getValue(int i){ 17 | return this.arr[i]; 18 | } 19 | 20 | private void increase(){ 21 | int[] temp = this.arr; 22 | this.arr = new int[temp.length*2]; 23 | for (int i = 0; i < temp.length; i++){ 24 | this.arr[i] = temp[i]; 25 | } 26 | } 27 | 28 | } 29 | 30 | -------------------------------------------------------------------------------- /Data Structures/Java/HashMap.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/l1ghtspeed/Programming-Notes/0ed0df7382f30512254438c08ce1b3081a9d3252/Data Structures/Java/HashMap.java -------------------------------------------------------------------------------- /Data Structures/Java/LinkedList.java: -------------------------------------------------------------------------------- 1 | //Doubly Linked List Example 2 | 3 | public class LinkedList { 4 | 5 | private Node head = new Node(); 6 | private Node tail = new Node(); 7 | 8 | public LinkedList(){ 9 | this.head.next = this.tail; 10 | this.tail.previous = this.head; 11 | } 12 | 13 | public void pushFront(E value){ 14 | if (this.head.next == this.tail) { 15 | Node temp = new Node(this.head, this.tail, value); 16 | head.setNext(temp); 17 | tail.setPrevious(temp); 18 | } else { 19 | Node temp = new Node(this.head, this.head.next, value); 20 | head.next.setPrevious(temp); 21 | head.setNext(temp); 22 | } 23 | } 24 | 25 | public void pushBack(E value){ 26 | if (this.tail.previous == this.head) { 27 | Node temp = new Node(this.head, this.tail, value); 28 | head.setNext(temp); 29 | tail.setPrevious(temp); 30 | } else { 31 | Node temp = new Node(this.tail.previous, this.tail, value); 32 | tail.previous.setNext(temp); 33 | tail.setPrevious(temp); 34 | } 35 | } 36 | 37 | public void popFront(){ 38 | if (this.head.next != this.tail && this.head.next.next != this.tail){ 39 | this.head.next = this.head.next.next; 40 | this.head.next.previous = this.head; 41 | } else if (this.head.next != null){ 42 | this.head.next = this.tail; 43 | this.tail.previous = this.head; 44 | } 45 | } 46 | 47 | public void popBack(){ 48 | if (this.tail.previous != null && this.tail.previous.previous != null){ 49 | this.tail.previous = this.tail.previous.previous; 50 | this.tail.previous.next = this.tail; 51 | } else if (this.tail.previous != null){ 52 | this.tail.previous = this.head; 53 | this.head.next = this.tail; 54 | } 55 | } 56 | 57 | public boolean isEmpty(){ 58 | if (this.head.next == this.tail){ 59 | return true; 60 | } 61 | return false; 62 | } 63 | 64 | public void Erase(E value){ 65 | Node curr = this.head; 66 | while (curr.next != this.tail){ 67 | System.out.println(2); 68 | if (curr.next.value == value){ 69 | if (curr.next.next != this.tail){ 70 | curr.next.next.previous = curr; 71 | curr.next = curr.next.next; 72 | break; 73 | } else { 74 | curr.next = this.tail; 75 | break; 76 | } 77 | } 78 | curr = curr.next; 79 | } 80 | } 81 | 82 | public String toString(){ 83 | String holder = ""; 84 | Node curr = this.head; 85 | 86 | while(curr.next != this.tail){ 87 | holder += curr.next.value; 88 | holder += " "; 89 | curr = curr.next; 90 | } 91 | 92 | return holder; 93 | } 94 | 95 | private class Node { 96 | 97 | public Node previous; 98 | public Node next; 99 | public E value; 100 | 101 | public Node(Node _previous, Node _next, E _value){ 102 | 103 | this.previous = _previous; 104 | this.next = _next; 105 | this.value = _value; 106 | 107 | } 108 | 109 | 110 | public Node(){ 111 | this.previous = null; 112 | this.next = null; 113 | this.value = null; 114 | } 115 | 116 | /** 117 | * @param next the next to set 118 | */ 119 | public void setNext(Node _next) { 120 | this.next = _next; 121 | } 122 | 123 | public void setPrevious(Node _previous){ 124 | this.previous = _previous; 125 | } 126 | } 127 | } 128 | -------------------------------------------------------------------------------- /Data Structures/Java/PriorityQueue.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/l1ghtspeed/Programming-Notes/0ed0df7382f30512254438c08ce1b3081a9d3252/Data Structures/Java/PriorityQueue.class -------------------------------------------------------------------------------- /Data Structures/Java/PriorityQueue.java: -------------------------------------------------------------------------------- 1 | /* Priority Queue Implementation using Heaps (Binary tree such that parent is always >= children) 2 | 3 | Insertion is random, we do not care, but extract Max always returns the greatest node 4 | 5 | Priority ques are very important, and can be used for a variety of things, such as load balancing and Dijkstras 6 | 7 | */ 8 | 9 | public class PriorityQueue { 10 | /* Remember to make complete trees using pointer to keep track of latest leaf */ 11 | private int[] arr; 12 | private int maxSize = 4; 13 | private int currentSize = 0; 14 | 15 | public PriorityQueue(){ 16 | this.arr = new int[maxSize]; 17 | this.arr[0] = 0; 18 | } 19 | 20 | public int extractMax(){ 21 | int temp = this.arr[1]; 22 | this.arr[1] = this.arr[currentSize]; 23 | this.currentSize-=1; 24 | this.siftdown(1); 25 | return temp; 26 | } 27 | 28 | public int getMax(){ 29 | return arr[1]; 30 | } 31 | 32 | public void insert(int value){ 33 | if (this.currentSize == this.maxSize-1){ 34 | this.enlarge(); 35 | } 36 | if(this.arr[0] <= value){ 37 | this.arr[0] = value+1; 38 | } 39 | this.arr[this.currentSize+1] = value; 40 | this.currentSize++; 41 | if (this.isLeft(this.currentSize)){ 42 | this.siftup(this.currentSize, this.currentSize/2); 43 | } else { 44 | this.siftup(this.currentSize, (this.currentSize-1)/2); 45 | } 46 | } 47 | 48 | private void siftup(int child_id, int parent_id){ 49 | if(this.arr[child_id] > this.arr[parent_id]){ 50 | int temp = this.arr[parent_id]; 51 | this.arr[parent_id] = this.arr[child_id]; 52 | this.arr[child_id] = temp; 53 | if (this.isLeft(child_id)){ 54 | this.siftup(parent_id, parent_id/2); 55 | } else { 56 | this.siftup(parent_id, (parent_id)/2); 57 | } 58 | } 59 | } 60 | 61 | private void siftdown(int id){ 62 | if (id*2+1 <= this.currentSize){ 63 | if (this.leftIsBigger(id)){ 64 | if (this.arr[id] < this.arr[id*2]){ 65 | int temp = this.arr[id*2]; 66 | this.arr[id*2] = this.arr[id]; 67 | this.arr[id] = temp; 68 | this.siftdown(id*2); 69 | } 70 | } else { 71 | if (this.arr[id] < this.arr[id*2+1]){ 72 | int temp = this.arr[id*2+1]; 73 | this.arr[id*2+1] = this.arr[id]; 74 | this.arr[id] = temp; 75 | this.siftdown(id*2+1); 76 | } 77 | } 78 | } else if (id*2 <= this.currentSize) { 79 | if (this.arr[id] < this.arr[id*2]){ 80 | int temp = this.arr[id*2]; 81 | this.arr[id*2] = this.arr[id]; 82 | this.arr[id] = temp; 83 | this.siftdown(id*2); 84 | } 85 | } 86 | } 87 | 88 | public String toString(){ 89 | String temp = ""; 90 | for (int i = 1; i <= this.currentSize; i++){ 91 | temp+= this.arr[i]; 92 | temp+= " "; 93 | } 94 | return temp; 95 | } 96 | 97 | private void enlarge(){ 98 | int[] temp = this.arr; 99 | this.arr = new int[temp.length*2]; 100 | this.maxSize = temp.length*2; 101 | for (int i = 0; i < temp.length; i++){ 102 | this.arr[i] = temp[i]; 103 | } 104 | } 105 | 106 | private boolean isLeft(int id){ 107 | return (id & 1) == 0; 108 | } 109 | 110 | private boolean leftIsBigger(int id){ 111 | return this.arr[id*2] >= this.arr[id*2+1]; 112 | } 113 | 114 | } -------------------------------------------------------------------------------- /Data Structures/Java/Queue.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/l1ghtspeed/Programming-Notes/0ed0df7382f30512254438c08ce1b3081a9d3252/Data Structures/Java/Queue.class -------------------------------------------------------------------------------- /Data Structures/Java/Queue.java: -------------------------------------------------------------------------------- 1 | public class Queue { 2 | 3 | private int[] queue; 4 | private int head = -1; 5 | private int tail = 0; 6 | private boolean filled = false; 7 | 8 | public Queue(int size){ 9 | this.queue = new int[size]; 10 | } 11 | 12 | public void enqueue(int value){ 13 | this.head++; 14 | if (this.head >= this.queue.length && this.tail > 0){ 15 | this.head = 0; 16 | } else if (this.head >= this.queue.length){ 17 | increase(false); 18 | } else if (this.head == this.tail && this.filled == true){ 19 | increase(true); 20 | } 21 | this.queue[this.head] = value; 22 | this.filled = true; 23 | } 24 | 25 | public void dequeue(){ 26 | if (this.head != this.tail){ 27 | this.queue[this.tail] = 0; 28 | this.tail++; 29 | if (this.head == this.tail){ 30 | this.filled = false; 31 | } 32 | if (this.tail >= this.queue.length){ 33 | this.tail = 0; 34 | } 35 | } 36 | } 37 | 38 | 39 | public int peek(){ 40 | return this.queue[this.tail]; 41 | } 42 | 43 | public boolean isEmpty(){ 44 | return this.head == this.tail; 45 | } 46 | 47 | private void increase(boolean state){ 48 | int[] curr = this.queue; 49 | this.queue = new int[curr.length*2]; 50 | if (state){ 51 | for (int i = 0; i < this.head; i++) { 52 | this.queue[i] = curr[i]; 53 | } 54 | for (int i = curr.length+this.tail; i < this.queue.length; i++){ 55 | this.queue[i] = curr[i-curr.length]; 56 | } 57 | } else { 58 | for (int i = 0; i < curr.length; i++){ 59 | this.queue[i] = curr[i]; 60 | } 61 | } 62 | } 63 | 64 | public String toString(){ 65 | String temp = ""; 66 | System.out.println(this.tail); 67 | System.out.println(this.head); 68 | if (this.head < this.tail){ 69 | for (int i = 0; i < this.head+1; i++){ 70 | temp += this.queue[i]; 71 | temp += " "; 72 | } 73 | for (int i = this.tail; i < this.queue.length; i++){ 74 | temp += this.queue[i]; 75 | temp += " "; 76 | } 77 | } else { 78 | for (int i = this.tail; i < this.head+1; i++){ 79 | temp += this.queue[i]; 80 | temp += " "; 81 | } 82 | } 83 | return temp; 84 | } 85 | 86 | } -------------------------------------------------------------------------------- /Data Structures/Java/Stack.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/l1ghtspeed/Programming-Notes/0ed0df7382f30512254438c08ce1b3081a9d3252/Data Structures/Java/Stack.class -------------------------------------------------------------------------------- /Data Structures/Java/Stack.java: -------------------------------------------------------------------------------- 1 | //Simple Array implementation of Stack 2 | 3 | public class Stack { 4 | 5 | private int[] stack = new int[8]; 6 | private int counter = -1; 7 | 8 | public Stack(){ 9 | } 10 | 11 | public void push(int value){ 12 | 13 | counter++; 14 | 15 | if (counter == stack.length){ 16 | this.increaseStack(); 17 | } 18 | 19 | stack[counter] = value; 20 | 21 | 22 | } 23 | 24 | public void pop(){ 25 | if (counter != 0){ 26 | stack[counter] = 0; 27 | counter--; 28 | } 29 | } 30 | 31 | public int peek(){ 32 | return stack[counter]; 33 | } 34 | 35 | public boolean isEmpty(){ 36 | return counter==0; 37 | } 38 | 39 | private void increaseStack(){ 40 | int[] oldStack = this.stack; 41 | this.stack = new int[oldStack.length*2]; 42 | for (int i = 0; i < oldStack.length; i++){ 43 | this.stack[i] = oldStack[i]; 44 | } 45 | } 46 | 47 | public String toString(){ 48 | String holder = ""; 49 | for (int i = 0; i < this.counter+1; i++){ 50 | holder+=this.stack[i]; 51 | holder+=" " 52 | } 53 | return holder; 54 | } 55 | 56 | } -------------------------------------------------------------------------------- /Data Structures/Java/Test.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/l1ghtspeed/Programming-Notes/0ed0df7382f30512254438c08ce1b3081a9d3252/Data Structures/Java/Test.class -------------------------------------------------------------------------------- /Data Structures/Java/Test.java: -------------------------------------------------------------------------------- 1 | public class Test { 2 | 3 | public static void main (String[] args){ 4 | int dsetsize = 10; 5 | DisjointSet dset = new DisjointSet(dsetsize); 6 | for (int i = 0; i < dsetsize; i++){ 7 | dset.MakeSet(i); 8 | } 9 | System.out.println(dset); 10 | dset.Union(2, 3); 11 | System.out.println(dset); 12 | dset.Union(2, 9); 13 | System.out.println(dset); 14 | dset.Union(5, 1); 15 | System.out.println(dset); 16 | dset.Union(5, 3); 17 | System.out.println(dset); 18 | System.out.println(dset.Find(5)); 19 | System.out.println(dset); 20 | 21 | } 22 | } 23 | 24 | -------------------------------------------------------------------------------- /Data Structures/Python/Arrays.py: -------------------------------------------------------------------------------- 1 | a = [2, 5, 3] 2 | b = [] 3 | 4 | -------------------------------------------------------------------------------- /Data Structures/Python/LinkedList.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/l1ghtspeed/Programming-Notes/0ed0df7382f30512254438c08ce1b3081a9d3252/Data Structures/Python/LinkedList.py -------------------------------------------------------------------------------- /Data Structures/Python/Queue.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/l1ghtspeed/Programming-Notes/0ed0df7382f30512254438c08ce1b3081a9d3252/Data Structures/Python/Queue.py -------------------------------------------------------------------------------- /Data Structures/Python/Stack.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/l1ghtspeed/Programming-Notes/0ed0df7382f30512254438c08ce1b3081a9d3252/Data Structures/Python/Stack.py -------------------------------------------------------------------------------- /Data Structures/Python/Trie.py: -------------------------------------------------------------------------------- 1 | from collections import deque 2 | class Trie(object): 3 | 4 | def __init__(self): 5 | """ 6 | Initialize your data structure here. 7 | """ 8 | self.root = Node('') 9 | 10 | def insert(self, word): 11 | """ 12 | Inserts a word into the trie. 13 | :type word: str 14 | :rtype: void 15 | """ 16 | node = self.root 17 | for c in word: 18 | if c in node.children: 19 | node = node.children[c] 20 | else: 21 | node.children[c] = Node(node.val+c) 22 | node = node.children[c] 23 | node.isWord = True 24 | 25 | def search(self, word): 26 | """ 27 | Returns if the word is in the trie. 28 | :type word: str 29 | :rtype: bool 30 | """ 31 | node = self.root 32 | for c in word: 33 | if c in node.children: 34 | node = node.children[c] 35 | else: 36 | return False 37 | return node.isWord 38 | 39 | def startsWith(self, prefix): 40 | """ 41 | Returns if there is any word in the trie that starts with the given prefix. 42 | :type prefix: str 43 | :rtype: bool 44 | """ 45 | node = self.root 46 | for c in prefix: 47 | if c in node.children: 48 | node = node.children[c] 49 | else: 50 | return False 51 | return True 52 | 53 | def __str__(self): 54 | """ 55 | Uses breadth first search to print out all elems and child nodes of Trie 56 | :rtype: string 57 | """ 58 | s = '' 59 | que = deque() 60 | que.append(self.root) 61 | while que: 62 | node = que.popleft() 63 | for key in node.children: 64 | que.append(node.children[key]) 65 | s += node.val 66 | print(s) 67 | return s 68 | 69 | 70 | 71 | class Node(object): 72 | def __init__(self, _val): 73 | """ 74 | Initialize your data structure here. 75 | """ 76 | self.val = _val 77 | self.children = {} 78 | self.isWord = False 79 | 80 | tr = Trie() 81 | tr.insert('hello') 82 | print(tr.startsWith('e')) 83 | 84 | 85 | 86 | -------------------------------------------------------------------------------- /Data Structures/Useful_Data_Structures.md: -------------------------------------------------------------------------------- 1 | # List of Useful Data Structures 2 | 3 | #### Arrays: 4 | - Basically all data structures are eventually stored as array 5 | - Most efficient lookup speed, efficient storage 6 | - Used for all kinds of problems, basic and complex 7 | - Inefficient search O(n) 8 | 9 | #### Linked Lists: 10 | - Useful for representing sequential access, and O(1) insertion 11 | - Flexible information storage, does not have to be strictly typed 12 | - Extra use of space due to pointers stored and O(n) search time 13 | 14 | #### Stacks: 15 | - Various real life representations for first in last out 16 | - Used in a bunch of graph and tree traversal algorithms 17 | - Fast insertion and removal of top item 18 | - Extra use of space due to pointers, O(n) search time 19 | 20 | #### Queues: 21 | - Various real life representations for first in first out 22 | - Used in a bunch of graph and tree traversal algorithms 23 | - Fast insertion and removal of last item 24 | - Extra use of space due to pointers, O(n) search time 25 | 26 | #### Binary Trees / N-ary Trees: 27 | - Allows for very fast search 28 | - Has relationships that model real life relationships (e.g. parent and child nodes) 29 | - Storage can be in an array to save space 30 | 31 | #### Tries 32 | - Lots of practical usages, such as autocomplete, genome analysis, data analytics 33 | 34 | #### Disjoint Sets 35 | - Super fast lookup time for group it belongs in 36 | - Real life applications such as finding minimum spanning trees, connectivity between nodes 37 | 38 | #### Graphs 39 | - Perhaps the data structure that has the most number of real life applications 40 | - Variety of efficient algorithms to store and process data using graphs 41 | 42 | #### Hash Structures 43 | - Super useful mainly for efficient look up, O(1), and establishing relationships between variables 44 | - Hash functions can be cool -------------------------------------------------------------------------------- /Frameworks/ReactJS/Introduction.md: -------------------------------------------------------------------------------- 1 | # Introduction - What is ReactJS and Why Use it? 2 | 3 | ReactJS, usually just referred to as 'React', is a front-end framework mainly used for making webapp user interfaces, created by developers at Facebook. There are many different front end frameworks out there (Angular, Vue), but React is one of the most popular. Why is that? 4 | 5 | Here are some reasons: 6 | 7 | 1. It is lightweight. It isn't overloaded with different functionalities, but still has powerful tools to do whats needed. 8 | 9 | 2. It is fast. React has a special way of updating front end interfaces that makes it much faster to relay changes in the state of the views. 10 | 11 | 3. It is scalable. React is modular, meaning it is easy to break down large pages into small components and reuse them if necessary. 12 | 13 | 4. It has lots of support, due to it's popularity, so problems you come into are more easily solved. 14 | 15 | 16 | If you're looking to get into web development, whether it's front end, back end, fullstack, or UI design, ReactJS is a very handy framework to learn. It is relatively easy to get started with, the only pre-requisite needed is some familiarity with Javascript (especially ES6 features). And while not necessary, it is also very helpful to have prior experience with HTML, CSS, and other more general web development concepts (E.G AJAX, REST). 17 | 18 | -------------------------------------------------------------------------------- /Frameworks/ReactJS/jsxBasicOverview.jsx: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | JSX is a pivotal part of React. It is a syntax extension - meaning it is not actual Javascript, but it can get compiled down to Javascript. 4 | 5 | It looks very similar to HTML, so when you see HTML-like elements in a React project, you'll know its JSX. Take a look at the code below. 6 | 7 | The 'const header' and ';' is normal JS but everything between the header tags is JSX. 8 | One complete JSX statement (including the tags) is known as a JSX element. 9 | 10 | */ 11 | 12 | const header =

This h1 element is JSX!

; 13 | 14 | /* 15 | 16 | JSX elements can be used in any way other Javascript data types can be used. 17 | E.g. Passed to a function, be part of an array. 18 | 19 | */ 20 | 21 | let list_Of_Superheros = [ 22 |
  • Ironman
  • , 23 |
  • Captain America
  • , 24 |
  • Hulk
  • , 25 |
  • Thor
  • 26 | ]; 27 | 28 | 29 | /* 30 | 31 | JSX elements, just like HTML, can have attributes. 32 | The attribute for 'class' in HTML is 'className' in JSX. 33 | This is because 'class' is already a reserved keyword in Javascript! 34 | 35 | */ 36 | 37 | var list_Of_Villains = [ 38 |
  • Thanos
  • , 39 |
  • Ultron
  • , 40 |
  • Joker
  • , 41 |
  • Darkseid
  • 42 | ]; 43 | 44 | 45 | /* 46 | 47 | Also like HTML elements, JSX elements can be nested. 48 | However, unless you write everything on one line, you must use brackets around them. 49 | Another thing - there can only be one highest layer JSX element. 50 | It's an easy problem to avoid, just wrap the multiple highest layer elements in a div. 51 | 52 | E.g. This won't compile properly: 53 | 54 | let foo = ( 55 |

    bar

    56 |

    bar

    57 | ); 58 | 59 | The example below will work though, since both elements are wrapped inside a parent div 60 | 61 | */ 62 | 63 | let image_links = ( 64 | 72 | ); 73 | 74 | 75 | /* 76 | 77 | You can insert normal Javascript into JSX, but it must be done within a set of curly brackets {}. 78 | They are commonly used to modify the content of a JSX element or an attribute of a JSX element. 79 | 80 | */ 81 | 82 | let favourite_movie = { 83 | name: 'Avengers Infinity War', 84 | link: 'https://www.imdb.com/title/tt4154756/' 85 | } 86 | 87 | let movie_and_review = My favourite movie is: {favourite_movie.name}; 88 | 89 | 90 | 91 | /* 92 | 93 | Event listeners can be added as attributes to JSX elements. 94 | For a full list of supported events visit this link: https://reactjs.org/docs/events.html#supported-events 95 | 96 | */ 97 | 98 | var hero_image_path = '../src/ironman.png'; 99 | var changePicture = () => {hero_image_path = '../src/batman.png'}; 100 | 101 | let super_hero_image = ; 107 | 108 | 109 | 110 | /* 111 | 112 | We can use conditionals along with JSX, but we cannot inject 'if/else' statements directly into the JSX elements. 113 | You can either use the ternary and '&&' statements inside the JSX element, or resort to putting the if/else statement outside. 114 | 115 | */ 116 | 117 | let today_is_monday = false; 118 | let have_coffee = true; 119 | 120 | let day; 121 | 122 | if (today_is_monday){ 123 | day = 'today is a horrible day'; 124 | } else { 125 | day = 'today is a great day'; 126 | } 127 | 128 | let good_or_bad_day =

    129 | 130 | {day}, {have_coffee ? 'but I will be productive' : 'and I will not be productive'} 131 | 132 |

    ; 133 | 134 | 135 | 136 | 137 | /* 138 | 139 | JSX gets rendered to a webpage via the 'render()' method from the ReactDOM library. 140 | Remember to install the correct dependencies using npm install. 141 | 142 | The render() method takes in two parameters - the JSX element, and an actual HTML element from the html file you are serving. 143 | 144 | The render() is one reason that makes React so special - it only updates the DOM elements that are changed, instead of the whole page. 145 | This makes the process a lot faster. I encourage you to read through this article https://www.codecademy.com/articles/react-virtual-dom 146 | 147 | */ 148 | 149 | import React from 'react'; 150 | import ReactDOM from 'react-dom'; 151 | 152 | ReactDOM.render(

    Some Heros and Villains

    , document.getElementById("header")); 153 | ReactDOM.render(

    Heros: {list_Of_Superheros}

    , document.getElementById("hero-container")); 154 | ReactDOM.render(

    Villains: {list_Of_Villains}

    , document.getElementById("villain-container")); 155 | ReactDOM.render(super_hero_image, document.getElementById("links")); 156 | 157 | 158 | 159 | 160 | 161 | 162 | -------------------------------------------------------------------------------- /Frameworks/ReactJS/reactOverview.jsx: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | OK Now we get to some meatier and more meaningful features of React 4 | What's required: decent understanding of JSX, Javascript, ES6, HTML, CSS 5 | 6 | */ 7 | 8 | 9 | /* 10 | 11 | Components: 12 | 13 | A React component is essentially a chunk of reuseable code. 14 | Think of the views you would have to build for a social media app - many different screens will require the same menu bar at the bottom. 15 | It would be much more convenient to just slide the same component in, instead of copy pasting it again. 16 | 17 | Components are also super nice because they allow you to easily insert JS logic into them. 18 | No longer will you have to have annoying script tags or have switch back between the script.js. 19 | 20 | */ 21 | 22 | import React from 'react'; 23 | import ReactDOM from 'react-dom'; 24 | 25 | class Automobile extends React.Component{ 26 | changePerson(person){ 27 | person='Steve'; 28 | } 29 | 30 | render(){ 31 | let person = 'Bob'; 32 | const car = { 33 | name: person=='Bob' ? 'BMW M4' : 'Mercedes E class', 34 | type: person=='Bob' ? 'Coupe' : 'Sedan', 35 | url: person=='Bob' ? '../src/bmw.png' : '../src/mercedes.png' 36 | } 37 | return ( 38 |
    39 |

    40 | I have the {car.name} {car.type} as my car. 41 |

    42 | {car.name}/ 43 |
    44 | ); 45 | } 46 | } 47 | 48 | ReactDOM.render(, document.getElementById('app')); 49 | 50 | /* 51 | 52 | Components can render other components! Even components from a different source file. 53 | As long as the variables you want to use are exported from the other source file, you can import them over. 54 | 55 | */ 56 | 57 | import { HeaderBar } from '../../Examples/reactapp/HeaderFile.jsx'; 58 | //imports footerbar class and pageNumber variable to use 59 | import { FooterBar, pageNumber } from '../../Examples/reactapp/FooterFile.jsx'; 60 | 61 | class MainSite extends React.Component { 62 | render(){ 63 | return ( 64 |
    65 | 66 | 67 |
    68 | ) 69 | } 70 | } 71 | 72 | ReactDOM.render(, document.getElementById('app')); 73 | 74 | /* 75 | 76 | Props: 77 | 78 | We've now goten to the second thing that makes React special. 79 | 'props' is an object used to pass information between components. 80 | 'this.props' contains a bunch of information about the component. 81 | 82 | Props can be used in many ways - you can pass functions through props such as event handlers. 83 | You can view all the child elements of a component instance. 84 | You can set default props if nothing else if passed in (useful for user inputs). 85 | 86 | */ 87 | 88 | class Login extends React.Component { 89 | render(){ 90 | return

    My name is {this.props.info.name}, and my passcode is {this.props.info.passcode}

    91 | } 92 | } 93 | 94 | ReactDOM.render( 95 | , 96 | document.getElementById('app') 97 | ); 98 | 99 | 100 | class Button extends React.Component{ 101 | render(){ 102 | return 103 | } 104 | } 105 | 106 | let alertButtonPress = () => {alert('the button has been pressed!')}; 107 | 108 | ReactDOM.render(, document.getElementById('app')); 109 | 110 | 111 | /* 112 | 113 | State: 114 | 115 | this.state is what is used to change and update a components state variables. 116 | Props are between components, state is within a component. 117 | State can only be changed with a this.setState function, but it also automatically calls the render() method. 118 | You need to wrap this.setState in another function, and re-bind this to that function. 119 | 120 | */ 121 | 122 | class MagicTrick extends React.Component { 123 | 124 | constructor(props){ 125 | super(props); 126 | this.state = { color: 'blue', colorcode: 'rgb(0, 0, 255)' }; 127 | this.changeColor = this.changeColor.bind(this); 128 | } 129 | 130 | changeColor(){ 131 | let prevColor = this.state.color; 132 | if (this.state.color == 'blue'){ 133 | this.setState({ color: 'red', colorcode: 'rgb(255, 0, 0)'}, this.alertFunction()); 134 | } else { 135 | this.setState({ color: 'blue', colorcode: 'rgb(255, 0, 0)'}, this.alertFunction()); 136 | } 137 | } 138 | 139 | alertFunction(){ 140 | alert('This alert message is due to the callback function from the setState method!') 141 | } 142 | 143 | render(){ 144 | return 145 | } 146 | } 147 | -------------------------------------------------------------------------------- /Object Oriented Programming/Abstraction.md: -------------------------------------------------------------------------------- 1 | # Abstraction 2 | 3 | Abstraction is best explained through an example. Let's go back to the car example for from **Encapsulation**. We have this Car **class** that has the function of drive, and rolling up windows. It also has a specific set of properties. Well in real life, when we go to use our Cars, we just use the drive function (e.g. turning on the car, stepping on the gas pedal), without really thinking about what is actually going on. All the stuff the engine is doing to make the car move is hidden. When using the car we don't really pay attention to these things - we just **use** it. 4 | 5 | That is abstraction. In programming, when you create an **instance** of a class, known as an **object**, the object has all the functions and properties of the class. Then we can just use those functions, and all we have to know is what input to give the function and what you expect the function to do. All the internal implementation of code is hidden. -------------------------------------------------------------------------------- /Object Oriented Programming/Automobile.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/l1ghtspeed/Programming-Notes/0ed0df7382f30512254438c08ce1b3081a9d3252/Object Oriented Programming/Automobile.class -------------------------------------------------------------------------------- /Object Oriented Programming/Automobile.java: -------------------------------------------------------------------------------- 1 | // This is the super class - automobile 2 | 3 | public class Automobile { 4 | 5 | // public static and final means no instance is required to use or access, and subclass cannot override these values, accessible anywhere 6 | public static final Boolean hasWheels = true; 7 | public static final Boolean hasEngine = true; 8 | 9 | // private is automobile class only 10 | private int numberOfChildClasses = 2; 11 | 12 | // protected is automobile and any of its child classes 13 | protected String name; 14 | protected int numWheels; 15 | 16 | // complex overloaded constructor 17 | public Automobile(String _name, int _numWheels){ 18 | this.name = _name; 19 | this.numWheels = _numWheels; 20 | } 21 | 22 | // simple constructor 23 | public Automobile(){ 24 | this.name = "no name given"; 25 | this.numWheels = 0; 26 | } 27 | 28 | 29 | // To String function, overwriting the base Object toString method from Java. 30 | public String toString() { 31 | return "An automobile is the parent class of many different child classes!"; 32 | } 33 | 34 | 35 | // final function, child class cannot overwrite 36 | public final String getName(){ 37 | return this.name; 38 | } 39 | 40 | 41 | // Static functions to be overwritten 42 | public static String about(){ 43 | return "Automobiles were first invented in the year (some year)"; 44 | } 45 | 46 | 47 | // Static function not to be overwritten 48 | public static final String defineEngine(){ 49 | return "Engine is something that makes the automobile move"; 50 | } 51 | 52 | 53 | // A general drive function, to be overwritten by child classes 54 | public void drive(){ 55 | System.out.println("The automobile drives down the road"); 56 | } 57 | 58 | public int getNumberOfChildClasses(){ 59 | return this.numberOfChildClasses; 60 | } 61 | } 62 | 63 | 64 | -------------------------------------------------------------------------------- /Object Oriented Programming/Car.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/l1ghtspeed/Programming-Notes/0ed0df7382f30512254438c08ce1b3081a9d3252/Object Oriented Programming/Car.class -------------------------------------------------------------------------------- /Object Oriented Programming/Car.java: -------------------------------------------------------------------------------- 1 | // Child class of automobile 2 | 3 | public class Car extends Automobile{ 4 | 5 | // Some car specific properties 6 | private String color; 7 | private String model; 8 | 9 | public Car(){ 10 | super(); 11 | } 12 | 13 | public Car(String _name, String _model, String _color){ 14 | super(_name, 4); 15 | this.model = _model; 16 | this.color = _color; 17 | } 18 | 19 | 20 | // some getter functions 21 | public String getColor(){ 22 | return this.color; 23 | } 24 | public String getModel(){ 25 | return this.model; 26 | } 27 | 28 | // overwrites the normal drive function from automobile 29 | public void drive(){ 30 | System.out.println("The car drives normally down the road"); 31 | } 32 | 33 | public String toString(){ 34 | return "A car is a type of automobile!"; 35 | } 36 | 37 | 38 | } -------------------------------------------------------------------------------- /Object Oriented Programming/Encapsulation.md: -------------------------------------------------------------------------------- 1 | # Encapsulation 2 | 3 | Encapsulation is the first fundamental concepts of OOP we will be covering. Encapsulation is just the bundling of any number of methods (functions), and any amount of data. Bundled data and functions can be then declared to be visible to only within the bundle - this is also called **information hiding**. 4 | 5 | It can sound kind of confusing with pure explaination, so let me give an example, with some sample Java code. 6 | 7 | Let's say we want to bundle together an object called a car. A car can do many things, and has many properties. For example, cars are able to drive. Cars are also able to roll up their windows. Cars also have many properties - we can consider this as the data we bundle. 8 | 9 | 10 | ``` 11 | 12 | public class Car { 13 | 14 | //some bundled variables (data) 15 | private int number_of_wheels = 4; 16 | private int number_of_doors = 4; 17 | private boolean isElectric = false; 18 | private String color = "red"; 19 | 20 | //constructor 21 | public Car(){ 22 | 23 | } 24 | 25 | //this is a bundled method! 26 | public void drive(String direction){ 27 | //some code here to move the car 28 | } 29 | 30 | 31 | //another bundled method 32 | private void roll_up_windows(){ 33 | //some other code here 34 | } 35 | 36 | 37 | } 38 | 39 | ``` 40 | 41 | If we bundled all that stuff together, we get something known in Object Oriented Programming as a **class**. In the example, the class is Car. You can think of classes as a mold, that creates an object with a defined set of properties and functions. 42 | 43 | Ok so you may have noticed that in the sample code before all the functions and type declarations there is a 'private', or 'public' keyword. That's the scope declarator that basically states whether or not that function or variable can be accessed outside of the class. I won't go too in depth about it since it varies based on language. 44 | 45 | Well that's encapsulation in a nutshell. It is basically just the bundling together of many different components into one thing. The full nuances of encapsulation go far beyond what I explained here but this is just a general definition. -------------------------------------------------------------------------------- /Object Oriented Programming/Inheritance.md: -------------------------------------------------------------------------------- 1 | # Inheritance 2 | 3 | Ok now we have gotten to a very unique part of Object Oriented Programming. The third of the four main principles of OOP, **inheritance** just means the ability to pass down traits for a child to **inherit** from it's parent(s). 4 | 5 | Think back to the Car class example from the previous two pages. The Car can be considered a child class. A suitable parent class could be named *Automobile*. All automobiles have traits such as engines. All automobiles have the ability to move. And since the Car class is a child of the Automobile class, the Car class has all the properties of the parent Automobile class. Another possible child class of the Automobile class can be the Motorcycle class. Notice that Car and Motorcycle can have properties independent of each other and it's parent class, but both will share the properties of the parent class. Both have engines and both can move, but for example, you can do a wheelie on a motorcycle, but you can't with a Car, and you can't with all Automobiles. 6 | 7 | The example above is an example of inheritance. There are many different forms, based on the language. It is a powerful concept and can make larger code bases a lot more streamlined and clean. 8 | 9 | 10 | -------------------------------------------------------------------------------- /Object Oriented Programming/Introduction.md: -------------------------------------------------------------------------------- 1 | # Object Oriented Programming 2 | 3 | **Object oriented programming** (**OOP** for short) is a very popular **programming language paradigm**. Programming language paradigms are ways to classify the behaviours of a programming language. A singular language can have multiple paradigms, but most of the super popular languages support the OOP paradigm, because of how clean and robust it is. It's something that just *makes sense*. 4 | 5 | Java, Python, C++, Ruby, C#, are all examples of heavily used languages that are classified under the OOP paradigm. Even JavaScript can be considered part of the family, although it doesn't do **class based** OOP like the aforementioned languages. We'll cover that later on. 6 | 7 | Knowledge of OOP is very important, and is required for the majority of programming jobs. 8 | 9 | ## Overview 10 | 11 | To properly explain all the concepts of OOP would take quite a long time, as it is one of the broadest and most well studied areas of computer science and software engineering. Instead I will do brief explainations on the important stuff - **the 4 Pillars of OOP**. 12 | 13 | Those are: 14 | 15 | ##### 1. Encapsulation 16 | ##### 2. Abstraction 17 | ##### 3. Inheritance 18 | ##### 4. Polymorphism 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /Object Oriented Programming/Polymorphism.md: -------------------------------------------------------------------------------- 1 | # Polymorphism 2 | Polymorphism is the last pillar of OOP, and is often the one that seems the most confusing. It's really not as scary as people think. Polymorphism as the name suggests, simply refers to the ability to change/have multiple definitions of a function with the same name. 3 | 4 | There are two main types of polymorphism - overriding a parent classes method, and overloading a method. 5 | 6 | Overloading just means creating two functions with the same name with different function parameters. This is super useful, as there are many tasks that can perform multiple different things based on what the input is. 7 | 8 | Overriding a parent class can be seen as lets say the parent class has a certain method to perform a certain action, but the child class is also able to perform the action but just in a slightly different way. In that case you can override the parent's method with the child method named the same way. 9 | 10 | -------------------------------------------------------------------------------- /Object Oriented Programming/Test.java: -------------------------------------------------------------------------------- 1 | public class Test { 2 | public static void main(String args[]){ 3 | Automobile randomAuto = new Automobile(); 4 | Car civic = new Car("Honda Civic 2010", "Sedan", "blue"); 5 | System.out.println(randomAuto); 6 | System.out.println(civic.getColor()); 7 | } 8 | } -------------------------------------------------------------------------------- /Programming Languages/Javascript/es6Overview.js: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | ES6, or EMCAScript6, is a (somewhat recent) huge update on the Javascript language. 4 | This new standard provides plenty of powerful and useful features, but also depricates some old ones. 5 | Proficiency in the new ES6 features is a must for any JS developers, whether it be for front end or back end. 6 | Below are some of the more important ES6 features, along with examples. 7 | For a full list on all the changes, visit this page: http://es6-features.org/#Constants 8 | 9 | */ 10 | 11 | 12 | /* 13 | 14 | Arrow Functions: 15 | 16 | General syntax: function_name = (function parameters) => {}; 17 | Simple syntax: function_name =(function parameters) => statement here will automatically be returned; 18 | Is basically a shorthand form for normal function notation, but binds 'this' differently. 19 | 20 | ALSO -> so in JS you can pass in additional arguments in a method call that are not specified. 21 | E.G. 22 | 23 | function foo(bar){ 24 | console.log(bar) 25 | }; 26 | 27 | 28 | --THIS IS OK-- 29 | foo('hello', 'world); 30 | 31 | Argument 'world' can then be accessed in 'arguments var associated with the function. E.g. 32 | 33 | function foo(bar){ 34 | console.log(bar) 35 | console.log(arguments) -> 'world' would be arguments[1] 36 | } 37 | 38 | ARGUMENTS var is GONE if you use arrow functions! 39 | 40 | 41 | */ 42 | 43 | let generic_arrow_func = (a, b) => { return a+b }; 44 | let simple_arrow_func = (a, b) => a + b; 45 | 46 | console.log(simple_arrow_func(4, 5)); 47 | 48 | 49 | /* 50 | 51 | Some stuff about 'this' in JS 52 | 53 | */ 54 | 55 | 56 | let user = { 57 | name: 'Zi Gao', 58 | age: '999', 59 | cities: ['Ottawa', 'Seattle', 'San Francisco', 'Beijing'], 60 | // BTW this new object method notation can be used to make things cleaner 61 | listCities(){ 62 | /* 63 | For the listCities function created normally, 'this' is referring to the object it belongs to. 64 | If listCities was created as an arrow function, 'this' would refer to the context in which the function was created, 65 | which would be the global context since that is what created the listCities method to be part of the 'user' object. 66 | */ 67 | 68 | return this.cities.map((city)=>this.name + ' has lived in ' + city+'!'); 69 | 70 | /* 71 | Another example is shown above -> because the arrow function passed into map is created by the 'user' object's function, 72 | it is created under the context of 'user', and therefore 'this' is referring to user. 73 | 74 | If we had used a normal function creation here, 'this' would be binding to the listCities function, which has no property 'name'. 75 | */ 76 | } 77 | } 78 | 79 | 80 | 81 | /* 82 | 83 | Class: 84 | 85 | Javascript now has "classes" - hooray! 86 | Javascript is still not a lanugage with an object oriented programming model, but now it has syntactic sugar that makes it look like one. 87 | This topic is a rather lengthy one, so for everything you need to know about "class" in JS, visit https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes 88 | 89 | */ 90 | 91 | class Snack { 92 | 93 | constructor(name, brand) { 94 | this.name = name; 95 | this.brand = brand; 96 | } 97 | 98 | static definition(){ 99 | console.log("Snacks are fun to eat!"); 100 | } 101 | 102 | eat() { 103 | console.log(this.name + ' by ' + this.brand + ' is fun to eat!'); 104 | } 105 | 106 | } 107 | 108 | class Chocolate extends Snack { 109 | 110 | constructor(name, brand) { 111 | super(name, brand); 112 | } 113 | 114 | static definition(){ 115 | console.log("Chocolates are sweet!"); 116 | } 117 | 118 | eat() { 119 | console.log(this.name + ' by ' + this.brand + ' is awesome chocolate'); 120 | } 121 | 122 | } 123 | 124 | let kitkat = new Chocolate('Kitkat', 'Nestle'); 125 | 126 | kitkat.eat(); 127 | Chocolate.definition(); 128 | 129 | 130 | /* 131 | 132 | String Interpolation: 133 | 134 | Ever struggled with multiline strings? Or including javascript inside strings? 135 | Say no more. 136 | 137 | */ 138 | 139 | let some_string = 'And can have embedded JS!' 140 | 141 | let this_is_awesome = `This string 142 | 143 | is multi 144 | 145 | lined! 146 | 147 | ${some_string} 148 | 149 | How awesome is that? 150 | `; 151 | 152 | console.log(this_is_awesome); 153 | 154 | 155 | /* 156 | 157 | Default: 158 | 159 | Default function parameter values. Why didn't JS have this before? 160 | 161 | */ 162 | 163 | let get_area = (height, width=10) => {return height*width}; 164 | 165 | 166 | /* 167 | 168 | Async, Await, Promises: 169 | 170 | Promises are a huge upgrade/update to Javascript callbacks. 171 | As you know, Javascript is asynchronous and non-blocking, meaning there is one main thread of execution, 172 | and it never stops to wait for some action to complete. 173 | 174 | */ 175 | 176 | 177 | // Old callback way of handling things (with jQuery and AJAX as example) 178 | let print_content = (data) => { 179 | //Put code in here that you want to execute after the request 180 | console.log(data); 181 | }; 182 | 183 | let get_README_md = (callback) => { 184 | $.ajax({ 185 | url: "https://raw.githubusercontent.com/ZGao28/Programming-Notes/master/README.md", 186 | success: callback(data) 187 | }); 188 | }; 189 | 190 | // New promises + async await way of handling things 191 | 192 | const https = require('https'); 193 | 194 | let add_some_numbers = async (url) => { 195 | //await is used to wait for the get request to go through and come back 196 | let bounds = await get_nums(url); 197 | let sum = 0; 198 | for (let i = (bounds[0] < bounds[1] ? bounds[0] : bounds[1]); i < (bounds[0] < bounds[1] ? bounds[1] : bounds[0]); i++){ 199 | sum+=i; 200 | } 201 | return sum; 202 | } 203 | 204 | let get_nums = (url) => { 205 | //A promise is an object. 206 | //After it gets resolved via the resolve function passed in, the promise gets returned to the await. 207 | return new Promise ((resolve, reject) => 208 | { 209 | https.get(url, (resp) => { 210 | let data = ''; 211 | 212 | resp.on('data', (chunk) => { 213 | data += chunk; 214 | }); 215 | 216 | resp.on('end', () => { 217 | resolve(data); 218 | }); 219 | 220 | }).on("error", (err) => { 221 | console.log("Error: " + err.message); 222 | }); 223 | }); 224 | } 225 | 226 | 227 | 228 | /* 229 | 230 | let and const: 231 | 232 | let and const should be used in place of var! 233 | 234 | const: can be declared and set once, and then cannot be changed again! 235 | Any variable that you know will not be changed should be declared with const -> it's good practice because if you do accidentally change it then the app will crash 236 | 237 | let can be reassigned but cannot be redeclared -> e.g. 238 | 239 | DOES NOT WORK 240 | let apple = 5; 241 | let apple = 5; 242 | 243 | DOES WORK 244 | let apple = 5; 245 | apple = 6; 246 | 247 | var would work in both examples. 248 | 249 | let and const are function scoped like var, but they are both also BLOCK SCOPED -> 250 | meaning if you declare it inside a for loop or if statement, it will not be visible to the outside! 251 | 252 | 253 | 254 | */ 255 | 256 | 257 | let globalLet = 2; //This is global 258 | var globalVar = 2; //This is also global 259 | 260 | if (abc == 2){ 261 | let ifStatementOnlyLet = 3; //This is only visible inside this if statement 262 | var stillGlobalVar = 4; //This is visible globally still! 263 | } 264 | 265 | // this would work 266 | console.log(stillGlobalVar); 267 | 268 | // this would crash 269 | console.log(ifStatementOnlyLet); 270 | 271 | 272 | 273 | /* 274 | 275 | Destructuring: 276 | 277 | Another neat trick offered by ES6, the last that we'll cover in this brief overview. 278 | There are a lot of different ways you can use destructuring, I'll only be showing a couple. 279 | For the full list, visit: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment 280 | 281 | */ 282 | 283 | let [coffee_crisp, aero, snickers, ...other_chocolates] = ["coffee crisp", "aero", "snickers bar", "kit kat", "Mr. Big", "Mars bar"]; 284 | 285 | console.log(other_chocolates); //Should print out array of last three chocolates 286 | 287 | const pet = { 288 | 289 | type: 'dog', 290 | breed: 'husky', 291 | age: 2, 292 | name: 'Rufus' 293 | 294 | } 295 | 296 | //name and age values of pet are passed in as new variables! 297 | //Constructor below is same as {name, age} = {pet.name, pet.age} 298 | let print_pet_name = ({name, age} = pet) => { 299 | console.log(`${name} is ${age} years old!` ); 300 | } 301 | 302 | -------------------------------------------------------------------------------- /Programming Languages/Javascript/jsOverview.js: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Javascript is a powerful, function forward scripting language. 4 | 5 | Thanks to the numerous frameworks developed to support Javascript, 6 | this popular programming language can be used to create a wide variety of things. 7 | 8 | There is React, Angular, Vue, jQuery, and many others for front end web developement. 9 | There is Node, Express, Meteor, Feather and many others for back end web development. 10 | There is even React Native for native app development. 11 | 12 | While it's not perfect, Javascript is still a language that I would recommend to all programmers. 13 | This is in no way a complete guide to the Javascript language, but there will be some important JS features and examples below. 14 | 15 | */ 16 | 17 | 18 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Programming Notes 2 | 3 | A repo containing a mish-mash of all my technical related notes. Based off various books, company technical blogs, Coursera courses, and other online learning material like Udemy, Codecademy, Lynda, ETC. 4 | -------------------------------------------------------------------------------- /System Design/system-design-notes.md: -------------------------------------------------------------------------------- 1 | # System Design 2 | When talking about good system design, there are three main general factors to consider. They are reliability, scalability, and maintainability. 3 | 4 | ### Reliability 5 | Reliability simply refers to how fault tolerant a system is. Given x number of conditions or placed in y scenario, how well does the system hold up? 6 | 7 | ##### Hardware Faults 8 | Hardware faults are when problems occur with hardware systems. Maybe an ethernet connection becomes loose, a hard drive gives out, or even something more extreme and uncontrollable happens - a blackout that crashes a large number or all of an applications servers. Hardware faults occur very frequently, and is hard to predict/prevent. The best way to deal with hardware faults is to use software and hardware redundancy techniques to ensure that the loss of one or more devices doesn't cause the entire system to go down. 9 | 10 | ##### Software Errors 11 | Software errors are bugs in code that can cause a chain reaction of failures in the whole system. There is really no good way to solve software errors other than thoroughly testing all code and processes, and tracking and monitoring behaviours of production software. 12 | 13 | ##### Human Errors 14 | A huge pain point in software systems - this could be inaccuracies in manual entry of data, or anything that involves human interactions. Humans are known to make mistakes. Try to decouple important processes from human interaction. The more automation the better. 15 | 16 | It is important to note that reliability does not simply mean "does my application crash if something unexpected happens". A lot of the times the application may not crash, but it yields a result that the end user does not expect - in a situation like that the system is still considered to be unreliable! A system must also be prone to attacks and general end user mistakes. Reliability in application design is dependant on the help and input of many others in the company, such as UX designers and operations teams. 17 | 18 | ### Scalability 19 | Scalability in system design refers to how reliable a system is when there is a drastic increase in load. The load of an application is comprised of it's load parameters. These can be factors like requests per second, number of reads/writes to a DB, number of concurrent processes, amount of data, etc. The main way to measure the scalability of a software is to analyze it's perfomance when one or more of it's load factors are increased. Performance, like load, can be composed of many things, such as average request speeds. 20 | 21 | Dealing with load usually comes with scaling the system by adding more hardware resources (e.g. vertical scaling by increasing the specs of one machine, or horizontal scaling where workload is distributed among an array of machines), or by fine tuning software to be more efficient. Software must be designed correctly for it to properly scale. 22 | 23 | ### Maintainability 24 | This is pretty self explanatory - after the initial creation and design of the software, how easy is it to adapt to new changes in application code or changes in product requirement or changes in third party software. It also refers to how easy it is for new engineers to understand the codebase and system design and how easy it is to operate. Most of maintainability can be solved through proper documentation and developing flexible, modular code/features. 25 | 26 | Keep these factors in mind when designing your systems - I will be constantly referring back to these in the analysis of which software choices to use. 27 | -------------------------------------------------------------------------------- /WebDevEncyclopedia.md: -------------------------------------------------------------------------------- 1 | # Web Development Encyclopedia 2 | A terminology document with anything related to web development, whether it be frontend or backend. Refer to this when engaging in intellectual conversations with other techies (to flex your knowledge :P). 3 | 4 | ## A - 5 | 6 | ### A/B Testing 7 | A form of user testing where two groups of users are given different versions of an application or general piece of software. Success is then measured with each group and compared. Mostly used to test conversion rates and engagement rates. 8 | 9 | E.G. Facebook A/B tests different designs to see if one design will drive more user interaction. 10 | 11 | ### API 12 | Stands for Application Programming Interface. Probably one of the most used terms in all of web development. An API is just a mechanism (function) that allows two programs to communicate and share information. 13 | 14 | **E.g.** My program will use the Facebook friends list API to get a list of friends. 15 | 16 | **For more**: https://medium.freecodecamp.org/what-is-an-api-in-english-please-b880a3214a82 17 | 18 | ## B - 19 | 20 | ## C - 21 | 22 | ### Cache 23 | A cache, or caching layer, is a storage system to provide snappier request completion and reduce stress on your servers. When a new resource is requested for the first time, it can be stored in a cache, and future requests for that resource will be sent from the cache instead of having to query everything through your backend all over again. Caches usually are pretty small in storage size and should not be used to store rapidly changing data - because then the users will be getting stale data! 24 | 25 | **E.g.** Facebook caches some of your old photos so that loading them is much faster. 26 | 27 | **For more** https://developer.mozilla.org/en-US/docs/Web/HTTP/Caching 28 | 29 | ### Camelcasing 30 | Naming convention in which all spaces are removed and the next term's first letter is capitalized. Standard naming convention used by languages like Javascript. 31 | 32 | **E.G.** someVariableName, or dogOne 33 | 34 | ### CORS 35 | Stands for Cross-Origin Resource Sharing. By adding additional information in the HTTP request header, it allows one domain to request resources from another domain. 36 | 37 | **E.G.** http://domainA.com/ makes a call to an api at http://api.domainB.com/someApi 38 | 39 | **For More:** https://www.codecademy.com/articles/what-is-cors 40 | 41 | ## D - 42 | 43 | ## E - 44 | 45 | ### EMCA Script (ES5, ES6, etc) 46 | 47 | EMCA Script is just another name for javascript, the versioning of which is determined by the number proceeding it. 48 | 49 | **E.G.** Compile your ES6 code down to ES5 for it to be compatible with all browsers! 50 | 51 | ## F - 52 | 53 | ## G - 54 | 55 | ## H - 56 | 57 | ## I - 58 | 59 | ## J - 60 | 61 | ## K - 62 | 63 | ## L - 64 | 65 | ### Lazy Loading 66 | Lazy loading is a general concept in programming in which assets are loaded only when they're needed. For example, if you have a lot of large, high definition images, it would take a lot of extra time loading them in. It's hugely inefficient and unneccesary if they won't even be rendered right away. The simple usage of lazy loading can decrease website load times several times over. 67 | 68 | **E.g.** Facebook lazy loads posts that are less relevant - they only load when you scroll down to them. 69 | 70 | **For more** https://developers.google.com/web/fundamentals/performance/lazy-loading-guidance/images-and-video/ 71 | 72 | 73 | ## M - 74 | 75 | ## N - 76 | 77 | ## O - 78 | 79 | ## P - 80 | 81 | ### Polyfill 82 | 83 | Basically code that makes certain APIs compatible with browsers. In fact a lot of code requires polyfill to work. Transpiling tools like Babel usually provide their own libraries for polyfills. 84 | 85 | ## Q - 86 | 87 | ## R - 88 | 89 | ### Regex 90 | Stands for regular expression. It is a special text string that specifies a pattern to search for in another string or document. 91 | 92 | **E.G.** To find all valid email addresses in a document could use the regular expression \b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z] 93 | {2,6}\b 94 | 95 | **For more**: https://www.regexbuddy.com/regex.html 96 | 97 | ### Responsive Design 98 | A general desgin term. Basically it just means designs that can scale up or down to look good on any device resolution. 99 | 100 | **E.G.** Facebook's webapp looks good on mobile and on a large monitor, because it has a expertly made responsive design. 101 | 102 | ## S - 103 | 104 | ### SaaS 105 | Not to be confused with SASS, a styling tool. SaaS stands for "Software as a Service". Basically referring to any kind of software platform that provides some kind of service. 106 | 107 | **E.G.** Amazon's AWS is a SaaS based product, as it provides a whole host of services through software, through the cloud. 108 | 109 | ### Snakecase 110 | A naming style in which spaces are replaced with underscores. This is the standard naming convention for SQL tables and columns. 111 | 112 | **E.G.** some_variable_name, or dog_one 113 | 114 | ## T - 115 | 116 | ## U - 117 | 118 | ## V - 119 | 120 | ## W - 121 | 122 | ### Webhook 123 | Basically a custom made callback with an event listener for when an action happens. 124 | 125 | **E.g.** Writing a webhook script for when a friend is added on Facebook, the callback sends an email to notify you. 126 | 127 | **For more**: https://sendgrid.com/blog/whats-webhook/ 128 | 129 | ## X - 130 | 131 | ## Y - 132 | 133 | ## Z - 134 | 135 | --------------------------------------------------------------------------------