Skip to content

Commit 3feffd3

Browse files
authored
Merge pull request #11 from trekhleb/master
fix longestCommonSubstring() to handle unicode characters
2 parents cb8e267 + ec6c427 commit 3feffd3

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+1233
-195
lines changed

.travis.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@ dist: trusty
33
language: node_js
44
node_js:
55
- node
6+
install:
7+
- npm install -g codecov
8+
- npm install
69
script:
710
- npm run ci
8-
- npm run codecov
11+
- codecov
912
notifications:
1013
email: false

README.md

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ _Read this in other languages:_
1919
[_Español_](README.es-ES.md),
2020
[_Português_](README.pt-BR.md)
2121

22+
*☝ Note that this project is meant to be used for learning and researching purposes
23+
only and it is **not** meant to be used for production.*
24+
2225
## Data Structures
2326

2427
A data structure is a particular way of organizing and storing data in a computer so that it can
@@ -58,7 +61,7 @@ a set of rules that precisely define a sequence of operations.
5861
* **Math**
5962
* `B` [Bit Manipulation](src/algorithms/math/bits) - set/get/update/clear bits, multiplication/division by two, make negative etc.
6063
* `B` [Factorial](src/algorithms/math/factorial)
61-
* `B` [Fibonacci Number](src/algorithms/math/fibonacci)
64+
* `B` [Fibonacci Number](src/algorithms/math/fibonacci) - classic and closed-form versions
6265
* `B` [Primality Test](src/algorithms/math/primality-test) (trial division method)
6366
* `B` [Euclidean Algorithm](src/algorithms/math/euclidean-algorithm) - calculate the Greatest Common Divisor (GCD)
6467
* `B` [Least Common Multiple](src/algorithms/math/least-common-multiple) (LCM)
@@ -67,6 +70,7 @@ a set of rules that precisely define a sequence of operations.
6770
* `B` [Pascal's Triangle](src/algorithms/math/pascal-triangle)
6871
* `B` [Complex Number](src/algorithms/math/complex-number) - complex numbers and basic operations with them
6972
* `B` [Radian & Degree](src/algorithms/math/radian) - radians to degree and backwards conversion
73+
* `B` [Fast Powering](src/algorithms/math/fast-powering)
7074
* `A` [Integer Partition](src/algorithms/math/integer-partition)
7175
* `A` [Liu Hui π Algorithm](src/algorithms/math/liu-hui) - approximate π calculations based on N-gons
7276
* `A` [Discrete Fourier Transform](src/algorithms/math/fourier-transform) - decompose a function of time (a signal) into the frequencies that make it up
@@ -105,6 +109,9 @@ a set of rules that precisely define a sequence of operations.
105109
* `B` [Shellsort](src/algorithms/sorting/shell-sort)
106110
* `B` [Counting Sort](src/algorithms/sorting/counting-sort)
107111
* `B` [Radix Sort](src/algorithms/sorting/radix-sort)
112+
* **Linked Lists**
113+
* `B` [Straight Traversal](src/algorithms/linked-list/traversal)
114+
* `B` [Reverse Traversal](src/algorithms/linked-list/reverse-traversal)
108115
* **Trees**
109116
* `B` [Depth-First Search](src/algorithms/tree/depth-first-search) (DFS)
110117
* `B` [Breadth-First Search](src/algorithms/tree/breadth-first-search) (BFS)
@@ -163,6 +170,7 @@ algorithm is an abstraction higher than a computer program.
163170
* `B` [Tree Depth-First Search](src/algorithms/tree/depth-first-search) (DFS)
164171
* `B` [Graph Depth-First Search](src/algorithms/graph/depth-first-search) (DFS)
165172
* `B` [Jump Game](src/algorithms/uncategorized/jump-game)
173+
* `B` [Fast Powering](src/algorithms/math/fast-powering)
166174
* `A` [Permutations](src/algorithms/sets/permutations) (with and without repetitions)
167175
* `A` [Combinations](src/algorithms/sets/combinations) (with and without repetitions)
168176
* **Dynamic Programming** - build up a solution using previously found sub-solutions
@@ -288,9 +296,3 @@ Below is the list of some of the most used Big O notations and their performance
288296
| **Shell sort** | n&nbsp;log(n) | depends on gap sequence | n&nbsp;(log(n))<sup>2</sup> | 1 | No | |
289297
| **Counting sort** | n + r | n + r | n + r | n + r | Yes | r - biggest number in array |
290298
| **Radix sort** | n * k | n * k | n * k | n + k | Yes | k - length of longest key |
291-
292-
## The Book
293-
294-
We’re writing a book that will clearly explain, in detail, the main algorithms.
295-
If you’d like to be notified when the “JavaScript Algorithms” book
296-
launches, [click here](https://upscri.be/402324/).

README.zh-CN.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,21 @@ _Read this in other languages:_
2222

2323
`B` - 初学者, `A` - 进阶
2424

25-
* `B` [链表](src/data-structures/linked-list)
26-
* `B` [双向链表](src/data-structures/doubly-linked-list)
27-
* `B` [队列](src/data-structures/queue)
28-
* `B` [](src/data-structures/stack)
29-
* `B` [哈希表](src/data-structures/hash-table)
30-
* `B` [](src/data-structures/heap)
31-
* `B` [优先队列](src/data-structures/priority-queue)
25+
* `B` [链表](src/data-structures/linked-list/README.zh-CN.md)
26+
* `B` [双向链表](src/data-structures/doubly-linked-list/README.zh-CN.md)
27+
* `B` [队列](src/data-structures/queue/README.zh-CN.md)
28+
* `B` [](src/data-structures/stack/README.zh-CN.md)
29+
* `B` [哈希表](src/data-structures/hash-table/README.zh-CN.md)
30+
* `B` [](src/data-structures/heap/README.zh-CN.md)
31+
* `B` [优先队列](src/data-structures/priority-queue/README.zh-CN.md)
3232
* `A` [字典树](src/data-structures/trie)
33-
* `A` [](src/data-structures/tree)
33+
* `A` [](src/data-structures/tree/README.zh-CN.md)
3434
* `A` [二叉查找树](src/data-structures/tree/binary-search-tree)
3535
* `A` [AVL 树](src/data-structures/tree/avl-tree)
3636
* `A` [红黑树](src/data-structures/tree/red-black-tree)
3737
* `A` [线段树](src/data-structures/tree/segment-tree) - 使用 最小/最大/总和 范围查询示例
3838
* `A` [树状数组](src/data-structures/tree/fenwick-tree) (二叉索引树)
39-
* `A` [](src/data-structures/graph) (有向图与无向图)
39+
* `A` [](src/data-structures/graph/README.zh-CN.md) (有向图与无向图)
4040
* `A` [并查集](src/data-structures/disjoint-set)
4141
* `A` [布隆过滤器](src/data-structures/bloom-filter)
4242

package-lock.json

Lines changed: 0 additions & 23 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66
"scripts": {
77
"lint": "eslint ./src/*",
88
"test": "jest",
9-
"ci": "npm run lint && npm run test -- --coverage",
10-
"codecov": "codecov"
9+
"ci": "npm run lint && npm run test -- --coverage"
1110
},
1211
"pre-commit": [
1312
"lint",
@@ -27,7 +26,9 @@
2726
"javascript-algorithms",
2827
"sorting-algorithms",
2928
"graph",
30-
"tree"
29+
"tree",
30+
"interview",
31+
"interview-preparation"
3132
],
3233
"author": "Oleksii Trekhleb (https://www.linkedin.com/in/trekhleb/)",
3334
"license": "MIT",
@@ -39,7 +40,6 @@
3940
"@types/jest": "^23.1.4",
4041
"babel-cli": "^6.26.0",
4142
"babel-preset-env": "^1.7.0",
42-
"codecov": "^3.0.2",
4343
"eslint": "^4.19.1",
4444
"eslint-config-airbnb": "^17.0.0",
4545
"eslint-plugin-import": "^2.13.0",
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Reversed Linked List Traversal
2+
3+
The task is to traverse the given linked list in reversed order.
4+
5+
For example for the following linked list:
6+
7+
![](https://upload.wikimedia.org/wikipedia/commons/6/6d/Singly-linked-list.svg)
8+
9+
The order of traversal should be:
10+
11+
```text
12+
37 → 99 → 12
13+
```
14+
15+
The time complexity is `O(n)` because we visit every node only once.
16+
17+
## Reference
18+
19+
- [Wikipedia](https://en.wikipedia.org/wiki/Linked_list)
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import LinkedList from '../../../../data-structures/linked-list/LinkedList';
2+
import reverseTraversal from '../reverseTraversal';
3+
4+
describe('reverseTraversal', () => {
5+
it('should traverse linked list in reverse order', () => {
6+
const linkedList = new LinkedList();
7+
8+
linkedList
9+
.append(1)
10+
.append(2)
11+
.append(3);
12+
13+
const traversedNodeValues = [];
14+
const traversalCallback = (nodeValue) => {
15+
traversedNodeValues.push(nodeValue);
16+
};
17+
18+
reverseTraversal(linkedList, traversalCallback);
19+
20+
expect(traversedNodeValues).toEqual([3, 2, 1]);
21+
});
22+
});
23+
24+
25+
// it('should reverse traversal the linked list with callback', () => {
26+
// const linkedList = new LinkedList();
27+
//
28+
// linkedList
29+
// .append(1)
30+
// .append(2)
31+
// .append(3);
32+
//
33+
// expect(linkedList.toString()).toBe('1,2,3');
34+
// expect(linkedList.reverseTraversal(linkedList.head, value => value * 2)).toEqual([6, 4, 2]);
35+
// expect(() => linkedList.reverseTraversal(linkedList.head)).toThrow();
36+
// });
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* Traversal callback function.
3+
* @callback traversalCallback
4+
* @param {*} nodeValue
5+
*/
6+
7+
/**
8+
* @param {LinkedListNode} node
9+
* @param {traversalCallback} callback
10+
*/
11+
function reverseTraversalRecursive(node, callback) {
12+
if (node) {
13+
reverseTraversalRecursive(node.next, callback);
14+
callback(node.value);
15+
}
16+
}
17+
18+
/**
19+
* @param {LinkedList} linkedList
20+
* @param {traversalCallback} callback
21+
*/
22+
export default function reverseTraversal(linkedList, callback) {
23+
reverseTraversalRecursive(linkedList.head, callback);
24+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Linked List Traversal
2+
3+
The task is to traverse the given linked list in straight order.
4+
5+
For example for the following linked list:
6+
7+
![](https://upload.wikimedia.org/wikipedia/commons/6/6d/Singly-linked-list.svg)
8+
9+
The order of traversal should be:
10+
11+
```text
12+
12 → 99 → 37
13+
```
14+
15+
The time complexity is `O(n)` because we visit every node only once.
16+
17+
## Reference
18+
19+
- [Wikipedia](https://en.wikipedia.org/wiki/Linked_list)
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import LinkedList from '../../../../data-structures/linked-list/LinkedList';
2+
import traversal from '../traversal';
3+
4+
describe('traversal', () => {
5+
it('should traverse linked list', () => {
6+
const linkedList = new LinkedList();
7+
8+
linkedList
9+
.append(1)
10+
.append(2)
11+
.append(3);
12+
13+
const traversedNodeValues = [];
14+
const traversalCallback = (nodeValue) => {
15+
traversedNodeValues.push(nodeValue);
16+
};
17+
18+
traversal(linkedList, traversalCallback);
19+
20+
expect(traversedNodeValues).toEqual([1, 2, 3]);
21+
});
22+
});

0 commit comments

Comments
 (0)