X Tutup
Skip to content

Commit fcd2b07

Browse files
committed
Merge branch 'master' of github.com:mgechev/javascript-algorithms
* 'master' of github.com:mgechev/javascript-algorithms: use https instead of ssh fibonacci spec and throw on too large of input. Fibonacci algorithm LZW Encoding/Decoding
2 parents f34b55a + 754579f commit fcd2b07

File tree

4 files changed

+165
-2
lines changed

4 files changed

+165
-2
lines changed

readme.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ npm install
2323
- Go to the parent directory of the `javascript-algorithms` folder and call:
2424

2525
```bash
26-
git clone git@github.com:mgechev/javascript-algorithms.git javascript-algorithms-docs
26+
git clone https://github.com/mgechev/javascript-algorithms.git javascript-algorithms-docs
2727
```
2828

2929
- Go to the `javascript-algorithms-docs` folder and change current branch to `gh-pages`:
@@ -81,4 +81,4 @@ If the build is not successful fix your code in order the tests and jshint valid
8181

8282
## License
8383

84-
The code in this repository is distributed under the terms of the MIT license.
84+
The code in this repository is distributed under the terms of the MIT license.

src/compression/LZW/LZW.js

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/**
2+
* LZW Encoding/Decoding
3+
*
4+
* Lempel–Ziv–Welch (LZW) is a universal lossless data
5+
* compression algorithm. It is an improved implementation
6+
* of the LZ78 algorithm.
7+
*
8+
* @example
9+
* var lzwModule = require('path-to-algorithms/src/compression'+
10+
* '/LZW/LZW');
11+
* var lzw = new lzwModule.LZW();
12+
*
13+
* var compressed = lzw.compress("ABCABCABCABCABCABC");
14+
* console.log(compressed);
15+
*
16+
* var decompressed = lzw.decompress(compressed);
17+
* console.log(decompressed);
18+
*
19+
* @module compression/LZW/LZW
20+
*/
21+
(function (exports) {
22+
'use strict';
23+
24+
exports.LZW = function () {
25+
this.dictionarySize = 256;
26+
};
27+
28+
exports.LZW.compress = function (data) {
29+
var i;
30+
var dictionary = {};
31+
var character;
32+
var wc;
33+
var w = '';
34+
var result = [];
35+
36+
for (i = 0; i < this.dictionarySize; i = i + 1) {
37+
dictionary[String.fromCharCode(i)] = i;
38+
}
39+
40+
for (i = 0; i < data.length; i = i + 1) {
41+
character = data.charAt(i);
42+
wc = w + character;
43+
if (dictionary.hasOwnProperty(wc)) {
44+
w = wc;
45+
} else {
46+
result.push(dictionary[w]);
47+
dictionary[wc] = this.dictionarySize;
48+
this.dictionarySize = this.dictionarySize + 1;
49+
w = String(character);
50+
}
51+
}
52+
53+
if (w !== '') {
54+
result.push(dictionary[w]);
55+
}
56+
57+
return result;
58+
};
59+
60+
exports.LZW.decompress = function (compressedData) {
61+
var i;
62+
var dictionary = [];
63+
var w;
64+
var result;
65+
var key;
66+
var entry = '';
67+
68+
for (i = 0; i < this.dictionarySize; i = i + 1) {
69+
dictionary[i] = String.fromCharCode(i);
70+
}
71+
72+
w = String.fromCharCode(compressedData[0]);
73+
result = w;
74+
75+
for (i = 1; i < compressedData.length; i = i + 1) {
76+
key = compressedData[i];
77+
if (dictionary[key]) {
78+
entry = dictionary[key];
79+
} else {
80+
if (key === this.dictionarySize) {
81+
entry = w + w.charAt(0);
82+
} else {
83+
return null;
84+
}
85+
}
86+
87+
result += entry;
88+
dictionary[this.dictionarySize] = w + entry.charAt(0);
89+
this.dictionarySize = this.dictionarySize + 1;
90+
w = entry;
91+
}
92+
93+
return result;
94+
};
95+
})(typeof window === 'undefined' ? module.exports : window);

src/others/fibonacci.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* Nth number of fibonacci's sequence
3+
*
4+
* Returns the nth number of fibonacci's sequence.
5+
*
6+
* @public
7+
*
8+
* @example
9+
* var fibonacci = require('path-to-algorithms/src/others/fibonacci').fibonacci;
10+
* var nth = fibonacci(20);
11+
*
12+
* console.log(nth); // 6765
13+
*
14+
* @param {Number} n The nth position in fibonacci's sequence
15+
*
16+
* @module others/fibonacci
17+
*/
18+
(function (exports) {
19+
'use strict';
20+
21+
function fibonacci (n) {
22+
if (n > 97) {
23+
throw 'Input too large, results in inaccurate fibonacci value.';
24+
}
25+
var n1 = 0;
26+
var n2 = 1;
27+
var aux;
28+
29+
while (n > 0) {
30+
aux = n1;
31+
n1 = n2;
32+
n2 += aux;
33+
n = n - 1;
34+
}
35+
36+
return n1;
37+
}
38+
39+
exports.fibonacci = fibonacci;
40+
})(typeof window === 'undefined' ? module.exports : window);

test/others/fibonacci.spec.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
'use strict';
2+
3+
var mod = require('../../src/others/fibonacci.js');
4+
var fibonacci = mod.fibonacci;
5+
6+
describe('fibonacci algorithm', function () {
7+
it('should return value 1 with input 1.', function () {
8+
expect(fibonacci(1)).toBe(1);
9+
});
10+
it('should return value 1 with input 2.', function () {
11+
expect(fibonacci(2)).toBe(1);
12+
});
13+
it('should return value 2 with input 3.', function () {
14+
expect(fibonacci(3)).toBe(2);
15+
});
16+
it('should return value 3 with input 4.', function () {
17+
expect(fibonacci(4)).toBe(3);
18+
});
19+
it('should return value 5 with input 5.', function () {
20+
expect(fibonacci(5)).toBe(5);
21+
});
22+
it('should be 83621143489848422977 with input 97.', function () {
23+
expect(fibonacci(97)).toBe(83621143489848422977);
24+
});
25+
it('should throw when input is too large.', function () {
26+
expect(function () {fibonacci(98)}).toThrow('Input too large, results in inaccurate fibonacci value.');
27+
});
28+
});

0 commit comments

Comments
 (0)
X Tutup