Skip to content

Commit 0b332f1

Browse files
examples and finalizing content
1 parent b587ba9 commit 0b332f1

14 files changed

Lines changed: 423 additions & 272 deletions
Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,22 @@
11
import content from './text/binary-search-fundamentals-content.md?raw';
2+
import example from './text/binary-search-fundamentals-example.js?raw';
3+
import completed from './text/binary-search-fundamentals-completed.js?raw';
24

35
export const binarySearchFundamentalsChapter = {
46
id: 'binary-search-fundamentals',
57
title: 'Binary Search Fundamentals',
68
sectionId: 'sorting-and-binary-search',
79
previousChapterId: 'sort-method',
810
content: content,
9-
exercise: null
11+
exercise: {
12+
starterCode: example,
13+
solution: completed,
14+
question: `
15+
# Exercise Question
16+
`,
17+
solution_explanation: `
18+
# Solution Explanation
19+
it goes like this
20+
`
21+
}
1022
};
Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,22 @@
11
import content from './text/binary-search-variants-content.md?raw';
2+
import example from './text/binary-search-variants-example.js?raw';
3+
import completed from './text/binary-search-variants-completed.js?raw';
24

35
export const binarySearchVariantsChapter = {
46
id: 'binary-search-variants',
57
title: 'Binary Search Variants',
68
sectionId: 'sorting-and-binary-search',
79
previousChapterId: 'binary-search-fundamentals',
810
content: content,
9-
exercise: null
11+
exercise: {
12+
starterCode: example,
13+
solution: completed,
14+
question: `
15+
# Exercise Question
16+
`,
17+
solution_explanation: `
18+
# Solution Explanation
19+
it goes like this
20+
`
21+
}
1022
};
Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,22 @@
11
import content from './text/sort-method-content.md?raw';
2+
import example from './text/sort-method-example.js?raw';
3+
import completed from './text/sort-method-completed.js?raw';
24

35
export const sortMethodChapter = {
46
id: 'sort-method',
57
title: 'Using .sort() with Comparators',
68
sectionId: 'sorting-and-binary-search',
79
previousChapterId: 'sorting-approaches',
810
content: content,
9-
exercise: null
11+
exercise: {
12+
starterCode: example,
13+
solution: completed,
14+
question: `
15+
# Exercise Question
16+
`,
17+
solution_explanation: `
18+
# Solution Explanation
19+
it goes like this
20+
`
21+
}
1022
};
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
2+
// Customer asks for "Hotel" but the full title is "Hotel California"
3+
function findAlbumsStartingWith(sortedCollection, prefix) {
4+
// Find the first album that starts with the prefix
5+
let left = 0;
6+
let right = sortedCollection.length - 1;
7+
let firstMatch = -1;
8+
9+
while (left <= right) {
10+
const mid = Math.floor(left + (right - left) / 2);
11+
const currentTitle = sortedCollection[mid].title;
12+
13+
if (currentTitle.toLowerCase().startsWith(prefix.toLowerCase())) {
14+
firstMatch = mid;
15+
right = mid - 1; // Keep looking for earlier matches
16+
} else if (currentTitle.toLowerCase() < prefix.toLowerCase()) {
17+
left = mid + 1;
18+
} else {
19+
right = mid - 1;
20+
}
21+
}
22+
23+
// Collect all albums starting with the prefix
24+
const matches = [];
25+
if (firstMatch !== -1) {
26+
for (let i = firstMatch; i < sortedCollection.length; i++) {
27+
if (sortedCollection[i].title.toLowerCase().startsWith(prefix.toLowerCase())) {
28+
matches.push(sortedCollection[i]);
29+
} else {
30+
break;
31+
}
32+
}
33+
}
34+
35+
return matches;
36+
}

src/chapters/sorting-and-binary-search/text/binary-search-fundamentals-content.md

Lines changed: 3 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -175,107 +175,14 @@ When implementing binary search, watch out for:
175175

