Skip to content

Instantly share code, notes, and snippets.

@mahdavipanah
Last active July 24, 2017 16:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mahdavipanah/a594bf9782610d6ffd0eb07fd3a43ffe to your computer and use it in GitHub Desktop.
Save mahdavipanah/a594bf9782610d6ffd0eb07fd3a43ffe to your computer and use it in GitHub Desktop.
Data Structures and Algorithms with JavaScript - Michael McMillan
/**
* Converts a number to another base.
*
* @param {Number} num A number to get converted. If it's float, it's integer part will get converted.
* @param {Number} [base=2] Number's new base.
*
* @returns {String} String representation of number in new base.
*
* @throws {TypeError} First parameter must be a number.
* @throws {RangeError} First Parameter must be >= 0.
*/
function convertToBase(num, base) {
if (typeof base !== 'number')
base = 2;
if (isNaN(num = parseInt(num)))
throw new TypeError("First parameter must be a number");
if (num < 0)
throw new RangeError("First Parameter must be >= 0");
var stack = [];
do {
stack.push(num % base);
num = Math.floor(num / base);
} while (num > 0);
var converted = '';
while (stack.length)
converted += stack.pop();
return converted;
}
/**
* Calculates a number's factorial using a stack.
*
* @param {Number} num
*
* @return {Number}
*
* @throws {TypeError} Parameter must be a number.
* @throws {RangeError} Parameter must be >= 0.
*/
function factorial(num) {
if (isNaN(num = parseInt(num)))
throw new TypeError("Parameter must be a number");
if (num < 0)
throw new RangeError("Parameter must be >= 0");
var stack = [];
while (num > 1)
stack.push(num--);
var fact = 1;
while (stack.length > 0)
fact *= stack.pop();
return fact;
}
/**
* Checks if the given string is palindrome.
*
* @param {String} str
*
* @returns {Boolean} True 'str' is palindrome and false otherwise.
*/
function isPalindrome(str) {
// Convert str to String and make it lower case
str = (str + '').toLowerCase();
var stack = [];
// Push characters to stack all the way to the half of the string
for (var i = 0; i < str.length / 2; i++)
stack.push(str[i]);
// If str's length odd then skip the middle character in the string
if (str.length % 2 !== 0)
stack.pop();
// Check if all the characters to end of string match
for (; i < str.length; i++)
if (stack.pop() !== str[i])
return false;
return true;
}
/**
* Sorts an array using radix sort algorithm.
*
* @example
* // Returns [15, 30, 34, 40, 180]
* radixSort([30, 15, 40, 34, 180], 3);
* @example
* // Returns [180, 40, 34, 30, 15]
* radixSort([30,15,40, 34, 180], 3, true)
*
* @param {Number[]} nums Array of numbers.
* @param {Number} len Length of biggest number in the array.
* @param {Boolean} [desc=false] Sort numbers in descending order.
*
* @return {Number[]} The sorted array.
*
* @throws {TypeError} First parameter must be array of numbers.
* @throws {TypeError} Second parameter must be an integer.
* @throws {RangeError} Second parameter must be greater than one.
*/
function radixSort(nums, len, desc) {
if (!Array.isArray(nums))
throw new TypeError("First parameter must be array of numbers");
if (isNaN(len = parseInt(len)))
throw new TypeError("Second parameter must be an integer");
if (len < 1)
throw new RangeError("Second parameter must be greater than one");
desc = Boolean(desc);
// A shallow copy of nums array
var sorted = nums.slice();
var i;
var bins = [
[],
[],
[],
[],
[],
[],
[],
[],
[],
[]
];
for (var l = 0; l < len; l++) {
for (i = 0; i < sorted.length; i++)
bins[parseInt(sorted[i] / Math.pow(10, l)) % 10].push(sorted[i]);
sorted = [];
for (i = desc ? bins.length - 1 : 0; desc ? i >= 0 : i < bins.length; desc ? i-- : i++)
while (bins[i].length)
sorted.push(bins[i].shift());
}
return sorted;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment