Skip to content

Commit b5fd9c9

Browse files
committed
feat: examples from chapter 6
1 parent 95953f2 commit b5fd9c9

3 files changed

Lines changed: 38 additions & 1 deletion

File tree

chapter-4/2_quadratic_problem.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ function hasDuplicateValueQuadratic(array: number[]): boolean { // this function
1313
}
1414

1515
function hasDuplicateValueLinear(array: number[]): boolean { // This function has efficiency O(N), because we are only running comparisons through one loop
16-
let existingNumbers: (number | undefined)[] = [] // will be something like [undefined, 1, undefined, undefined, 1], where "1" marks the if the number in that index has been seen
16+
let existingNumbers: (number | undefined)[] = [] // will be something like [undefined, 1, undefined, undefined, 1], where "1" marks the place if the number in that index has been seen
1717
for (let i = 0; i < array.length; i++) {
1818
if (existingNumbers[array[i]] === 1) {
1919
return true

chapter-6/1_insertion_sort.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
def insertion_sort(array):
2+
for index in range(1, len(array)):
3+
temp_value = array[index]
4+
position = index - 1
5+
6+
while position >= 0:
7+
if array[position] > temp_value:
8+
array[position + 1] = array[position]
9+
position = position - 1
10+
else:
11+
break
12+
array[position + 1] = temp_value
13+
return array

chapter-6/2_intersection.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
function intersection(firstArray, secondArray){
2+
let result = []
3+
4+
for (let i = 0; i < firstArray.length; i++) {
5+
for (let j = 0; j < secondArray.length; j++) {
6+
if (firstArray[i] === secondArray[j]) {
7+
result.push(firstArray[importScripts])
8+
}
9+
}
10+
}
11+
}
12+
13+
function intersection(firstArray, secondArray){
14+
let result = []
15+
16+
for (let i = 0; i < firstArray.length; i++) {
17+
for (let j = 0; j < secondArray.length; j++) {
18+
if (firstArray[i] === secondArray[j]) {
19+
result.push(firstArray[importScripts])
20+
break
21+
}
22+
}
23+
}
24+
}

0 commit comments

Comments
 (0)