From cecde9c8b3cb5a8f5bb94539efda2f737b23ed72 Mon Sep 17 00:00:00 2001 From: SomayaB Date: Tue, 25 Apr 2017 15:45:44 -0700 Subject: [PATCH 1/4] Complete setUnion algorithm w tests --- CONTRACT.md | 4 ++-- src/setUnion.js | 22 ++++++++++++++++++++++ test/setUnion_test.js | 20 ++++++++++++++++++++ 3 files changed, 44 insertions(+), 2 deletions(-) create mode 100644 src/setUnion.js create mode 100644 test/setUnion_test.js diff --git a/CONTRACT.md b/CONTRACT.md index 5b01d97..1eb880f 100644 --- a/CONTRACT.md +++ b/CONTRACT.md @@ -38,8 +38,8 @@ Complete **ONLY** the [Classic](https://github.com/GuildCrafts/core-algorithms/b - [x] Tests for `fibonacci()` exist with at least 2 unit tests using valid inputs, and at least 1 unit test using invalid inputs. - [x] `collatzConjecture()` algorithm is implemented according to the description in [algorithms.md][algorithms-list]. - [x] Tests for `collatzConjecture()` exist with at least 2 unit tests using valid inputs, and at least 1 unit test using invalid inputs. -- [ ] `setUnion()` algorithm is implemented according to the description in [algorithms.md][algorithms-list]. -- [ ] Tests for `setUnion()` exist with at least 2 unit tests using valid inputs, and at least 1 unit test using invalid inputs. +- [x] `setUnion()` algorithm is implemented according to the description in [algorithms.md][algorithms-list]. +- [x] Tests for `setUnion()` exist with at least 2 unit tests using valid inputs, and at least 1 unit test using invalid inputs. - [ ] `setIntersection()` algorithm is implemented according to the description in [algorithms.md][algorithms-list]. - [ ] Tests for `setIntersection()` exist with at least 2 unit tests using valid inputs, and at least 1 unit test using invalid inputs. - [ ] `setComplement()` algorithm is implemented according to the description in [algorithms.md][algorithms-list]. diff --git a/src/setUnion.js b/src/setUnion.js new file mode 100644 index 0000000..79f5b94 --- /dev/null +++ b/src/setUnion.js @@ -0,0 +1,22 @@ +// setUnion +// +// Return the union of two sets. +// +// const a = [1, 2, 3, 4] +// const b = [2, 4, 6, 8] +// setUnion(a, b) +// // => [1, 2, 3, 4, 6, 8] + +export default function setUnion(a, b){ + if (!Array.isArray(a) || !Array.isArray(b)){ + throw new Error('Invalid input format. Expected two arrays') + } + let concatArr = a.concat(b) + let sortedArr = concatArr.sort() + + function isUnique(value, index, array){ + return array.indexOf(value) === index + } + let filteredArr = sortedArr.filter(isUnique) + return filteredArr +} diff --git a/test/setUnion_test.js b/test/setUnion_test.js new file mode 100644 index 0000000..6813273 --- /dev/null +++ b/test/setUnion_test.js @@ -0,0 +1,20 @@ +import { expect } from 'chai' +import setUnion from '../src/setUnion' + +describe('setUnion', function(){ + + it('should be a function', function() { + expect(setUnion).to.be.a('function') + }) + + it('returns the union of two sets', function() { + expect(setUnion([1,2,3,4],[2,4,6,8])).to.eql([1, 2, 3, 4, 6, 8]) + expect(setUnion([2,3,4,4], [1,2,3,4]).to.eqal([1,2,3,4]) + }) + + it('throws an error when given invalid inputs', function() { + expect(function() {setUnion('string')}).to.throw( + 'Invalid input format. Expected two arrays' + ) + }) +}) From 80095c9daaccf2c8d0c378436246b7d8d4dbcefa Mon Sep 17 00:00:00 2001 From: SomayaB Date: Tue, 25 Apr 2017 16:10:59 -0700 Subject: [PATCH 2/4] Complete setIntersection w tests --- CONTRACT.md | 10 +++++----- src/setIntersection.js | 23 +++++++++++++++++++++++ test/setIntersection_test.js | 20 ++++++++++++++++++++ test/setUnion_test.js | 2 +- 4 files changed, 49 insertions(+), 6 deletions(-) create mode 100644 src/setIntersection.js create mode 100644 test/setIntersection_test.js diff --git a/CONTRACT.md b/CONTRACT.md index 1eb880f..28e9da0 100644 --- a/CONTRACT.md +++ b/CONTRACT.md @@ -40,19 +40,19 @@ Complete **ONLY** the [Classic](https://github.com/GuildCrafts/core-algorithms/b - [x] Tests for `collatzConjecture()` exist with at least 2 unit tests using valid inputs, and at least 1 unit test using invalid inputs. - [x] `setUnion()` algorithm is implemented according to the description in [algorithms.md][algorithms-list]. - [x] Tests for `setUnion()` exist with at least 2 unit tests using valid inputs, and at least 1 unit test using invalid inputs. -- [ ] `setIntersection()` algorithm is implemented according to the description in [algorithms.md][algorithms-list]. -- [ ] Tests for `setIntersection()` exist with at least 2 unit tests using valid inputs, and at least 1 unit test using invalid inputs. +- [x] `setIntersection()` algorithm is implemented according to the description in [algorithms.md][algorithms-list]. +- [x] Tests for `setIntersection()` exist with at least 2 unit tests using valid inputs, and at least 1 unit test using invalid inputs. - [ ] `setComplement()` algorithm is implemented according to the description in [algorithms.md][algorithms-list]. - [ ] Tests for `setComplement()` exist with at least 2 unit tests using valid inputs, and at least 1 unit test using invalid inputs. - [ ] `setSymmetricDifference()` algorithm is implemented according to the description in [algorithms.md][algorithms-list]. - [ ] Tests for `setSymmetricDifference()` exist with at least 2 unit tests using valid inputs, and at least 1 unit test using invalid inputs. - [ ] Repository includes a README file with basic installation and setup instructions. -- [ ] All dependencies are properly declared in `package.json`. +- [x] All dependencies are properly declared in `package.json`. - [ ] All major features are added via pull requests with a clear description and concise commit messages. - [ ] Code uses a linter and there are no linting errors. - [ ] Variables, functions, files, etc. have appropriate and meaningful names. -- [ ] Functions are small and serve a single purpose. -- [ ] The artifact produced is properly licensed, preferably with the [MIT license][mit-license]. +- [] Functions are small and serve a single purpose. +- [x] The artifact produced is properly licensed, preferably with the [MIT license][mit-license]. ### Stretch diff --git a/src/setIntersection.js b/src/setIntersection.js new file mode 100644 index 0000000..a5a1118 --- /dev/null +++ b/src/setIntersection.js @@ -0,0 +1,23 @@ +// setIntersection +// +// Return the intersection of two sets. +// +// const a = [1, 2, 3, 4] +// const b = [2, 4, 6, 8] +// setIntersection(a, b) +// // => [2, 4] + + +export default function setIntersection(a, b){ + if ( !Array.isArray(a) || !Array.isArray(b) ) { + throw new Error('Invalid input format. Expected two arrays') + } + let concatArr = a.concat(b) + let sortedArr = concatArr.sort() + + function isDuplicate(value, index, array){ + return array.indexOf(value) !== index + } + let filteredArr = sortedArr.filter(isDuplicate) + return filteredArr +} diff --git a/test/setIntersection_test.js b/test/setIntersection_test.js new file mode 100644 index 0000000..871a6a0 --- /dev/null +++ b/test/setIntersection_test.js @@ -0,0 +1,20 @@ +import { expect } from 'chai' +import setIntersection from '../src/setIntersection' + +describe('setIntersection', function(){ + + it('should be a function', function() { + expect(setIntersection).to.be.a('function') + }) + + it('returns the intersection of two sets', function () { + expect(setIntersection([1, 2, 3, 4],[2, 4, 6, 8])).to.eql([2,4]) + expect(setIntersection([82,90,1], [1, 2, 90])).to.eql([1,90]) + }) + + it('throws an error when given invalid inputs', function() { + expect(function() {setIntersection('string')}).to.throw( + 'Invalid input format. Expected two arrays' + ) + }) +}) diff --git a/test/setUnion_test.js b/test/setUnion_test.js index 6813273..154031e 100644 --- a/test/setUnion_test.js +++ b/test/setUnion_test.js @@ -9,7 +9,7 @@ describe('setUnion', function(){ it('returns the union of two sets', function() { expect(setUnion([1,2,3,4],[2,4,6,8])).to.eql([1, 2, 3, 4, 6, 8]) - expect(setUnion([2,3,4,4], [1,2,3,4]).to.eqal([1,2,3,4]) + expect(setUnion([2,3,4,4],[1,2,3,4])).to.eql([1,2,3,4]) }) it('throws an error when given invalid inputs', function() { From d9697a3d0065681bb38a91544651082b4242231b Mon Sep 17 00:00:00 2001 From: SomayaB Date: Tue, 25 Apr 2017 17:53:59 -0700 Subject: [PATCH 3/4] Complete setComplement algorithm w tests --- CONTRACT.md | 4 ++-- src/setComplement.js | 22 ++++++++++++++++++++++ test/setComplement_test.js | 27 +++++++++++++++++++++++++++ 3 files changed, 51 insertions(+), 2 deletions(-) create mode 100644 src/setComplement.js create mode 100644 test/setComplement_test.js diff --git a/CONTRACT.md b/CONTRACT.md index 28e9da0..c91e6fb 100644 --- a/CONTRACT.md +++ b/CONTRACT.md @@ -42,8 +42,8 @@ Complete **ONLY** the [Classic](https://github.com/GuildCrafts/core-algorithms/b - [x] Tests for `setUnion()` exist with at least 2 unit tests using valid inputs, and at least 1 unit test using invalid inputs. - [x] `setIntersection()` algorithm is implemented according to the description in [algorithms.md][algorithms-list]. - [x] Tests for `setIntersection()` exist with at least 2 unit tests using valid inputs, and at least 1 unit test using invalid inputs. -- [ ] `setComplement()` algorithm is implemented according to the description in [algorithms.md][algorithms-list]. -- [ ] Tests for `setComplement()` exist with at least 2 unit tests using valid inputs, and at least 1 unit test using invalid inputs. +- [x] `setComplement()` algorithm is implemented according to the description in [algorithms.md][algorithms-list]. +- [x] Tests for `setComplement()` exist with at least 2 unit tests using valid inputs, and at least 1 unit test using invalid inputs. - [ ] `setSymmetricDifference()` algorithm is implemented according to the description in [algorithms.md][algorithms-list]. - [ ] Tests for `setSymmetricDifference()` exist with at least 2 unit tests using valid inputs, and at least 1 unit test using invalid inputs. - [ ] Repository includes a README file with basic installation and setup instructions. diff --git a/src/setComplement.js b/src/setComplement.js new file mode 100644 index 0000000..208675c --- /dev/null +++ b/src/setComplement.js @@ -0,0 +1,22 @@ +// setComplement +// +// Return the complement of two sets. +// +// const a = [1, 2, 3, 4] +// const b = [2, 4, 6, 8] +// setComplement(a, b) +// // => [6, 8] + + +export default function setComplement(setA, setB) { + if ( !Array.isArray(setA) || !Array.isArray(setB) ) { + throw new Error('Invalid input format. Expected two arrays') + } + let complementArray = [] + for(let element of setB) { + if(!setA.includes(element)) { + complementArray.push(element) + } + } + return complementArray + } diff --git a/test/setComplement_test.js b/test/setComplement_test.js new file mode 100644 index 0000000..a8159ec --- /dev/null +++ b/test/setComplement_test.js @@ -0,0 +1,27 @@ +import { expect } from 'chai' +import setComplement from '../src/setComplement' + +describe('setComplement', function(){ + + it('should be a function', function() { + expect(setComplement).to.be.a('function') + }) + + it('should show the items from set B that are not present in set A', function() { + const setA = ['fluffy', 'cat', 'scratchy'] + const setB = ['scratchy', 'fluffy', 'dog'] + expect(setComplement(setA, setB)).to.eql(['dog']) + }) + + it('should show empty set if no items in B are not in A', function() { + const setA = ['fluffy', 'cat', 'scratchy'] + const setB = ['scratchy', 'fluffy'] + expect(setComplement(setA, setB)).to.eql([]) + }) + + it('throws an error when given invalid inputs', function() { + expect(function() {setComplement({}, false)}).to.throw( + 'Invalid input format. Expected two arrays' + ) + }) +}) From 41d8ce9c525ac0098d22f5cd7aa2a07e396ee464 Mon Sep 17 00:00:00 2001 From: SomayaB Date: Wed, 26 Apr 2017 14:54:34 -0700 Subject: [PATCH 4/4] Complete setSymmetricDifference algorithm w tests --- CONTRACT.md | 10 +++++----- src/factorial.js | 22 ++++++++++++++------- src/setSymmetricDifference.js | 30 +++++++++++++++++++++++++++++ test/setSymmetricDifference_test.js | 20 +++++++++++++++++++ 4 files changed, 70 insertions(+), 12 deletions(-) create mode 100644 src/setSymmetricDifference.js create mode 100644 test/setSymmetricDifference_test.js diff --git a/CONTRACT.md b/CONTRACT.md index c91e6fb..7b1f973 100644 --- a/CONTRACT.md +++ b/CONTRACT.md @@ -44,14 +44,14 @@ Complete **ONLY** the [Classic](https://github.com/GuildCrafts/core-algorithms/b - [x] Tests for `setIntersection()` exist with at least 2 unit tests using valid inputs, and at least 1 unit test using invalid inputs. - [x] `setComplement()` algorithm is implemented according to the description in [algorithms.md][algorithms-list]. - [x] Tests for `setComplement()` exist with at least 2 unit tests using valid inputs, and at least 1 unit test using invalid inputs. -- [ ] `setSymmetricDifference()` algorithm is implemented according to the description in [algorithms.md][algorithms-list]. -- [ ] Tests for `setSymmetricDifference()` exist with at least 2 unit tests using valid inputs, and at least 1 unit test using invalid inputs. +- [x] `setSymmetricDifference()` algorithm is implemented according to the description in [algorithms.md][algorithms-list]. +- [x] Tests for `setSymmetricDifference()` exist with at least 2 unit tests using valid inputs, and at least 1 unit test using invalid inputs. - [ ] Repository includes a README file with basic installation and setup instructions. - [x] All dependencies are properly declared in `package.json`. -- [ ] All major features are added via pull requests with a clear description and concise commit messages. +- [x] All major features are added via pull requests with a clear description and concise commit messages. - [ ] Code uses a linter and there are no linting errors. -- [ ] Variables, functions, files, etc. have appropriate and meaningful names. -- [] Functions are small and serve a single purpose. +- [x] Variables, functions, files, etc. have appropriate and meaningful names. +- [ ] Functions are small and serve a single purpose. - [x] The artifact produced is properly licensed, preferably with the [MIT license][mit-license]. ### Stretch diff --git a/src/factorial.js b/src/factorial.js index cb8372f..3b13a65 100644 --- a/src/factorial.js +++ b/src/factorial.js @@ -6,11 +6,19 @@ // // => 120 export default function factorial(num) { - var factorialElements = [num] - for (var i = num; i > 1; i--) { - factorialElements.push(i - 1) + var accumulator = num + for (var i = num; i > 1; i--) { + accumulator = accumulator * (i - 1) + } + return accumulator } - return factorialElements.reduce(function(a,b){ - return a*b - }, 1) -} + +//Other way: +// var factorialElements = [num] +// for (var i = num; i > 1; i--) { +// factorialElements.push(i - 1) +// } +// return factorialElements.reduce(function(a,b){ +// return a*b +// }, 1) +// } diff --git a/src/setSymmetricDifference.js b/src/setSymmetricDifference.js new file mode 100644 index 0000000..945cec0 --- /dev/null +++ b/src/setSymmetricDifference.js @@ -0,0 +1,30 @@ +// setSymmetricDifference +// +// Return the symmetric difference of two sets. +// +// const a = [1, 2, 3, 4] +// const b = [2, 4, 6, 8] +// setSymmetricDifference(a, b) +// // => [1, 3, 6, 8] + + +export default function setSymmetricDifference(setA, setB) { + if ( !Array.isArray(setA) || !Array.isArray(setB) ) { + throw new Error('Invalid input format. Expected two arrays') + } + let complementArray = [] + + for(let element of setB) { + if(!setA.includes(element)) { + complementArray.push(element) + } + } + for(let element of setA){ + if(!setB.includes(element)) { + complementArray.push(element) + } + } + return complementArray.sort() + } + +// diff --git a/test/setSymmetricDifference_test.js b/test/setSymmetricDifference_test.js new file mode 100644 index 0000000..11f9554 --- /dev/null +++ b/test/setSymmetricDifference_test.js @@ -0,0 +1,20 @@ +import { expect } from 'chai' +import setSymmetricDifference from '../src/setSymmetricDifference' + + + +describe('setSymmetricDifference', function() { + + it('should be a function', function() { + expect(setSymmetricDifference).to.be.a('function') + }) + + it('returns the symmetric difference of two sets', function() { + expect(setSymmetricDifference([1, 2, 3, 4], [2, 4, 6, 8])).to.eql([1, 3, 6, 8]) + }) + + it('throws an error when given invalid inputs', function() { + expect(function() {setSymmetricDifference('string', [1,2])}). + to.throw('Invalid input format. Expected two arrays') + }) +})