Skip to content

Commit fe1c06c

Browse files
committed
update
1 parent 6d68d93 commit fe1c06c

10 files changed

+8936
-1091
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import Autocomplete from '../autocomplete-typeahead-search-brute-force';
2+
3+
const states = [
4+
'Alabama', 'Alaska', 'Arizona', 'Arkansas', 'California', 'Colorado', 'Connecticut',
5+
'Delaware', 'Florida', 'Georgia', 'Hawaii', 'Idaho', 'Illinois', 'Indiana', 'Iowa', 'Kansas',
6+
'Kentucky', 'Louisiana', 'Maine', 'Maryland', 'Massachusetts', 'Michigan', 'Minnesota',
7+
'Mississippi', 'Missouri', 'Montana', 'Nebraska', 'Nevada', 'New Hampshire', 'New Jersey',
8+
'New Mexico', 'New York', 'North Carolina', 'North Dakota', 'Ohio', 'Oklahoma', 'Oregon',
9+
'Pennsylvania', 'Rhode Island', 'South Carolina', 'South Dakota', 'Tennessee', 'Texas', 'Utah',
10+
'Vermont', 'Virginia', 'Washington', 'West Virginia', 'Wisconsin', 'Wyoming'
11+
];
12+
13+
describe('Brute Force', () => {
14+
15+
let autocomplete;
16+
17+
beforeAll(() => {
18+
autocomplete = new Autocomplete(states);
19+
});
20+
21+
test('should return an array', () => {
22+
const result = autocomplete.getAutocompleteList('');
23+
expect(result).toBeInstanceOf(Array);
24+
});
25+
26+
test('should return an empty array when there is no match', () => {
27+
const result = autocomplete.getAutocompleteList('q');
28+
expect(result).toHaveLength(0);
29+
});
30+
31+
test('should return a list of possible values', () => {
32+
const result = autocomplete.getAutocompleteList('New');
33+
const expected = [ 'New Hampshire', 'New Jersey', 'New Mexico', 'New York' ];
34+
expect(result).toEqual(expected);
35+
});
36+
37+
test('should return an array containing exact one element', () => {
38+
const result = autocomplete.getAutocompleteList('New York');
39+
const expected = [ 'New York' ];
40+
expect(result).toEqual(expected);
41+
});
42+
43+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
import Autocomplete from '../autocomplete-typeahead-search-trie';
2+
3+
const states = [
4+
'Alabama', 'Alaska', 'Arizona', 'Arkansas', 'California', 'Colorado', 'Connecticut',
5+
'Delaware', 'Florida', 'Georgia', 'Hawaii', 'Idaho', 'Illinois', 'Indiana', 'Iowa', 'Kansas',
6+
'Kentucky', 'Louisiana', 'Maine', 'Maryland', 'Massachusetts', 'Michigan', 'Minnesota',
7+
'Mississippi', 'Missouri', 'Montana', 'Nebraska', 'Nevada', 'New Hampshire', 'New Jersey',
8+
'New Mexico', 'New York', 'North Carolina', 'North Dakota', 'Ohio', 'Oklahoma', 'Oregon',
9+
'Pennsylvania', 'Rhode Island', 'South Carolina', 'South Dakota', 'Tennessee', 'Texas', 'Utah',
10+
'Vermont', 'Virginia', 'Washington', 'West Virginia', 'Wisconsin', 'Wyoming'
11+
];
12+
13+
describe('Autocomplete using Trie', () => {
14+
15+
let autocomplete;
16+
17+
beforeAll(() => {
18+
autocomplete = new Autocomplete(states);
19+
});
20+
21+
test('should build a trie', () => {
22+
const words = [
23+
'apple',
24+
'cat',
25+
'car',
26+
'app',
27+
];
28+
const autocomplete = new Autocomplete(words);
29+
const result = JSON.stringify(autocomplete.root);
30+
const expected = JSON.stringify({
31+
children: {
32+
a: {
33+
children: {
34+
p: {
35+
children: {
36+
p: {
37+
children: {
38+
l: {
39+
children: {
40+
e: {
41+
children: {},
42+
last: true,
43+
},
44+
},
45+
last: false,
46+
},
47+
},
48+
last: true,
49+
},
50+
},
51+
last: false,
52+
},
53+
},
54+
last: false,
55+
},
56+
c: {
57+
children: {
58+
a: {
59+
children: {
60+
t: {
61+
children: {},
62+
last: true,
63+
},
64+
r: {
65+
children: {},
66+
last: true,
67+
},
68+
},
69+
last: false,
70+
},
71+
},
72+
last: false,
73+
}
74+
},
75+
last: false,
76+
});
77+
78+
expect(result).toEqual(expected);
79+
});
80+
81+
describe('Depth First Search', () => {
82+
83+
test('should return an array', () => {
84+
const result = autocomplete.getAutocompleteListDfs('');
85+
expect(result).toBeInstanceOf(Array);
86+
});
87+
88+
test('should return an empty array when there is no match', () => {
89+
const result = autocomplete.getAutocompleteListDfs('q');
90+
expect(result).toHaveLength(0);
91+
});
92+
93+
test('should return a list of possible values', () => {
94+
const result = autocomplete.getAutocompleteListDfs('New');
95+
const expected = [ 'New Hampshire', 'New Jersey', 'New Mexico', 'New York' ];
96+
expect(result).toEqual(expected);
97+
});
98+
99+
test('should return an array containing exact one element', () => {
100+
const result = autocomplete.getAutocompleteListDfs('New York');
101+
const expected = [ 'New York' ];
102+
expect(result).toEqual(expected);
103+
});
104+
105+
});
106+
107+
describe('Breath First Search', () => {
108+
109+
test('should return an array', () => {
110+
const result = autocomplete.getAutocompleteListBfs('');
111+
expect(result).toBeInstanceOf(Array);
112+
});
113+
114+
test('should return an empty array when there is no match', () => {
115+
const result = autocomplete.getAutocompleteListBfs('q');
116+
expect(result).toHaveLength(0);
117+
});
118+
119+
test('should return a list of possible values', () => {
120+
const result = autocomplete.getAutocompleteListBfs('New');
121+
const expected = [ 'New York', 'New Jersey', 'New Mexico', 'New Hampshire' ];
122+
expect(result).toEqual(expected);
123+
});
124+
125+
test('should return an array containing exact one element', () => {
126+
const result = autocomplete.getAutocompleteListBfs('New York');
127+
const expected = [ 'New York' ];
128+
expect(result).toEqual(expected);
129+
});
130+
131+
});
132+
133+
134+
});

__test__/maxStockProfit.test.js

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import maxStockProfit from '../maxStockProfit';
2+
3+
describe('maxStockProfit', () => {
4+
5+
it('should return 22 for input [32, 46, 26, 38, 40, 48, 42]', () => {
6+
const result = maxStockProfit([32, 46, 26, 38, 40, 48, 42]);
7+
const expected = 22;
8+
9+
expect(result).toStrictEqual(22);
10+
});
11+
12+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
const states = [
2+
'Alabama', 'Alaska', 'Arizona', 'Arkansas', 'California', 'Colorado', 'Connecticut',
3+
'Delaware', 'Florida', 'Georgia', 'Hawaii', 'Idaho', 'Illinois', 'Indiana', 'Iowa', 'Kansas',
4+
'Kentucky', 'Louisiana', 'Maine', 'Maryland', 'Massachusetts', 'Michigan', 'Minnesota',
5+
'Mississippi', 'Missouri', 'Montana', 'Nebraska', 'Nevada', 'New Hampshire', 'New Jersey',
6+
'New Mexico', 'New York', 'North Carolina', 'North Dakota', 'Ohio', 'Oklahoma', 'Oregon',
7+
'Pennsylvania', 'Rhode Island', 'South Carolina', 'South Dakota', 'Tennessee', 'Texas', 'Utah',
8+
'Vermont', 'Virginia', 'Washington', 'West Virginia', 'Wisconsin', 'Wyoming'
9+
];
10+
11+
class AutocompleteBruteForce {
12+
constructor(list) {
13+
this.list = list;
14+
}
15+
16+
/**
17+
* Check if the string starts with another string
18+
* @param {string} word
19+
* @param {string} input
20+
* @return {boolean} whether or not the word starts with input
21+
* @time complexity O(1)
22+
* @space complexity O(1)
23+
*/
24+
static isStartsWith(word, input) {
25+
return word.toUpperCase().startsWith(input.toUpperCase());
26+
}
27+
28+
/**
29+
* Get an array of possible values
30+
* @param {string} input
31+
* @return {string[]} an array of possible values
32+
* @time complexity O(M) where M is the size of list
33+
* @space complexity O(M) where M is the size of list
34+
*/
35+
getAutocompleteList(input) {
36+
// RETURN EMPTY ARRAY IF INPUT IS EMPTY
37+
if (!input) {
38+
return [];
39+
}
40+
41+
// HOLD ALL POSSIBLE VALUES
42+
const possibleValues = [];
43+
44+
for (let i = 0; i < this.list.length; i++) {
45+
// ADD TO THE RETURN ARRAY IF THE ITEM STARTS WITH THE INPUT
46+
if (AutocompleteBruteForce.isStartsWith(this.list[i], input)) {
47+
possibleValues.push(this.list[i]);
48+
}
49+
}
50+
51+
return possibleValues;
52+
}
53+
}
54+
55+
// /**
56+
// * Get a list of possible states
57+
// * @param {string[]} states
58+
// * @param {string} input
59+
// * @return {string[]} an array of states
60+
// * @time complexity O(M) where M is the size of states
61+
// * @space complexity O(M) where M s the size of states
62+
// */
63+
// const bruteForce = (states, input) => {
64+
// const list = [];
65+
66+
// // CONVERT TO UPPERCASE
67+
// // SO THEY ARE COMPARING IN SAME LETTER CASE OR CASE INSENSITIVE
68+
// input = input.toUpperCase();
69+
70+
// for (let i = 0; i < states.length; i++) {
71+
// // CHECK IF THE STATE STARTS WITH INPUT
72+
// if (states[i].toUpperCase().startsWith(input)) {
73+
// // ADD TO THE LIST
74+
// list.push(states[i]);
75+
// }
76+
// }
77+
78+
// return list;
79+
// };
80+
81+
// export default bruteForce;
82+
export default AutocompleteBruteForce;
83+
84+
85+

0 commit comments

Comments
 (0)