176176
5. **Duplicate Elements:** Standard binary search finds any matching element, not necessarily the first or last occurrence.
177177

178-
## Binary Search Challenges at Groove Records
179-
180-
As Sam implemented binary search in his daily operations, he encountered some practical challenges that taught Alex important lessons:
181178

182179
### Handling Partial Matches
183-
```javascript
184-
// Customer asks for "Hotel" but the full title is "Hotel California"
185-
function findAlbumsStartingWith(sortedCollection, prefix) {
186-
// Find the first album that starts with the prefix
187-
let left = 0;
188-
let right = sortedCollection.length - 1;
189-
let firstMatch = -1;
190-
191-
while (left <= right) {
192-
const mid = Math.floor(left + (right - left) / 2);
193-
const currentTitle = sortedCollection[mid].title;
194-
195-
if (currentTitle.toLowerCase().startsWith(prefix.toLowerCase())) {
196-
firstMatch = mid;
197-
right = mid - 1; // Keep looking for earlier matches
198-
} else if (currentTitle.toLowerCase() < prefix.toLowerCase()) {
199-
left = mid + 1;
200-
} else {
201-
right = mid - 1;
202-
}
203-
}
204-
205-
// Collect all albums starting with the prefix
206-
const matches = [];
207-
if (firstMatch !== -1) {
208-
for (let i = firstMatch; i < sortedCollection.length; i++) {
209-
if (sortedCollection[i].title.toLowerCase().startsWith(prefix.toLowerCase())) {
210-
matches.push(sortedCollection[i]);
211-
} else {
212-
break;
213-
}
214-
}
215-
}
216-
217-
return matches;
218-
}
219-
```
220-
221-
### Case-Insensitive Search
222-
```javascript
223-
// Handle different capitalization in search terms
224-
function findAlbumCaseInsensitive(sortedCollection, targetTitle) {
225-
let left = 0;
226-
let right = sortedCollection.length - 1;
227-
228-
while (left <= right) {
229-
const mid = Math.floor(left + (right - left) / 2);
230-
const currentTitle = sortedCollection[mid].title.toLowerCase();
231-
const target = targetTitle.toLowerCase();
232-
233-
if (currentTitle === target) {
234-
return sortedCollection[mid];
235-
} else if (currentTitle < target) {
236-
left = mid + 1;
237-
} else {
238-
right = mid - 1;
239-
}
240-
}
241-
242-
return null;
243-
}
244-
```
245-
246-
### Price Range Searches
247-
248-
```javascript
249-
// Find all albums in a specific price range
250-
function findAlbumsInPriceRange(collectionSortedByPrice, minPrice, maxPrice) {
251-
// Find first album >= minPrice
252-
const startIndex = findInsertionPoint(collectionSortedByPrice, minPrice, 'price');
253-
// Find first album > maxPrice
254-
const endIndex = findInsertionPoint(collectionSortedByPrice, maxPrice + 0.01, 'price');
255-
256-
return collectionSortedByPrice.slice(startIndex, endIndex);
257-
}
258-
259-
function findInsertionPoint(sortedCollection, targetValue, property) {
260-
let left = 0;
261-
let right = sortedCollection.length;
262-
263-
while (left < right) {
264-
const mid = Math.floor(left + (right - left) / 2);
265-
if (sortedCollection[mid][property] < targetValue) {
266-
left = mid + 1;
267-
} else {
268-
right = mid;
269-
}
270-
}
271-
272-
return left;
273-
}
274-
```
275180

276181
"Binary search isn't just about finding exact matches," Alex realized. "It's about efficiently navigating any sorted data to answer complex questions. The core principle stays the same - divide and conquer - but the implementation details matter for creating a great customer experience."
277182

