Skip to content

Commit 4a78b0b

Browse files
committed
Merge pull request mgechev#26 from AndreyGeonya/doc-pack7
Doc pack7
2 parents ea88a54 + 6a14fca commit 4a78b0b

9 files changed

Lines changed: 146 additions & 51 deletions

‎doc-config.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"./src/graphs/shortest-path/",
1010
"./src/graphs/spanning-trees/",
1111
"./src/data-structures/",
12+
"./src/combinatorics/",
1213
"./src/primes/"
1314
],
1415
"includePattern": ".+\\.js(doc)?$",

‎src/combinatorics/cartesianproduct.js‎

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,29 @@
1414
}
1515
}
1616

17+
/**
18+
* Calculates Cartesian product of provided sets.
19+
*
20+
* @module combinatorics/cartesianproduct
21+
* @public
22+
* @param {Array} sets Array of sets.
23+
* @return {Array} Cartesian product of provided sets.
24+
*
25+
* @example
26+
* var product = require('path-to-algorithms/src/combinatorics/' +
27+
* 'cartesianproduct').cartesianProduct;
28+
* var result = product([[1, 2, 3], [3, 2, 1]]);
29+
* // [ [ 1, 3 ],
30+
* // [ 1, 2 ],
31+
* // [ 1, 1 ],
32+
* // [ 2, 3 ],
33+
* // [ 2, 2 ],
34+
* // [ 2, 1 ],
35+
* // [ 3, 3 ],
36+
* // [ 3, 2 ],
37+
* // [ 3, 1 ] ]
38+
* console.log(result);
39+
*/
1740
return function (sets) {
1841
result = [];
1942
cartesianProduct(sets, 0, []);
@@ -23,4 +46,4 @@
2346

2447
exports.cartesianProduct = cartesianProduct;
2548

26-
}(typeof exports === 'undefined' ? window : exports));
49+
}((typeof window === 'undefined') ? module.exports : window));

‎src/combinatorics/combinations.js‎

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,30 @@
1515
}
1616
}
1717

18+
/**
19+
* Finds all the combinations of given array.<br><br>
20+
* A combination is a way of selecting members from a grouping,
21+
* such that (unlike permutations) the order of selection does not matter.
22+
* For example given three fruits, say an apple, an orange and a pear,
23+
* there are three combinations of two that can be drawn from this set:
24+
* an apple and a pear; an apple and an orange; or a pear and an orange.
25+
*
26+
* @example
27+
*
28+
* var combinations = require('path-to-algorithms/src/' +
29+
* 'combinatorics/combinations').combinations;
30+
* var result = combinations(['apple', 'orange', 'pear'], 2);
31+
* // [['apple', 'orange'],
32+
* // ['apple', 'pear'],
33+
* // ['orange', 'pear']]
34+
* console.log(result);
35+
*
36+
* @module combinatorics/combinations
37+
* @public
38+
* @param arr {Array} Set of items.
39+
* @param k {Number} Size of each combination.
40+
* @return {Array} Returns all combinations.
41+
*/
1842
return function (arr, k) {
1943
res = [];
2044
combinations(arr, k, 0, 0, []);
@@ -27,4 +51,4 @@
2751

2852
exports.combinations = combinations;
2953

30-
}(typeof exports === 'undefined' ? window : exports));
54+
}((typeof window === 'undefined') ? module.exports : window));

‎src/combinatorics/permutations.js‎

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,6 @@
1010
arr[j] = temp;
1111
}
1212

13-
/**
14-
* Finds all the permutations of given array.
15-
* Complexity O(n*n!).
16-
*
17-
* @param {Array} arr Array to find the permutations of
18-
* @param {Number} current Current element
19-
* @returns {Array} Array containing all the permutations
20-
*/
2113
function permutations(arr, current) {
2214
if (current >= arr.length) {
2315
return res.push(arr.slice());
@@ -29,6 +21,34 @@
2921
}
3022
}
3123

24+
/**
25+
* Finds all the permutations of given array.<br><br>
26+
* Permutation relates to the act of rearranging, or permuting,
27+
* all the members of a set into some sequence or order.
28+
* For example there are six permutations of the set {1,2,3}, namely:
29+
* (1,2,3), (1,3,2), (2,1,3), (2,3,1), (3,1,2), and (3,2,1).<br><br>
30+
* Complexity: O(N*N!).
31+
*
32+
* @example
33+
*
34+
* var permutations = require('path-to-algorithms/src/' +
35+
* 'combinatorics/permutations').permutations;
36+
* var result = permutations(['apple', 'orange', 'pear']);
37+
*
38+
* // [ [ 'apple', 'orange', 'pear' ],
39+
* // [ 'apple', 'pear', 'orange' ],
40+
* // [ 'orange', 'apple', 'pear' ],
41+
* // [ 'orange', 'pear', 'apple' ],
42+
* // [ 'pear', 'orange', 'apple' ],
43+
* // [ 'pear', 'apple', 'orange' ] ]
44+
* console.log(result);
45+
*
46+
* @module combinatorics/permutations
47+
* @public
48+
* @param {Array} arr Array to find the permutations of.
49+
* @param {Number} current Current element.
50+
* @returns {Array} Array containing all the permutations.
51+
*/
3252
return function (arr) {
3353
res = [];
3454
permutations(arr, 0);
@@ -41,4 +61,4 @@
4161

4262
exports.permutations = permutations;
4363

44-
}(typeof exports === 'undefined' ? window : exports));
64+
}((typeof window === 'undefined') ? module.exports : window));

‎src/combinatorics/variations-repetion.js‎

Lines changed: 0 additions & 28 deletions
This file was deleted.
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
(function (exports) {
2+
'use strict';
3+
4+
var variationsWithRepetion = (function () {
5+
var res;
6+
7+
function variations(arr, k, index, current) {
8+
if (k === index) {
9+
return res.push(current.slice());
10+
}
11+
for (var i = 0; i < arr.length; i += 1) {
12+
current[index] = arr[i];
13+
variations(arr, k, index + 1, current);
14+
}
15+
}
16+
17+
/**
18+
* Finds all the variations with repetition of given array.<br><br>
19+
* Variations with repetition is the number of ways to sample k elements
20+
* from a set of elements (which may be repeated).
21+
*
22+
* @example
23+
* var variations = require('path-to-algorithms/src/combinatorics/' +
24+
* 'variations-repetition').variationsWithRepetion;
25+
* var result = variations(['apple', 'orange', 'pear'], 2);
26+
*
27+
* // [['apple', 'apple'],
28+
* // ['apple', 'orange'],
29+
* // ['apple', 'pear'],
30+
* // ['orange', 'apple'],
31+
* // ['orange', 'orange'],
32+
* // ['orange', 'pear'],
33+
* // ['pear', 'apple'],
34+
* // ['pear', 'orange'],
35+
* // ['pear', 'pear']]
36+
* console.log(result);
37+
*
38+
* @module combinatorics/variations-repetition
39+
* @public
40+
* @param arr {Array} Set of items.
41+
* @param k {Number} Size of each combination.
42+
* @return {Array} Returns all combinations.
43+
*/
44+
return function (arr, k) {
45+
res = [];
46+
variations(arr, k, 0, []);
47+
var temp = res;
48+
res = undefined;
49+
return temp;
50+
};
51+
}());
52+
53+
exports.variationsWithRepetion = variationsWithRepetion;
54+
55+
}((typeof window === 'undefined') ? module.exports : window));

‎src/primes/is-prime.js‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66
* For example for number 104743 it should return true, for 104744 - false.
77
*
88
* @module primes/is-prime
9-
* @param {Number} number - Number that we check on prime
10-
* @returns {Boolean} Will return true if provided number is prime
9+
* @param {Number} number - Number that we check on prime.
10+
* @returns {Boolean} Will return true if provided number is prime.
1111
*
1212
* @example
13-
* var isPrime = require('path/to/primes/is-prime').isPrime;
13+
* var isPrime = require('path-to-algorithms/src/is-prime').isPrime;
1414
*
1515
* console.log(isPrime(7)); // true
1616
* console.log(isPrime(18)); // false

‎src/primes/prime-factor-tree.js‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77
* [2, 3, 3].
88
*
99
* @module primes/prime-factor-tree
10-
* @param {Number} number - Number for which method will find all primes
11-
* @returns {Array} List of available primes for provided number
10+
* @param {Number} number - Number for which method will find all primes.
11+
* @returns {Array} List of available primes for provided number.
1212
*
1313
* @example
14-
* var primeFactorTree = require('path/to/primes/prime-factor-tree')
14+
* var primeFactorTree = require('path-to-algorithms/src/prime-factor-tree')
1515
* .primeFactorTree;
1616
*
1717
* console.log(primeFactorTree(18)); // [2, 3, 3]

‎src/primes/sieve-of-eratosthenes.js‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,17 @@
99
* Returns list of primes up to specified limit.
1010
*
1111
* For example, for limit 10 it should return following list of primes:
12-
* [2, 3, 5, 7]
12+
* [2, 3, 5, 7].
1313
*
1414
* @module primes/sieve-of-eratosthenes
1515
* @param {Number} limit - Algorithm will returns list of primes up to
16-
* specified limit
17-
* @returns {Array} Will return list with all prime numbers up to provided
18-
* limit
16+
* specified limit.
17+
* @returns {Array} Will return list with all prime numbers up to provided.
18+
* limit.
1919
*
2020
* @example
21-
* var sieveOfEratosthenes = require('path/to/primes/sieve-of-eratosthenes')
22-
* .sieveOfEratosthenes;
21+
* var sieveOfEratosthenes =
22+
* require('path-to-algorithms/src/sieve-of-eratosthenes').sieveOfEratosthenes;
2323
*
2424
* console.log(sieveOfEratosthenes(12)); // [2, 3, 5, 7, 11]
2525
*/

0 commit comments

Comments
 (0)