Skip to content

Commit 25a4dff

Browse files
authored
Merge pull request loiane#80 from loiane/third-edition
[Algorithm Design and Techniques]
2 parents 03f832a + 873f929 commit 25a4dff

32 files changed

+2883
-2470
lines changed

.eslintrc.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
"no-return-assign": 0,
3030
"no-restricted-globals": 0,
3131
"no-multi-assign": 0,
32-
"prefer-destructuring": ["error", {"object": true, "array": false}]
32+
"prefer-destructuring": ["error", {"object": true, "array": false}],
33+
"padded-blocks": 0
3334
}
3435
}

examples/PacktDataStructuresAlgorithms.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/PacktDataStructuresAlgorithms.min.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
const { lcs } = PacktDataStructuresAlgorithms;
2+
const { lcsPrint } = PacktDataStructuresAlgorithms;
3+
4+
const wordX = 'acbaed';
5+
const wordY = 'abcadf';
6+
7+
console.log('lcs', lcs(wordX, wordY));
8+
console.log('lcsPrint', lcsPrint(wordX, wordY));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
const { lcsRecursive } = PacktDataStructuresAlgorithms;
2+
3+
const wordX = 'acbaed';
4+
const wordY = 'abcadf';
5+
6+
console.log('lcsRecursive', lcsRecursive(wordX, wordY));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
const { matrixChainOrder } = PacktDataStructuresAlgorithms;
2+
3+
const p = [10, 100, 5, 50, 1];
4+
console.log(matrixChainOrder(p));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
const { matrixChainOrderGreedy } = PacktDataStructuresAlgorithms;
2+
3+
const p = [10, 100, 5, 50, 1];
4+
console.log(matrixChainOrderGreedy(p));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
console.log('Using imperative JS');
2+
3+
var printArray = function(array){
4+
for (var i=0; i<array.length; i++){
5+
console.log(array[i]);
6+
}
7+
};
8+
9+
printArray([1, 2, 3, 4, 5]);
10+
11+
//how can we abstract the For flow? Can we use a callback for action?
12+
13+
console.log('Using functional JS');
14+
15+
var forEach = function(array, action){
16+
for (var i=0; i<array.length; i++){
17+
action(array[i]);
18+
}
19+
};
20+
21+
var logItem = function (item) {
22+
console.log(item);
23+
};
24+
25+
forEach([1, 2, 3, 4, 5], logItem);
26+
27+
//how can we abstract the For flow?
28+
console.log('Finding the min value in an array - imperative');
29+
30+
var findMinArray = function(array){
31+
var minValue = array[0];
32+
for (var i=1; i<array.length; i++){
33+
if (minValue > array[i]){
34+
minValue = array[i];
35+
}
36+
}
37+
38+
return minValue;
39+
};
40+
41+
console.log(findMinArray([8,6,4,5,9]));
42+
43+
console.log('Finding the min value in an array - functional ES2015');
44+
const min_ = function(array){
45+
return Math.min(...array)
46+
};
47+
48+
//simplifying using arrow functions
49+
const min = arr => Math.min(...arr);
50+
51+
console.log(min_([8,6,4,5,9]));
52+
console.log(min([8,6,4,5,9]));
53+
54+
//concat + reduce
55+
console.log('merge arrays - imperative');
56+
57+
var mergeArrays_ = function(arrays){
58+
var count = arrays.length,
59+
newArray = [],
60+
k =0;
61+
for (var i=0; i<count; i++){
62+
for (var j=0; j<arrays[i].length; j++){
63+
newArray[k++] = arrays[i][j];
64+
}
65+
}
66+
return newArray;
67+
};
68+
69+
console.log(mergeArrays_([[1, 2, 3], [4, 5], [6]]));
70+
71+
console.log('merge arrays - using concat');
72+
var mergeArraysConcat = function(arrays){
73+
return arrays.reduce( function(p,n){
74+
return p.concat(n);
75+
});
76+
};
77+
78+
console.log(mergeArraysConcat([[1, 2, 3], [4, 5], [6]]));
79+
80+
console.log('merge arrays - ES2015');
81+
82+
const mergeArrays = (...arrays) => [].concat(...arrays);
83+
console.log(mergeArrays([1, 2, 3], [4, 5], [6]));
84+
85+
console.log('sum values of arrays - imperative');
86+
var sumValues = function(array){
87+
var total = array[0];
88+
for (var i=1; i<array.length; i++){
89+
total += array[i];
90+
}
91+
return total;
92+
};
93+
94+
console.log(sumValues([1, 2, 3, 4, 5]));
95+
96+
//reduce
97+
console.log('sum values of arrays - functional');
98+
var sum_ = function(array){
99+
return array.reduce(function(a, b){
100+
return a + b;
101+
})
102+
};
103+
104+
console.log(sum_([1, 2, 3, 4, 5]));
105+
106+
console.log('sum values of arrays - ES2015');
107+
const sum = arr => arr.reduce((a, b) => a + b);
108+
109+
console.log(sum([1, 2, 3, 4, 5]));
110+
111+
//map
112+
var daysOfWeek = [
113+
{name: 'Monday', value: 1},
114+
{name: 'Tuesday', value: 2},
115+
{name: 'Wednesday', value: 7}
116+
];
117+
118+
var daysOfWeekValues_ = [];
119+
for (var i = 0; i < daysOfWeek.length; i++) {
120+
daysOfWeekValues_.push(daysOfWeek[i].value);
121+
}
122+
123+
//to
124+
var daysOfWeekValues = daysOfWeek.map(function(day) {
125+
return day.value;
126+
});
127+
console.log(daysOfWeekValues);
128+
129+
130+
//filter
131+
var positiveNumbers_ = function(array){
132+
var positive = [];
133+
for (var i = 0; i < array.length; i++) {
134+
if (array[i] >= 0){
135+
positive.push(array[i]);
136+
}
137+
}
138+
return positive;
139+
}
140+
console.log(positiveNumbers_([-1,1,2,-2]));
141+
142+
var positiveNumbers = function(array){
143+
return array.filter(function(num){
144+
return num >= 0;
145+
})
146+
};
147+
console.log(positiveNumbers([-1,1,2,-2]));

0 commit comments

Comments
 (0)