Spaces:
Sleeping
Sleeping
File size: 8,818 Bytes
4a3f117 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 | import os
docs = {
"binary_search.txt": """Binary Search is an efficient algorithm for finding an item in a sorted list.
It works by repeatedly dividing the search interval in half.
Time Complexity: O(log n). Space Complexity: O(1).
Steps:
1) Find the middle element.
2) If target equals middle, return index.
3) If target < middle, search left half.
4) Else search right half.
Requires the array to be sorted before searching.
Best case: O(1) when middle element is the target.
""",
"stack_queue.txt": """Stack is a linear data structure following LIFO (Last In First Out) principle.
Operations: push() adds element, pop() removes top element, peek() views top.
All stack operations run in O(1) time.
Use stack for: undo operations, expression evaluation, function call stack, DFS traversal.
Queue follows FIFO (First In First Out) principle.
Operations: enqueue() adds to rear, dequeue() removes from front.
All queue operations run in O(1) time.
Use queue for: BFS traversal, scheduling, print spooler, task queues.
Key difference: Stack removes most recently added item; Queue removes oldest item.
""",
"sorting_algorithms.txt": """Bubble Sort: repeatedly swaps adjacent elements if out of order.
Time: O(n^2) average and worst. Space: O(1). Stable sort.
Merge Sort: divide array in half, sort each half recursively, merge results.
Time: O(n log n) all cases. Space: O(n). Stable sort. Best for linked lists.
Quick Sort: picks a pivot element, partitions array around it, recursively sorts partitions.
Time: O(n log n) average, O(n^2) worst case. Space: O(log n). Not stable.
Insertion Sort: builds sorted array one element at a time by inserting each into correct position.
Time: O(n^2) worst, O(n) best. Space: O(1). Good for small or nearly sorted arrays.
Heap Sort: builds a max-heap, repeatedly extracts maximum element.
Time: O(n log n). Space: O(1). Not stable.
""",
"linked_list.txt": """Linked List is a linear data structure where elements are stored in nodes.
Each node contains: data field + pointer to next node.
Types:
- Singly Linked List: each node points to next node only.
- Doubly Linked List: each node has pointers to both next and previous nodes.
- Circular Linked List: last node points back to first node.
Advantages over arrays: dynamic size, O(1) insertion/deletion at head.
Disadvantages: no random access (must traverse), extra memory for pointers.
Operations: Insert at head O(1), Search O(n), Delete O(n), Access by index O(n).
""",
"trees.txt": """Binary Tree: each node has at most 2 children called left and right child.
Root is the topmost node. Leaf nodes have no children.
Binary Search Tree (BST): left child < parent node < right child.
Search, Insert, Delete all take O(log n) average, O(n) worst case.
Tree Traversals:
- Inorder (Left, Root, Right): gives sorted order for BST.
- Preorder (Root, Left, Right): used to copy tree.
- Postorder (Left, Right, Root): used to delete tree.
- Level Order: BFS traversal level by level.
Height of tree: number of edges on longest path from root to leaf.
Balanced BST examples: AVL Tree, Red-Black Tree — guarantee O(log n) operations.
""",
"dynamic_programming.txt": """Dynamic Programming (DP) solves problems by breaking into overlapping subproblems.
Stores results of subproblems to avoid recomputation (memoization).
Key properties required:
1. Optimal substructure: optimal solution built from optimal subproblem solutions.
2. Overlapping subproblems: same subproblems solved multiple times.
Approaches:
- Top-down (Memoization): recursion + cache results in a table.
- Bottom-up (Tabulation): fill table iteratively from smallest subproblems.
Classic DP problems:
- Fibonacci: O(2^n) naive → O(n) with DP.
- 0/1 Knapsack: maximize value within weight limit.
- Longest Common Subsequence (LCS).
- Coin Change: minimum coins to make a sum.
- Longest Increasing Subsequence.
""",
"react_hooks.txt": """React Hooks are functions that let you use state and lifecycle features in functional components.
Introduced in React 16.8 to replace class components.
useState: manages local component state.
Syntax: const [state, setState] = useState(initialValue)
Example: const [count, setCount] = useState(0)
useEffect: handles side effects like API calls, subscriptions, DOM updates.
Runs after every render by default.
Syntax: useEffect(() => { /* effect */ }, [dependencies])
Empty dependency array [] means run only once on mount.
useContext: consume React context without wrapping in Consumer component.
Syntax: const value = useContext(MyContext)
useRef: holds mutable value that does not trigger re-render. Also used to access DOM elements.
Rules of Hooks:
1. Only call hooks at the top level (not inside loops or conditions).
2. Only call hooks from React function components or custom hooks.
""",
"rest_api.txt": """REST API (Representational State Transfer) is an architectural style for web services.
REST uses standard HTTP methods to perform operations on resources.
HTTP Methods:
- GET: retrieve/read resource. No request body.
- POST: create new resource. Data sent in request body.
- PUT: update existing resource completely.
- PATCH: partially update a resource.
- DELETE: remove a resource.
HTTP Status Codes:
- 200 OK: request succeeded.
- 201 Created: new resource created successfully.
- 400 Bad Request: invalid request from client.
- 401 Unauthorized: authentication required.
- 403 Forbidden: authenticated but not authorized.
- 404 Not Found: resource does not exist.
- 500 Internal Server Error: server-side error.
REST principles: Stateless, Client-Server, Cacheable, Uniform Interface.
JSON is the most common data format for REST API responses.
""",
"css_flexbox.txt": """CSS Flexbox (Flexible Box Layout) is a layout model for arranging items in a container.
Activated by setting display: flex on a container element.
Container properties:
- flex-direction: row (default, horizontal) or column (vertical).
- justify-content: aligns items on main axis. Values: flex-start, flex-end, center, space-between, space-around.
- align-items: aligns items on cross axis. Values: stretch (default), flex-start, flex-end, center.
- flex-wrap: nowrap (default) or wrap to allow items to move to next line.
- gap: space between flex items.
Item properties:
- flex-grow: how much item grows relative to siblings. 0 means no grow.
- flex-shrink: how much item shrinks when space is limited.
- flex-basis: initial size of item before flex calculations.
- align-self: overrides align-items for a single item.
Shorthand: flex: grow shrink basis (e.g., flex: 1 1 auto)
""",
"javascript_promises.txt": """JavaScript Promises handle asynchronous operations in a cleaner way than callbacks.
Promise States:
- Pending: initial state, operation not complete.
- Fulfilled: operation completed successfully.
- Rejected: operation failed.
Creating a Promise:
const myPromise = new Promise((resolve, reject) => {
if (success) resolve(result);
else reject(error);
});
Consuming a Promise:
myPromise
.then(result => console.log(result))
.catch(error => console.error(error))
.finally(() => console.log('always runs'));
async/await syntax (ES2017) is syntactic sugar over promises:
async function fetchData() {
try {
const data = await fetch(url);
const json = await data.json();
return json;
} catch (error) {
console.error(error);
}
}
Promise utility methods:
- Promise.all([p1, p2]): runs in parallel, resolves when all resolve.
- Promise.race([p1, p2]): resolves/rejects with first settled promise.
- Promise.allSettled(): waits for all, returns all results regardless of success/failure.
""",
"graph_algorithms.txt": """Graph is a data structure with nodes (vertices) connected by edges.
Types: Directed/Undirected, Weighted/Unweighted, Cyclic/Acyclic.
Representations:
- Adjacency Matrix: 2D array. O(1) edge lookup, O(V^2) space.
- Adjacency List: array of lists. O(V+E) space. Better for sparse graphs.
BFS (Breadth-First Search):
Explores level by level using a queue. Time: O(V+E).
Used for: shortest path in unweighted graph, level-order traversal.
DFS (Depth-First Search):
Explores as far as possible using stack/recursion. Time: O(V+E).
Used for: cycle detection, topological sort, connected components.
Dijkstra's Algorithm: shortest path in weighted graph with non-negative edges.
Time: O((V+E) log V) with priority queue.
Topological Sort: linear ordering of vertices where each directed edge u→v means u comes before v.
Only valid for Directed Acyclic Graphs (DAGs).
"""
}
os.makedirs("docs", exist_ok=True)
for filename, content in docs.items():
with open(f"docs/{filename}", "w", encoding="utf-8") as f:
f.write(content.strip())
print(f"Created {len(docs)} documents in docs/")
|