Merge branch 'master' of github.com:charliejmoore/Javascript into cycle-sort-tests

This commit is contained in:
Charlie Moore
2021-10-06 07:42:16 -04:00
11 changed files with 12691 additions and 33 deletions

View File

@ -0,0 +1,44 @@
/*
Problem: Given two numbers, n and k, make all unique combinations of k numbers from 1 to n and in sorted order
What is combinations?
- Combinations is selecting items froms a collections without considering order of selection
Example:
- We have an apple, a banana, and a jackfruit
- We have three objects, and need to choose two items, then combinations will be
1. Apple & Banana
2. Apple & Jackfruit
3. Banana & Jackfruit
To read more about combinations, you can visit the following link:
- https://betterexplained.com/articles/easy-permutations-and-combinations/
Solution:
- We will be using backtracking to solve this questions
- Take one element, and make all them combinations for k-1 elements
- Once we get all combinations of that element, pop it and do same for next element
*/
class Combinations {
constructor (n, k) {
this.n = n
this.k = k
this.combinationArray = [] // will be used for storing current combination
}
findCombinations (high = this.n, total = this.k, low = 1) {
if (total === 0) {
console.log(this.combinationArray)
return
}
for (let i = low; i <= high; i++) {
this.combinationArray.push(i)
this.findCombinations(high, total - 1, i + 1)
this.combinationArray.pop()
}
}
}
export { Combinations }

View File

@ -0,0 +1,13 @@
import { Combinations } from '../AllCombinationsOfSizeK'
describe('AllCombinationsOfSizeK', () => {
it('should return 3x2 matrix solution for n = 3 and k = 2', () => {
const test1 = new Combinations(3, 2)
expect(test1.findCombinations).toEqual([[1, 2], [1, 3], [2, 3]])
})
it('should return 6x2 matrix solution for n = 3 and k = 2', () => {
const test2 = new Combinations(4, 2)
expect(test2.findCombinations).toEqual([[1, 2], [1, 3], [1, 4], [2, 3], [2, 4], [3, 4]])
})
})

View File