278-
Sam was delighted. "Now I can instantly answer customer questions like 'What albums do you have under $20?' or 'Show me everything from the 1980s.' My customers love how quickly I can help them!"
183+
Sam was delighted. "Now I can instantly answer all kinds of customer questions and sell more records!"
184+
185+
Try out a new challenge in the exercise column. This is a difficult challenge, so try your best to follow the comments and implement that logic.
279186

280187
## Real-World Applications
281188

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
2+
function findAlbumsStartingWith(sortedCollection, prefix) {
3+
4+
// start by setting left and right pointers to the first and last indices of our collection,
5+
// set firstMatch to -1, a placeholder
6+
7+
while (left <= right) {
8+
9+
//set a variable called mid, to the middle index between our pointers
10+
//set a variable currentTitle to the title property of the element at mid
11+
12+
//check if that title starts with the specified prefix string (bonus: make it case insensitive)
13+
//if so, set firstMatch to this element, then move the right pointer just left of mid to search for an earlier match
14+
//if the currentTitle is before ( < ) the prefix, set left to right after mid to search the right half next
15+
//otherwise, set right to just before mid to search the left half next
16+
}
17+
18+
// Collect all albums starting with the prefix
19+
const matches = [];
20+
if (firstMatch !== -1) {
21+
//if we found at least one match, iterate through the collection starting with that and keep adding records as long as they start with the prefix
22+
}
23+
24+
return matches;
25+
}
26+
27+
28+
////// Some test data and calls to test out these functions
29+
const vinylCollection = [
30+
{
31+
title: "Abbey Road",
32+
artist: "The Beatles",
33+
},
34+
{
35+
title: "Back in Black",
36+
artist: "AC/DC",
37+
},
38+
{
39+
title: "Born to Run",
40+
artist: "Bruce Springsteen",
41+
},
42+
{
43+
title: "Born in the U.S.A.",
44+
artist: "Bruce Springsteen",
45+
},
46+
{
47+
title: "Brothers in Arms",
48+
artist: "Dire Straits",
49+
},
50+
{
51+
title: "Dark Side of the Moon",
52+
artist: "Pink Floyd",
53+
},
54+
{
55+
title: "Desperado",
56+
artist: "Eagles",
57+
},
58+
{
59+
title: "Fleetwood Mac",
60+
artist: "Fleetwood Mac",
61+
},
62+
{
63+
title: "Hotel California",
64+
artist: "Eagles",
65+
},
66+
{
67+
title: "Houses of the Holy",
68+
artist: "Led Zeppelin",
69+
},
70+
{
71+
title: "Hysteria",
72+
artist: "Def Leppard",
73+
},
74+
{
75+
title: "Led Zeppelin IV",
76+
artist: "Led Zeppelin",
77+
},
78+
{
79+
title: "Let It Be",
80+
artist: "The Beatles",
81+
},
82+
{
83+
title: "Let It Bleed",
84+
artist: "The Rolling Stones",
85+
},
86+
{
87+
title: "London Calling",
88+
artist: "The Clash",
89+
},
90+
{
91+
title: "Nevermind",
92+
artist: "Nirvana",
93+
},
94+
{
95+
title: "Physical Graffiti",
96+
artist: "Led Zeppelin",
97+
},
98+
{
99+
title: "Purple Rain",
100+
artist: "Prince",
101+
},
102+
{
103+
title: "Rumours",
104+
artist: "Fleetwood Mac",
105+
},
106+
{
107+
title: "Sgt. Pepper's Lonely Hearts Club Band",
108+
artist: "The Beatles",
109+
},
110+
{
111+
title: "The Wall",
112+
artist: "Pink Floyd",
113+
},
114+
{
115+
title: "Thriller",
116+
artist: "Michael Jackson",
117+
},
118+
{
119+
title: "Who's Next",
120+
artist: "The Who",
121+
}
122+
];
123+
124+
// Customer asks for "ho" and should find Hotel California and Houses of the Holy
125+
const albums = findAlbumsStartingWith(vinylCollection, "ho");
126+
console.log(albums);

0 commit comments

Comments
 (0)