Skip to content

Commit 1a410ea

Browse files
committed
add pair instruction and examples
1 parent fc8b922 commit 1a410ea

9 files changed

Lines changed: 153 additions & 31 deletions

File tree

src/chapters/combining-patterns/9b8a8427.js

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ export const codeExcerciseOneChapter = {
1010
1111
**Navigate:** 1 | [2](a4b7c9d2) | [3](e8f1a5b3) | [4](c6d9e2f4) | [5](b3a8d7c1) | [6](f9e4b2a7) | [7](d1c5f8e3) | [8](a7b2e9f6) | [9](c4f7a1d8)
1212
13+
## 👥 Pair Programming Instructions
14+
15+
**Work in pairs for this challenge!** One person should be the **Driver** (writing code) and the other the **Navigator** (reviewing and guiding). **Switch roles for each function** you implement.
16+
17+
<iframe width="560" height="315" src="https://www.youtube.com/embed/jqGmL6Hf23k?si=qXmQcnQigfo1adTb" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe>
18+
1319
**Why it matters:** Arrays are the workhorse. Mastering iteration and index-based edits builds intuition for time complexity.
1420
1521
**Objectives:**
@@ -22,8 +28,16 @@ export const codeExcerciseOneChapter = {
2228
## Problems to Solve:
2329
2430
1. **sumOfSquares(nums)** - Return the sum of x² for each x in nums (no Array.prototype.map)
31+
- Example: \`sumOfSquares([1, 2, 3])\` → \`14\` (1² + 2² + 3² = 1 + 4 + 9 = 14)
32+
- Example: \`sumOfSquares([2, 4])\` → \`20\` (2² + 4² = 4 + 16 = 20)
33+
2534
2. **insertAt(arr, index, value)** - Return NEW array with value inserted at index
35+
- Example: \`insertAt([10, 20, 30], 1, 99)\` → \`[10, 99, 20, 30]\`
36+
- Example: \`insertAt([], 0, 'a')\` → \`['a']\`
37+
2638
3. **removeAt(arr, index)** - Return NEW array without the element at index
39+
- Example: \`removeAt([10, 20, 30], 1)\` → \`[10, 30]\`
40+
- Example: \`removeAt([5], 0)\` → \`[]\`
2741
2842
## Big-O Analysis:
2943
- **insertAt/removeAt are O(n)**: They require shifting elements to maintain array structure
@@ -80,6 +94,9 @@ function insertAt(arr, index, value) {
8094
8195
function removeAt(arr, index) {
8296
// returns NEW array without the element at index
97+
if (index < 0 || index >= arr.length) {
98+
return [...arr]; // Return a new array that is a copy of the original
99+
}
83100
return [...arr.slice(0, index), ...arr.slice(index + 1)];
84101
}
85102
@@ -93,8 +110,8 @@ function removeAt(arr, index) {
93110
name: "sumOfSquares calculates correctly",
94111
test: (code) => {
95112
try {
96-
const func = new Function(`${code}; return { sumOfSquares, insertAt, removeAt };`)();
97-
const result = func.sumOfSquares([1, 2, 3]);
113+
const sumOfSquares = new Function(`${code}; \n return sumOfSquares;`)();
114+
const result = sumOfSquares([1, 2, 3]);
98115
if (result === 14) {
99116
return new TestResult({ passed: true });
100117
} else {
@@ -116,9 +133,9 @@ function removeAt(arr, index) {
116133
name: "insertAt works correctly",
117134
test: (code) => {
118135
try {
119-
const func = new Function(`${code}; return { sumOfSquares, insertAt, removeAt };`)();
120-
const result1 = func.insertAt([10, 20, 30], 1, 99);
121-
const result2 = func.insertAt([], 0, 'a');
136+
const insertAt = new Function(`${code}; \n return insertAt;`)();
137+
const result1 = insertAt([10, 20, 30], 1, 99);
138+
const result2 = insertAt([], 0, 'a');
122139

123140
const test1 = JSON.stringify(result1) === JSON.stringify([10, 99, 20, 30]);
124141
const test2 = JSON.stringify(result2) === JSON.stringify(['a']);
@@ -144,8 +161,8 @@ function removeAt(arr, index) {
144161
name: "removeAt works correctly",
145162
test: (code) => {
146163
try {
147-
const func = new Function(`${code}; return { sumOfSquares, insertAt, removeAt };`)();
148-
const result = func.removeAt([10, 20, 30], 1);
164+
const removeAt = new Function(`${code}; \n return removeAt;`)();
165+
const result = removeAt([10, 20, 30], 1);
149166

150167
if (JSON.stringify(result) === JSON.stringify([10, 30])) {
151168
return new TestResult({ passed: true });

src/chapters/combining-patterns/a4b7c9d2.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ export const codeExcerciseTwoChapter = {
1010
1111
**Navigate:** [1](9b8a8427) | 2 | [3](e8f1a5b3) | [4](c6d9e2f4) | [5](b3a8d7c1) | [6](f9e4b2a7) | [7](d1c5f8e3) | [8](a7b2e9f6) | [9](c4f7a1d8)
1212
13+
## 👥 Pair Programming Instructions
14+
15+
**Work in pairs for this challenge!** One person should be the **Driver** (writing code) and the other the **Navigator** (reviewing and guiding). **Switch roles for each function** you implement.
16+
17+
<iframe width="560" height="315" src="https://www.youtube.com/embed/jqGmL6Hf23k?si=qXmQcnQigfo1adTb" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe>
18+
1319
**Why it matters:** Grid problems are everywhere (games, pathfinding, image processing). Neighbor logic is foundational.
1420
1521
**Objectives:**
@@ -21,7 +27,12 @@ export const codeExcerciseTwoChapter = {
2127
## Problems to Solve:
2228
2329
1. **countNeighbors(grid, r, c, target)** - Count 8-direction neighbors that match a target value
30+
- Example: \`countNeighbors([[1,1,0],[0,1,0],[0,0,1]], 0, 0, 1)\` → \`2\` (neighbors at (0,1) and (1,1))
31+
- Example: \`countNeighbors([[1,0],[0,1]], 1, 1, 0)\` → \`2\` (neighbors at (0,1) and (1,0))
32+
2433
2. **neighborCountGrid(grid, target)** - For each cell, set it to the number of matching neighbors (new grid)
34+
- Example: \`neighborCountGrid([[1,0],[0,1]], 1)\` → \`[[1,2],[2,1]]\`
35+
- Example: \`neighborCountGrid([[0,0],[0,0]], 1)\` → \`[[0,0],[0,0]]\`
2536
2637
## Big-O Analysis:
2738
- **Runtime: O(R*C)** since each cell looks at constant 8 neighbors
@@ -99,7 +110,7 @@ function neighborCountGrid(grid, target) {
99110
name: "countNeighbors works correctly",
100111
test: (code) => {
101112
try {
102-
const func = new Function(`${code}; return { countNeighbors, neighborCountGrid };`)();
113+
const func = new Function(`${code}; \n return { countNeighbors, neighborCountGrid };`)();
103114
const g = [
104115
[1,1,0],
105116
[0,1,0],
@@ -128,7 +139,7 @@ function neighborCountGrid(grid, target) {
128139
name: "neighborCountGrid works correctly",
129140
test: (code) => {
130141
try {
131-
const func = new Function(`${code}; return { countNeighbors, neighborCountGrid };`)();
142+
const func = new Function(`${code}; \n return { countNeighbors, neighborCountGrid };`)();
132143
const result = func.neighborCountGrid([[1,0],[0,1]], 1);
133144
const expected = [[1,2],[2,1]];
134145

@@ -153,7 +164,7 @@ function neighborCountGrid(grid, target) {
153164
name: "Handles edge cases",
154165
test: (code) => {
155166
try {
156-
const func = new Function(`${code}; return { countNeighbors, neighborCountGrid };`)();
167+
const func = new Function(`${code}; \n return { countNeighbors, neighborCountGrid };`)();
157168

158169
// Single cell grid
159170
const result1 = func.countNeighbors([[1]], 0, 0, 1);

src/chapters/combining-patterns/a7b2e9f6.js

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ export const codeExcerciseEightChapter = {
1010
1111
**Navigate:** [1](9b8a8427) | [2](a4b7c9d2) | [3](e8f1a5b3) | [4](c6d9e2f4) | [5](b3a8d7c1) | [6](f9e4b2a7) | [7](d1c5f8e3) | 8 | [9](c4f7a1d8)
1212
13+
## 👥 Pair Programming Instructions
14+
15+
**Work in pairs for this challenge!** One person should be the **Driver** (writing code) and the other the **Navigator** (reviewing and guiding). **Switch roles for each function** you implement.
16+
17+
<iframe width="560" height="315" src="https://www.youtube.com/embed/jqGmL6Hf23k?si=qXmQcnQigfo1adTb" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe>
18+
1319
**Why it matters:** Real work is often sorting records by one or more fields.
1420
1521
**Objectives:**
@@ -26,6 +32,13 @@ export const codeExcerciseEightChapter = {
2632
2733
Task object format: \`{id: number, title: string, priority: number, due: string}\`
2834
35+
**Examples:**
36+
- Input: \`[{id:1, priority:2, due:'2025-09-10'}, {id:2, priority:1, due:'2025-09-15'}]\`
37+
- Output: \`[{id:2, priority:1, due:'2025-09-15'}, {id:1, priority:2, due:'2025-09-10'}]\` (priority 1 first)
38+
39+
- Input: \`[{id:3, priority:1, due:'2025-09-01'}, {id:4, priority:1, due:'2025-08-31'}]\`
40+
- Output: \`[{id:4, priority:1, due:'2025-08-31'}, {id:3, priority:1, due:'2025-09-01'}]\` (earlier date first)
41+
2942
## Algorithm:
3043
1. Use Array.sort() with custom comparator
3144
2. First compare by priority
@@ -89,7 +102,7 @@ function sortTasks(tasks){
89102
name: "sortTasks works correctly",
90103
test: (code) => {
91104
try {
92-
const func = new Function(`${code}; return sortTasks;`)();
105+
const func = new Function(`${code}; \n return sortTasks;`)();
93106

94107
const tasks = [
95108
{id:1, title:'a', priority:2, due:'2025-09-10'},
@@ -122,7 +135,7 @@ function sortTasks(tasks){
122135
name: "Does not mutate original array",
123136
test: (code) => {
124137
try {
125-
const func = new Function(`${code}; return sortTasks;`)();
138+
const func = new Function(`${code}; \n return sortTasks;`)();
126139

127140
const tasks = [
128141
{id:1, title:'a', priority:2, due:'2025-09-10'},
@@ -157,7 +170,7 @@ function sortTasks(tasks){
157170
name: "Handles edge cases",
158171
test: (code) => {
159172
try {
160-
const func = new Function(`${code}; return sortTasks;`)();
173+
const func = new Function(`${code}; \n return sortTasks;`)();
161174

162175
// Empty array
163176
const result1 = func([]);

src/chapters/combining-patterns/b3a8d7c1.js

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ export const codeExcerciseFiveChapter = {
1010
1111
**Navigate:** [1](9b8a8427) | [2](a4b7c9d2) | [3](e8f1a5b3) | [4](c6d9e2f4) | 5 | [6](f9e4b2a7) | [7](d1c5f8e3) | [8](a7b2e9f6) | [9](c4f7a1d8)
1212
13+
## 👥 Pair Programming Instructions
14+
15+
**Work in pairs for this challenge!** One person should be the **Driver** (writing code) and the other the **Navigator** (reviewing and guiding). **Switch roles for each function** you implement.
16+
17+
<iframe width="560" height="315" src="https://www.youtube.com/embed/jqGmL6Hf23k?si=qXmQcnQigfo1adTb" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe>
18+
1319
**Why it matters:** Hash-based structures unlock O(1) average lookups and fast membership tests.
1420
1521
**Objectives:**
@@ -21,7 +27,14 @@ export const codeExcerciseFiveChapter = {
2127
## Problems to Solve:
2228
2329
1. **wordCount(text)** - Return a Map of word → count (case-insensitive, letters only)
30+
- Example: \`wordCount('One fish two fish')\` → Map{'one' => 1, 'fish' => 2, 'two' => 1}
31+
- Example: \`wordCount('Hello, World!')\` → Map{'hello' => 1, 'world' => 1}
32+
- Example: \`wordCount('123 !@#')\` → Map{} (no letters)
33+
2434
2. **hasDuplicate(arr)** - Return true if array has any duplicate value (use Set)
35+
- Example: \`hasDuplicate([1, 2, 3, 2])\` → \`true\` (2 appears twice)
36+
- Example: \`hasDuplicate([1, 2, 3])\` → \`false\` (all unique)
37+
- Example: \`hasDuplicate([])\` → \`false\` (empty array)
2538
2639
## Big-O Analysis:
2740
- **Average O(1)** for Map/Set operations (.get/.set/.has)
@@ -90,7 +103,7 @@ function hasDuplicate(arr) {
90103
name: "wordCount works correctly",
91104
test: (code) => {
92105
try {
93-
const func = new Function(`${code}; return { wordCount, hasDuplicate };`)();
106+
const func = new Function(`${code}; \n return { wordCount, hasDuplicate };`)();
94107
const wc = func.wordCount('One fish two fish, red fish blue fish.');
95108

96109
const fishCount = wc.get('fish');
@@ -117,7 +130,7 @@ function hasDuplicate(arr) {
117130
name: "hasDuplicate detects duplicates",
118131
test: (code) => {
119132
try {
120-
const func = new Function(`${code}; return { wordCount, hasDuplicate };`)();
133+
const func = new Function(`${code}; \n return { wordCount, hasDuplicate };`)();
121134

122135
const result1 = func.hasDuplicate([1, 2, 3, 2]);
123136
const result2 = func.hasDuplicate([1, 2, 3]);
@@ -143,7 +156,7 @@ function hasDuplicate(arr) {
143156
name: "Handles edge cases",
144157
test: (code) => {
145158
try {
146-
const func = new Function(`${code}; return { wordCount, hasDuplicate };`)();
159+
const func = new Function(`${code}; \n return { wordCount, hasDuplicate };`)();
147160

148161
// Empty text
149162
const wc1 = func.wordCount('');

src/chapters/combining-patterns/c4f7a1d8.js

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ export const codeExcerciseNineChapter = {
1010
1111
**Navigate:** [1](9b8a8427) | [2](a4b7c9d2) | [3](e8f1a5b3) | [4](c6d9e2f4) | [5](b3a8d7c1) | [6](f9e4b2a7) | [7](d1c5f8e3) | [8](a7b2e9f6) | 9
1212
13+
## 👥 Pair Programming Instructions
14+
15+
**Work in pairs for this challenge!** One person should be the **Driver** (writing code) and the other the **Navigator** (reviewing and guiding). **Switch roles for each function** you implement.
16+
17+
<iframe width="560" height="315" src="https://www.youtube.com/embed/jqGmL6Hf23k?si=qXmQcnQigfo1adTb" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe>
18+
1319
**Why it matters:** Sliding window replaces nested loops when aggregating over contiguous spans.
1420
1521
**Objectives:**
@@ -22,6 +28,13 @@ export const codeExcerciseNineChapter = {
2228
2329
**maxSumOfSizeK(nums, k)** - Return the maximum sum of any contiguous subarray of length k
2430
31+
**Examples:**
32+
- \`maxSumOfSizeK([2,1,5,1,3,2], 3)\` → \`9\` (subarray [5,1,3] = 5+1+3 = 9)
33+
- \`maxSumOfSizeK([2,3,4,1,5], 2)\` → \`7\` (subarray [3,4] = 3+4 = 7)
34+
- \`maxSumOfSizeK([1], 1)\` → \`1\` (single element)
35+
- \`maxSumOfSizeK([1,2], 3)\` → \`null\` (k > array length)
36+
- \`maxSumOfSizeK([-1,-2,-3,-4], 2)\` → \`-3\` (subarray [-1,-2] = -1+(-2) = -3)
37+
2538
## Algorithm:
2639
1. Calculate sum of first k elements (initial window)
2740
2. Slide the window: add next element, remove first element
@@ -94,7 +107,7 @@ function maxSumOfSizeK(nums, k){
94107
name: "maxSumOfSizeK works correctly",
95108
test: (code) => {
96109
try {
97-
const func = new Function(`${code}; return maxSumOfSizeK;`)();
110+
const func = new Function(`${code}; \n return maxSumOfSizeK;`)();
98111

99112
const result1 = func([2,1,5,1,3,2], 3); // 5+1+3 = 9
100113
const result2 = func([2,3,4,1,5], 2); // 3+4 = 7
@@ -121,7 +134,7 @@ function maxSumOfSizeK(nums, k){
121134
name: "Handles edge cases",
122135
test: (code) => {
123136
try {
124-
const func = new Function(`${code}; return maxSumOfSizeK;`)();
137+
const func = new Function(`${code}; \n return maxSumOfSizeK;`)();
125138

126139
const result1 = func([1,2], 3); // k > length
127140
const result2 = func([], 1); // empty array
@@ -148,7 +161,7 @@ function maxSumOfSizeK(nums, k){
148161
name: "Works with negative numbers",
149162
test: (code) => {
150163
try {
151-
const func = new Function(`${code}; return maxSumOfSizeK;`)();
164+
const func = new Function(`${code}; \n return maxSumOfSizeK;`)();
152165

153166
const result1 = func([-1, -2, -3, -4], 2); // max of [-1,-2], [-2,-3], [-3,-4] = -3
154167
const result2 = func([1, -1, 0, 2], 2); // max of [1,-1], [-1,0], [0,2] = 2

src/chapters/combining-patterns/c6d9e2f4.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ export const codeExcerciseFourChapter = {
1010
1111
**Navigate:** [1](9b8a8427) | [2](a4b7c9d2) | [3](e8f1a5b3) | 4 | [5](b3a8d7c1) | [6](f9e4b2a7) | [7](d1c5f8e3) | [8](a7b2e9f6) | [9](c4f7a1d8)
1212
13+
## 👥 Pair Programming Instructions
14+
15+
**Work in pairs for this challenge!** One person should be the **Driver** (writing code) and the other the **Navigator** (reviewing and guiding). **Switch roles for each function** you implement.
16+
17+
<iframe width="560" height="315" src="https://www.youtube.com/embed/jqGmL6Hf23k?si=qXmQcnQigfo1adTb" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe>
18+
1319
**Why it matters:** Two pointers converts nested loops to linear time when data is ordered.
1420
1521
**Objectives:**
@@ -21,6 +27,10 @@ export const codeExcerciseFourChapter = {
2127
## Problem to Solve:
2228
2329
**hasPairWithSum(sortedNums, target)** - Given a sorted array and target, return true if any pair sums to target
30+
- Example: \`hasPairWithSum([1, 2, 4, 4], 8)\` → \`true\` (4 + 4 = 8)
31+
- Example: \`hasPairWithSum([1, 2, 3, 9], 8)\` → \`false\` (no pair sums to 8)
32+
- Example: \`hasPairWithSum([1, 2], 3)\` → \`true\` (1 + 2 = 3)
33+
- Example: \`hasPairWithSum([5], 5)\` → \`false\` (single element, no pair)
2434
2535
## Algorithm:
2636
1. Use two pointers: left at start, right at end
@@ -85,7 +95,7 @@ function hasPairWithSum(sortedNums, target) {
8595
name: "Finds pair that sums to target",
8696
test: (code) => {
8797
try {
88-
const func = new Function(`${code}; return hasPairWithSum;`)();
98+
const func = new Function(`${code}; \n return hasPairWithSum;`)();
8999
const result = func([1, 2, 4, 4], 8);
90100

91101
if (result === true) {
@@ -109,7 +119,7 @@ function hasPairWithSum(sortedNums, target) {
109119
name: "Returns false when no pair exists",
110120
test: (code) => {
111121
try {
112-
const func = new Function(`${code}; return hasPairWithSum;`)();
122+
const func = new Function(`${code}; \n return hasPairWithSum;`)();
113123
const result = func([1, 2, 3, 9], 8);
114124

115125
if (result === false) {
@@ -133,7 +143,7 @@ function hasPairWithSum(sortedNums, target) {
133143
name: "Handles edge cases",
134144
test: (code) => {
135145
try {
136-
const func = new Function(`${code}; return hasPairWithSum;`)();
146+
const func = new Function(`${code}; \n return hasPairWithSum;`)();
137147

138148
const result1 = func([], 5); // Empty array
139149
const result2 = func([5], 5); // Single element

0 commit comments

Comments
 (0)