@ -1,5 +1,6 @@
## Backtracking
* [AllCombinationsOfSizeK](https://github.com/TheAlgorithms/Javascript/blob/master/Backtracking/AllCombinationsOfSizeK.js)
* [GeneratePermutations](https://github.com/TheAlgorithms/Javascript/blob/master/Backtracking/GeneratePermutations.js)
* [KnightTour](https://github.com/TheAlgorithms/Javascript/blob/master/Backtracking/KnightTour.js)
* [NQueen](https://github.com/TheAlgorithms/Javascript/blob/master/Backtracking/NQueen.js)
@ -156,6 +157,7 @@
* [IsDivisible](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/IsDivisible.js)
* [IsEven](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/IsEven.js)
* [isOdd](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/isOdd.js)
* [LeapYear](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/LeapYear.js)
* [Mandelbrot](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/Mandelbrot.js)
* [MatrixExponentiationRecursive](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/MatrixExponentiationRecursive.js)
* [MatrixMultiplication](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/MatrixMultiplication.js)

22
Maths/LeapYear.js Normal file
View File

@ -0,0 +1,22 @@
/**
* isLeapYear :: Number -> Boolean
*
* Check if a year is a leap year or not. A leap year is a year which has 366 days.
* For the extra +1 day the February month contains 29 days instead of 28 days.
*
* The logic behind the leap year is-
* 1. If the year is divisible by 400 then it is a leap year.
* 2. If it is not divisible by 400 but divisible by 100 then it is not a leap year.
* 3. If the year is not divisible by 400 but not divisible by 100 and divisible by 4 then a leap year.
* 4. Other cases except the describing ones are not a leap year.
*
* @param {number} year
* @returns {boolean} true if this is a leap year, false otherwise.
*/
export const isLeapYear = (year) => {
if (year % 400 === 0) return true
if (year % 100 === 0) return false
if (year % 4 === 0) return true
return false
}

View File

@ -0,0 +1,22 @@
import { isLeapYear } from '../LeapYear'
describe('Leap Year', () => {
it('Should return true on the year 2000', () => {
expect(isLeapYear(2000)).toBe(true)
})
it('Should return false on the year 2001', () => {
expect(isLeapYear(2001)).toBe(false)
})
it('Should return false on the year 2002', () => {
expect(isLeapYear(2002)).toBe(false)
})
it('Should return false on the year 2003', () => {
expect(isLeapYear(2003)).toBe(false)
})
it('Should return false on the year 2004', () => {
expect(isLeapYear(2004)).toBe(true)
})
it('Should return false on the year 1900', () => {
expect(isLeapYear(1900)).toBe(false)
})
})

View File

@ -1,5 +1,5 @@
/*
Wikipedia says: Bucket sort, or bin sort, is a sorting algorithm that works by distributing the
[Wikipedia says](https://en.wikipedia.org/wiki/Bucket_sort#:~:text=Bucket%20sort%2C%20or%20bin%20sort,applying%20the%20bucket%20sorting%20algorithm.&text=Sort%20each%20non%2Dempty%20bucket.): Bucket sort, or bin sort, is a sorting algorithm that works by distributing the
elements of an array into a number of buckets. Each bucket is then sorted individually, either using
a different sorting algorithm, or by recursively applying the bucket sorting algorithm. It is a
distribution sort, and is a cousin of radix sort in the most to least significant digit flavour.
@ -11,6 +11,14 @@ Time Complexity of Solution:
Best Case O(n); Average Case O(n); Worst Case O(n)
*/
/**
* bucketSort returns an array of numbers sorted in increasing order.
*
* @param {number[]} list The array of numbers to be sorted.
* @param {number} size The size of the buckets used. If not provided, size will be 5.
* @return {number[]} An array of numbers sorted in increasing order.
*/
function bucketSort (list, size) {
if (undefined === size) {
size = 5
@ -45,7 +53,7 @@ function bucketSort (list, size) {
const sorted = []
// now sort every bucket and merge it to the sorted list
for (let iBucket = 0; iBucket < buckets.length; iBucket++) {
const arr = buckets[iBucket].sort()
const arr = buckets[iBucket].sort((a, b) => a - b)
for (let iSorted = 0; iSorted < arr.length; iSorted++) {
sorted.push(arr[iSorted])
}
@ -53,12 +61,4 @@ function bucketSort (list, size) {
return sorted
}
// Testing
const arrOriginal = [5, 6, 7, 8, 1, 2, 12, 14]
// > bucketSort(arrOriginal)
// [1, 2, 5, 6, 7, 8, 12, 14]
// Array before Sort
console.log(arrOriginal)
const arrSorted = bucketSort(arrOriginal)
// Array after sort
console.log(arrSorted)
export { bucketSort }

View File

@ -16,6 +16,12 @@
* Wikipedia: https://en.wikipedia.org/wiki/Comb_sort
*/
/**
* combSort returns an array of numbers sorted in increasing order.
*
* @param {number[]} list The array of numbers to sort.
* @return {number[]} The array of numbers sorted in increasing order.
*/
function combSort (list) {
if (list.length === 0) {
return list
@ -43,14 +49,4 @@ function combSort (list) {
return list
}
/**
* Implementation of Comb Sort
*/
const array = [5, 6, 7, 8, 1, 2, 12, 14]
// Before Sort
console.log('\n- Before Sort | Implementation of Comb Sort -')
console.log(array)
// After Sort
console.log('- After Sort | Implementation of Comb Sort -')
console.log(combSort(array))
console.log('\n')
export { combSort }

View File

@ -0,0 +1,69 @@
import { bucketSort } from '../BucketSort'
describe('Tests for bucketSort function', () => {
it('should correctly sort an input list that is sorted backwards', () => {
const array = [5, 4, 3, 2, 1]
expect(bucketSort(array)).toEqual([1, 2, 3, 4, 5])
})
it('should correctly sort an input list that is unsorted', () => {
const array = [15, 24, 3, 2224, 1]
expect(bucketSort(array)).toEqual([1, 3, 15, 24, 2224])
})
describe('Variations of input array lengths', () => {
it('should return an empty list with the input list is an empty list', () => {
expect(bucketSort([])).toEqual([])
})
it('should correctly sort an input list of length 1', () => {
expect(bucketSort([100])).toEqual([100])
})
it('should correctly sort an input list of an odd length', () => {
expect(bucketSort([101, -10, 321])).toEqual([-10, 101, 321])
})
it('should correctly sort an input list of an even length', () => {
expect(bucketSort([40, 42, 56, 45, 12, 3])).toEqual([3, 12, 40, 42, 45, 56])
})
})
describe('Variations of input array elements', () => {
it('should correctly sort an input list that contains only positive numbers', () => {
expect(bucketSort([50, 33, 11, 2])).toEqual([2, 11, 33, 50])
})
it('should correctly sort an input list that contains only negative numbers', () => {
expect(bucketSort([-1, -21, -2, -35])).toEqual([-35, -21, -2, -1])
})
it('should correctly sort an input list that contains only a mix of positive and negative numbers', () => {
expect(bucketSort([-40, 42, 56, -45, 12, -3])).toEqual([-45, -40, -3, 12, 42, 56])
})
it('should correctly sort an input list that contains only whole numbers', () => {
expect(bucketSort([11, 3, 12, 4, -15])).toEqual([-15, 3, 4, 11, 12])
})
it('should correctly sort an input list that contains only decimal numbers', () => {
expect(bucketSort([1.0, 1.42, 2.56, 33.45, 13.12, 2.3])).toEqual([1.0, 1.42, 2.3, 2.56, 13.12, 33.45])
})
it('should correctly sort an input list that contains only a mix of whole and decimal', () => {
expect(bucketSort([32.40, 12.42, 56, 45, 12, 3])).toEqual([3, 12, 12.42, 32.40, 45, 56])
})
it('should correctly sort an input list that contains only fractional numbers', () => {
expect(bucketSort([0.98, 0.4259, 0.56, -0.456, -0.12, 0.322])).toEqual([-0.456, -0.12, 0.322, 0.4259, 0.56, 0.98])
})
it('should correctly sort an input list that contains only a mix of whole, decimal, and fractional', () => {
expect(bucketSort([-40, -0.222, 5.6, -4.5, 12, 0.333])).toEqual([-40, -4.5, -0.222, 0.333, 5.6, 12])
})
it('should correctly sort an input list that contains duplicates', () => {
expect(bucketSort([4, 3, 4, 2, 1, 2])).toEqual([1, 2, 2, 3, 4, 4])
})
})
})

View File

@ -0,0 +1,69 @@
import { combSort } from '../CombSort'
describe('combSort function', () => {
it('should correctly sort an input list that is sorted backwards', () => {
const array = [5, 4, 3, 2, 1]
expect(combSort(array)).toEqual([1, 2, 3, 4, 5])
})
it('should correctly sort an input list that is unsorted', () => {
const array = [15, 24, 3, 2224, 1]
expect(combSort(array)).toEqual([1, 3, 15, 24, 2224])
})
describe('Variations of input array lengths', () => {
it('should return an empty list with the input list is an empty list', () => {
expect(combSort([])).toEqual([])
})
it('should correctly sort an input list of length 1', () => {
expect(combSort([100])).toEqual([100])
})
it('should correctly sort an input list of an odd length', () => {
expect(combSort([101, -10, 321])).toEqual([-10, 101, 321])
})
it('should correctly sort an input list of an even length', () => {
expect(combSort([40, 42, 56, 45, 12, 3])).toEqual([3, 12, 40, 42, 45, 56])
})
})
describe('Variations of input array elements', () => {
it('should correctly sort an input list that contains only positive numbers', () => {
expect(combSort([50, 33, 11, 2])).toEqual([2, 11, 33, 50])
})
it('should correctly sort an input list that contains only negative numbers', () => {
expect(combSort([-1, -21, -2, -35])).toEqual([-35, -21, -2, -1])
})
it('should correctly sort an input list that contains only a mix of positive and negative numbers', () => {
expect(combSort([-40, 42, 56, -45, 12, -3])).toEqual([-45, -40, -3, 12, 42, 56])
})
it('should correctly sort an input list that contains only whole numbers', () => {
expect(combSort([11, 3, 12, 4, -15])).toEqual([-15, 3, 4, 11, 12])
})
it('should correctly sort an input list that contains only decimal numbers', () => {
expect(combSort([1.0, 1.42, 2.56, 33.45, 13.12, 2.3])).toEqual([1.0, 1.42, 2.3, 2.56, 13.12, 33.45])
})
it('should correctly sort an input list that contains only a mix of whole and decimal', () => {
expect(combSort([32.40, 12.42, 56, 45, 12, 3])).toEqual([3, 12, 12.42, 32.40, 45, 56])
})
it('should correctly sort an input list that contains only fractional numbers', () => {
expect(combSort([0.98, 0.4259, 0.56, -0.456, -0.12, 0.322])).toEqual([-0.456, -0.12, 0.322, 0.4259, 0.56, 0.98])
})
it('should correctly sort an input list that contains only a mix of whole, decimal, and fractional', () => {
expect(combSort([-40, -0.222, 5.6, -4.5, 12, 0.333])).toEqual([-40, -4.5, -0.222, 0.333, 5.6, 12])
})
it('should correctly sort an input list that contains duplicates', () => {
expect(combSort([4, 3, 4, 2, 1, 2])).toEqual([1, 2, 2, 3, 4, 4])
})
})
})

12437
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -14,10 +14,10 @@
"@babel/core": "^7.11.6",
"@babel/plugin-transform-runtime": "^7.11.5",
"@babel/preset-env": "^7.11.5",
"atob": "2.1.2",
"jsdom": "^16.3.0",
"node": "^14.13.1",
"node-fetch": "2.6.1",
"atob": "2.1.2"
"node-fetch": "2.6.1"
},
"standard": {
"env": [
@ -31,4 +31,4 @@
"jest": "^26.4.2",
"standard": "^14.3.4"
}
}
}