algs4js
Version:
Basic algorithms and data structures implemented with es6
66 lines (55 loc) • 2.05 kB
JavaScript
Object.defineProperty(exports, "__esModule", {
value: true
});
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
/**
* Class ArrayUtil
*
* Provides utility functions for working with arrays of items
*/
var ArrayUtil = function () {
function ArrayUtil() {
_classCallCheck(this, ArrayUtil);
}
_createClass(ArrayUtil, null, [{
key: "randomIntArray",
/**
* Creates an array of random positive integers
*
* @param {Number} size the size of the array
* @param {Number} MaxValue the maximum value of an element of the array
* @return {Object} the array of positive integers
*/
value: function randomIntArray(size, maxValue) {
var arr = Array.from({
length: size
}, function () {
return Math.floor(Math.random() * maxValue);
});
return arr;
}
/**
* Verifys that an array is sorted by increasing order
*
* @param {Object} arr, the array to verify.
* @return {Boolean} true if sorted, false otherwise.
*/
}, {
key: "isSorted",
value: function isSorted(arr) {
if (arr.length < 2) {
return true;
}
for (var i = 1; i < arr.length - 1; i++) {
if (arr[i] < arr[i - 1]) {
return false;
}
}
return true;
}
}]);
return ArrayUtil;
}();
exports.default = ArrayUtil;
;