From 42fe56a5bf73994059da435d0dca8e3e1e3f4089 Mon Sep 17 00:00:00 2001 From: Yoo Jin Lim Date: Mon, 5 Jun 2017 12:37:18 -0700 Subject: [PATCH 01/31] solve issue #58 find guestlist for Max fun --- solutions/58.js | 52 +++++++++++++++++++++++++++++++++++++++++++++++++ test/58.js | 39 +++++++++++++++++++++++++++++++++++++ 2 files changed, 91 insertions(+) create mode 100644 solutions/58.js create mode 100644 test/58.js diff --git a/solutions/58.js b/solutions/58.js new file mode 100644 index 0000000..d9ca824 --- /dev/null +++ b/solutions/58.js @@ -0,0 +1,52 @@ +// Max Fun Problem +// Return an array of employees that creates the maximum fun +// Input: Employee object +// Output: [Employee objects] + +// Solution by Yoo Jin + +/** + * Given an Employee, return an array of employees that creates the maximum fun. + * There is one condition: An Employee cannot be on the guestlist if his direct manager is going to the party. + * @param {Employee} emp - an Employee with fun value and a list of his staff + * @returns {Employee []]} list - Array of employees that creates the maximum fun + */ + +const solution = (emp) => { + if(!emp) { + return []; + } + + if(emp.staff.length === 0) { + return [emp]; + } + + let funValWithoutEmp = 0; + let funStaffListWithoutEmp = []; + emp.staff.forEach((staff) => { + solution(staff).forEach((emp) => { + funValWithoutEmp += emp.funVal; + funStaffListWithoutEmp.push(emp); + }); + }); + + let funValWithEmp = emp.funVal; + let funStaffListWithEmp = [emp]; + emp.staff.forEach((staff) => { + staff.staff.forEach((staffstaff) => { + solution(staffstaff).forEach((emp) => { + funValWithEmp += emp.funVal; + funStaffListWithEmp.push(emp); + }); + }); + }); + + if (funValWithoutEmp > funValWithEmp) { + return funStaffListWithoutEmp; + } + return funStaffListWithEmp; +}; + +module.exports = { + solution +}; diff --git a/test/58.js b/test/58.js new file mode 100644 index 0000000..218fb16 --- /dev/null +++ b/test/58.js @@ -0,0 +1,39 @@ +const expect = require('chai').expect; +const solution = require('../solutions/58.js').solution; +// solution = require('../yourSolution').solution; + +describe('return smallest number of coins required to return the change', () => { + class Employee { + constructor(funVal, staffArray=[]) { + this.funVal = funVal; + this.staff = []; + this.addStaff(staffArray); + } + + addStaff(staffArray) { + staffArray.forEach((staff) => { + this.staff.push(staff); + }); + } + } + + let emp1 = new Employee(1); + let emp2 = new Employee(5); + let emp3 = new Employee(11); + let emp4 = new Employee(10,[emp1]); + let emp5 = new Employee(2,[emp2,emp3]); + let emp6 = new Employee(4,[emp4,emp5]); + + it('simplest case of one Employee object', () => { + expect(solution(emp1)).to.deep.equal([emp1]); + }); + it('simple case of one Employee with Staff, where Employee has greater fun value', () => { + expect(solution(emp4)).to.deep.equal([emp4]); + }); + it('simple case of one Employee with Staff, where staff have greater fun value', () => { + expect(solution(emp5)).to.deep.equal([emp2,emp3]); + }); + it('hard case of one Employee with many staff', () => { + expect(solution(emp6)).to.deep.equal([emp4,emp2,emp3]); + }); +}); From 061c1c5f8aa1d2bd9d9a315d448295318bbc439d Mon Sep 17 00:00:00 2001 From: seemcat Date: Mon, 5 Jun 2017 08:09:15 -0700 Subject: [PATCH 02/31] Test & Solution for #34 --- solutions/34a.js | 22 ++++++++++++++++++++++ test/34a.js | 21 +++++++++++++++++++++ 2 files changed, 43 insertions(+) create mode 100644 solutions/34a.js create mode 100644 test/34a.js diff --git a/solutions/34a.js b/solutions/34a.js new file mode 100644 index 0000000..aaaf9d3 --- /dev/null +++ b/solutions/34a.js @@ -0,0 +1,22 @@ +// Maricris Bonzo +// Takes in (array, number) and outputs the number of +// times the number shows up in the array. + +const solution = (array, num) => { + let i = 0; + let numOfTimes = 0; + + while (i < array.length){ + if (array[i] === num){ + numOfTimes = numOfTimes + 1; + i++; + } else { + i++; + } + } + return numOfTimes; +}; + +module.exports = { + solution +}; diff --git a/test/34a.js b/test/34a.js new file mode 100644 index 0000000..cf46798 --- /dev/null +++ b/test/34a.js @@ -0,0 +1,21 @@ +const expect = require('chai').expect; +let solution = require('../solutions/34a').solution; + +describe('number of repeating elements', () => { + it('should return 1 for ([1,2,2,3],3)', () => { + const result = solution([1,2,2,3], 3); + expect(result).to.equal(1); + }); + it('should return 2 for ([1,2,2,3],2)', () => { + const result = solution([1,2,2,3], 2); + expect(result).to.equal(2); + }); + it('should return 1 for ([1,2,2,3],1)', () => { + const result = solution([1,2,2,3], 1); + expect(result).to.equal(1); + }); + it('should return 0 for ([1,2,2,3],4)', () => { + const result = solution([1,2,2,3], 4); + expect(result).to.equal(0); + }); +}); From 9875e4a21384e31c9bd2efbfa61d9bf2e3e6b090 Mon Sep 17 00:00:00 2001 From: seemcat Date: Mon, 5 Jun 2017 11:45:26 -0700 Subject: [PATCH 03/31] Necessary Changes --- solutions/34.js | 25 ++++++++++++++++++++++--- test/34.js | 1 + test/34a.js | 21 --------------------- 3 files changed, 23 insertions(+), 24 deletions(-) delete mode 100644 test/34a.js diff --git a/solutions/34.js b/solutions/34.js index 1e8382c..19619b7 100644 --- a/solutions/34.js +++ b/solutions/34.js @@ -9,7 +9,26 @@ const solution = (arr, k) => { } } return total; - }; - module.exports = { - solution +}; + +// Maricris Bonzo + +const solution1 = (array, num) => { + let i = 0; + let numOfTimes = 0; + + while (i < array.length){ + if (array[i] === num){ + numOfTimes = numOfTimes + 1; + i++; + } else { + i++; + } + } + return numOfTimes; +}; + +module.exports = { + solution, + solution1 }; diff --git a/test/34.js b/test/34.js index 8940b77..962792b 100644 --- a/test/34.js +++ b/test/34.js @@ -1,5 +1,6 @@ const expect = require('chai').expect; const solution = require('../solutions/34.js').solution; +const solution1 = require('../solutions/34.js').solution; // solution = require('../yourSolution').solution; describe('return number of repeating elements', () => { diff --git a/test/34a.js b/test/34a.js deleted file mode 100644 index cf46798..0000000 --- a/test/34a.js +++ /dev/null @@ -1,21 +0,0 @@ -const expect = require('chai').expect; -let solution = require('../solutions/34a').solution; - -describe('number of repeating elements', () => { - it('should return 1 for ([1,2,2,3],3)', () => { - const result = solution([1,2,2,3], 3); - expect(result).to.equal(1); - }); - it('should return 2 for ([1,2,2,3],2)', () => { - const result = solution([1,2,2,3], 2); - expect(result).to.equal(2); - }); - it('should return 1 for ([1,2,2,3],1)', () => { - const result = solution([1,2,2,3], 1); - expect(result).to.equal(1); - }); - it('should return 0 for ([1,2,2,3],4)', () => { - const result = solution([1,2,2,3], 4); - expect(result).to.equal(0); - }); -}); From 89f778d8d1045af2007fdaaeb8198e16e0a55258 Mon Sep 17 00:00:00 2001 From: seemcat Date: Tue, 6 Jun 2017 12:10:47 -0700 Subject: [PATCH 04/31] Modified test file for #34 --- test/34.js | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/test/34.js b/test/34.js index 962792b..f598afd 100644 --- a/test/34.js +++ b/test/34.js @@ -16,5 +16,18 @@ describe('return number of repeating elements', () => { it('hardest case', () => { expect(solution([5,5,3,2,3,2,5,5],5)).to.equal(4); }); + it('simple case', () => { + expect(solution1([2],2)).to.equal(1); + }); + it('hard case', () => { + expect(solution1([3,2],1)).to.equal(0); + }); + it('harder case', () => { + expect(solution1([3,2,3,2],2)).to.equal(2); + }) + it('hardest case', () => { + expect(solution1([5,5,3,2,3,2,5,5],5)).to.equal(4); + }); + }); From 8f7b4296459b9d75861e5a63e61524744e4293c6 Mon Sep 17 00:00:00 2001 From: seemcat Date: Tue, 6 Jun 2017 12:22:23 -0700 Subject: [PATCH 05/31] Deleted 34a.js solutions file --- solutions/34a.js | 22 ---------------------- 1 file changed, 22 deletions(-) delete mode 100644 solutions/34a.js diff --git a/solutions/34a.js b/solutions/34a.js deleted file mode 100644 index aaaf9d3..0000000 --- a/solutions/34a.js +++ /dev/null @@ -1,22 +0,0 @@ -// Maricris Bonzo -// Takes in (array, number) and outputs the number of -// times the number shows up in the array. - -const solution = (array, num) => { - let i = 0; - let numOfTimes = 0; - - while (i < array.length){ - if (array[i] === num){ - numOfTimes = numOfTimes + 1; - i++; - } else { - i++; - } - } - return numOfTimes; -}; - -module.exports = { - solution -}; From 2ee704f58be049273ffaa42037e463804cf176eb Mon Sep 17 00:00:00 2001 From: seemcat Date: Wed, 7 Jun 2017 08:07:10 -0700 Subject: [PATCH 06/31] Modified test file for #34 again. --- test/34.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/34.js b/test/34.js index f598afd..83c8805 100644 --- a/test/34.js +++ b/test/34.js @@ -1,6 +1,6 @@ const expect = require('chai').expect; const solution = require('../solutions/34.js').solution; -const solution1 = require('../solutions/34.js').solution; +const solution1 = require('../solutions/34.js').solution1; // solution = require('../yourSolution').solution; describe('return number of repeating elements', () => { From e9f6b25179407e0f0d20b4e0d8f55ca6520bccb1 Mon Sep 17 00:00:00 2001 From: Rahul Kalra Date: Wed, 7 Jun 2017 14:39:32 -0700 Subject: [PATCH 07/31] solution and test for #32 --- solutions/32.js | 23 +++++++++++++++++++++++ test/32.js | 8 ++++++++ 2 files changed, 31 insertions(+) create mode 100644 solutions/32.js create mode 100644 test/32.js diff --git a/solutions/32.js b/solutions/32.js new file mode 100644 index 0000000..e363636 --- /dev/null +++ b/solutions/32.js @@ -0,0 +1,23 @@ +//Rahul Kalra +//Finding Singleton + +const solution = (array) =>{ + let data = {}; + let newArray = []; + for(let i = 0; i < array.length; i++){ + if(!(data[array[i]])){ + data[array[i]] = 1; + } + else{ + data[array[i]]++; + } + } + for(let key in data){ + if(data[key] === 1){ + newArray.push(Number(key)); + } + } + return newArray; +}; + +module.exports = {solution}; diff --git a/test/32.js b/test/32.js new file mode 100644 index 0000000..9fad46d --- /dev/null +++ b/test/32.js @@ -0,0 +1,8 @@ +const expect = require('chai').expect; +let solution = require('../solutions/32').solution; + +describe('Finding singleton in an array', () =>{ + it.only('should return singleton in an array', () =>{ + expect(solution([1,2,3,3,1,4])).to.eql([2, 4]); + }); +}); From ee0b8c2a01d91bf166542e423cecb3b929c2f665 Mon Sep 17 00:00:00 2001 From: Rahul Kalra Date: Thu, 8 Jun 2017 05:21:48 -0700 Subject: [PATCH 08/31] solution and test for problem #21 --- solutions/21.js | 35 +++++++++++++++++++++++++++++++++++ test/21.js | 14 ++++++++++++++ 2 files changed, 49 insertions(+) create mode 100644 solutions/21.js create mode 100644 test/21.js diff --git a/solutions/21.js b/solutions/21.js new file mode 100644 index 0000000..1a4a6a4 --- /dev/null +++ b/solutions/21.js @@ -0,0 +1,35 @@ +// Rahul Kalra +// Return array of largest 3 values in descending order from a given array + +const max = (array) =>{ + let max = array[0]; + for(let i = 0; i < array.length; i++){ + if(max <= array[i+1]){ + max = array[i+1]; + } + } + return max; +}; + +const removeNum = (array, max) =>{ + let newArray = []; + for(let i = 0; i < array.length; i++){ + if(!(max === array[i])){ + newArray.push(array[i]); + } + } + return newArray; +}; + +const solution = (array) =>{ + let a = 0; + let newArray = []; + for(let i = 0; i < 3; i++){ + a = max(array);; + array = removeNum(array, a); + newArray.push(a); + } + return [newArray[0], newArray[1], newArray[2]]; +}; + +module.exports = {solution}; diff --git a/test/21.js b/test/21.js new file mode 100644 index 0000000..408d751 --- /dev/null +++ b/test/21.js @@ -0,0 +1,14 @@ +let expect = require('chai').expect; +let solution = require('../solutions/21').solution; + +describe('Return 3 top largest values of an array in a descending order', () =>{ + it('should return 3 top largest values of an array', () =>{ + expect(solution([8,1,4,2,12,6,7,19,2,9])).to.eql([19, 12, 9]); + }); +}); + +describe('Return 3 top largest values of an array in a descending order', () =>{ + it('should return 3 top largest values of an array', () =>{ + expect(solution([-8,-3,-7,-1,-20,-2])).to.eql([-1,-2,-3]); + }); +}); From 6e118484eb7a65296642a9e401f58ee8242af181 Mon Sep 17 00:00:00 2001 From: seemcat Date: Mon, 5 Jun 2017 10:45:35 -0700 Subject: [PATCH 09/31] Test & Solution for #61 --- solutions/61.js | 54 ++++++++++++++++++++++++++++++++++++++++++++++--- test/61.js | 1 + 2 files changed, 52 insertions(+), 3 deletions(-) diff --git a/solutions/61.js b/solutions/61.js index a65a660..2fa0275 100644 --- a/solutions/61.js +++ b/solutions/61.js @@ -3,8 +3,8 @@ // Input: 16,24 // Output: 8 -// Solutions by Colin Xie @ColinX13 - +// Solution #0 by Colin Xie @ColinX13 +// Solution #1 by Maricris Bonzo @seemcat /** * Factor is determined when both remainders of num1 and num2 are 0 when factor is divided from them. * @param num1 - the first number input @@ -18,6 +18,54 @@ const solution = (num1, num2) => { } } }; + +const solution1 = (num1, num2) => { + if (num1 > num2){ + let long = num1; + let short = num2; + } else { + let long = num2; + let short = num1; + }; + + let num1Array = []; + let num2Array = []; + let i = 1; + let j = 1; + + while (i < long) { + if (long%i === 0){ + num1Array.push(i); + i++; + } else { + i++; + } + }; + + while (j < short) { + if (short%i === 0){ + num2Array.push(j); + j++; + } else { + j++; + } + }; + + let k, l = 0; + let commonNumbers = []; + for (k; k < num1Array.length; k++){ + for (l; l < num2Array.length; l++){ + if (num1Array[k] === num2Array[l]){ + commonNumbers.push(num1Array[k]); + } + } + }; + + let GCD = Math.max(commonNumbers); + return GCD; +}; + module.exports = { - solution + solution, + solution1 }; diff --git a/test/61.js b/test/61.js index 49d6f3b..b8fa8e6 100644 --- a/test/61.js +++ b/test/61.js @@ -1,5 +1,6 @@ const expect = require('chai').expect; let solution = require('../solutions/61').solution; +let solution1 = require('../solutions/61').solution; // solution = require('./yourSolution').solution; describe('greatest common denominator', () => { From c3142331292b1b0f74d308c26e2bfe10ff413d0a Mon Sep 17 00:00:00 2001 From: seemcat Date: Tue, 6 Jun 2017 12:02:29 -0700 Subject: [PATCH 10/31] Modified test file for #61 --- test/61.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/61.js b/test/61.js index b8fa8e6..f653159 100644 --- a/test/61.js +++ b/test/61.js @@ -14,12 +14,12 @@ describe('greatest common denominator', () => { expect(solution(50,60)).eql(10); }); it('the gcd for 40 and 20 is 20', () => { - expect(solution(40,20)).eql(20); + expect(solution1(40,20)).eql(20); }); it('the gcd for 20 and 40 is 20', () => { - expect(solution(20,40)).eql(20); + expect(solution1(20,40)).eql(20); }); it('the gcd for 1 and 4 is 1', () => { - expect(solution(1,4)).eql(1); + expect(solution1(1,4)).eql(1); }); }); From 5c5967894c5c2c4dd24b42c04a6d3b3680bb4813 Mon Sep 17 00:00:00 2001 From: seemcat Date: Wed, 7 Jun 2017 08:20:13 -0700 Subject: [PATCH 11/31] Modified test file for #61 again --- test/61.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/61.js b/test/61.js index f653159..228a628 100644 --- a/test/61.js +++ b/test/61.js @@ -1,6 +1,6 @@ const expect = require('chai').expect; let solution = require('../solutions/61').solution; -let solution1 = require('../solutions/61').solution; +let solution1 = require('../solutions/61').solution1; // solution = require('./yourSolution').solution; describe('greatest common denominator', () => { From 5684ed99286ff9ade574c7e37b2edc31cc49178a Mon Sep 17 00:00:00 2001 From: seemcat Date: Thu, 8 Jun 2017 11:07:47 -0700 Subject: [PATCH 12/31] Corrected solution for #61. --- solutions/61.js | 42 ++++++++++++++++++------------------------ 1 file changed, 18 insertions(+), 24 deletions(-) diff --git a/solutions/61.js b/solutions/61.js index 2fa0275..166304f 100644 --- a/solutions/61.js +++ b/solutions/61.js @@ -19,49 +19,43 @@ const solution = (num1, num2) => { } }; -const solution1 = (num1, num2) => { - if (num1 > num2){ - let long = num1; - let short = num2; - } else { - let long = num2; - let short = num1; - }; - +const solution1 = (num1, num2) => { let num1Array = []; let num2Array = []; let i = 1; let j = 1; - while (i < long) { - if (long%i === 0){ + while (i <= num1) { + if (num1%i === 0){ num1Array.push(i); i++; } else { i++; } }; - - while (j < short) { - if (short%i === 0){ + + while (j <= num2) { + if (num2%j === 0){ num2Array.push(j); j++; } else { j++; } }; - - let k, l = 0; + + let k = 0; let commonNumbers = []; - for (k; k < num1Array.length; k++){ - for (l; l < num2Array.length; l++){ - if (num1Array[k] === num2Array[l]){ - commonNumbers.push(num1Array[k]); - } - } + + while (k < num2Array.length){ + if (num1Array.indexOf(num2Array[k]) === -1){ + k++; + } else { + commonNumbers.push(num2Array[k]); + k++; + }; }; - - let GCD = Math.max(commonNumbers); + + let GCD = Math.max.apply(Math, commonNumbers); return GCD; }; From 407ea0996470c939cc9f5256fbc715d09e539dd4 Mon Sep 17 00:00:00 2001 From: Yoo Jin Lim Date: Wed, 14 Jun 2017 10:36:48 -0700 Subject: [PATCH 13/31] issue #58 move Employee class to solution file and use reduce functions --- solutions/58.js | 44 +++++++++++++++++++++++++++----------------- test/58.js | 17 ++--------------- 2 files changed, 29 insertions(+), 32 deletions(-) diff --git a/solutions/58.js b/solutions/58.js index d9ca824..361dd86 100644 --- a/solutions/58.js +++ b/solutions/58.js @@ -11,6 +11,20 @@ * @param {Employee} emp - an Employee with fun value and a list of his staff * @returns {Employee []]} list - Array of employees that creates the maximum fun */ +class Employee { + constructor(funVal, staffArray=[]) { + this.funVal = funVal; + this.staff = []; + this.addStaff(staffArray); + } + + addStaff(staffArray) { + staffArray.forEach((staff) => { + this.staff.push(staff); + }); + } +} + const solution = (emp) => { if(!emp) { @@ -21,32 +35,28 @@ const solution = (emp) => { return [emp]; } - let funValWithoutEmp = 0; let funStaffListWithoutEmp = []; - emp.staff.forEach((staff) => { - solution(staff).forEach((emp) => { - funValWithoutEmp += emp.funVal; - funStaffListWithoutEmp.push(emp); - }); - }); + let funValWithoutEmp = emp.staff.reduce((funVal, staff) => { + let staffList = solution(staff); + funStaffListWithoutEmp = funStaffListWithoutEmp.concat(staffList); + return staffList.reduce((acc, emp) => (acc + emp.funVal), funVal); + }, 0); let funValWithEmp = emp.funVal; let funStaffListWithEmp = [emp]; emp.staff.forEach((staff) => { - staff.staff.forEach((staffstaff) => { - solution(staffstaff).forEach((emp) => { - funValWithEmp += emp.funVal; - funStaffListWithEmp.push(emp); - }); - }); + funValWithEmp = staff.staff.reduce((funVal, staff) => { + let staffList = solution(staff); + funStaffListWithEmp = funStaffListWithEmp.concat(staffList); + return staffList.reduce((acc, emp) => (acc + emp.funVal), funVal); + }, funValWithEmp); }); - if (funValWithoutEmp > funValWithEmp) { - return funStaffListWithoutEmp; - } - return funStaffListWithEmp; + return funValWithoutEmp > funValWithEmp ? + funStaffListWithoutEmp : funStaffListWithEmp; }; module.exports = { + Employee, solution }; diff --git a/test/58.js b/test/58.js index 218fb16..1d4d2da 100644 --- a/test/58.js +++ b/test/58.js @@ -1,22 +1,9 @@ const expect = require('chai').expect; const solution = require('../solutions/58.js').solution; +const Employee = require('../solutions/58.js').Employee; // solution = require('../yourSolution').solution; -describe('return smallest number of coins required to return the change', () => { - class Employee { - constructor(funVal, staffArray=[]) { - this.funVal = funVal; - this.staff = []; - this.addStaff(staffArray); - } - - addStaff(staffArray) { - staffArray.forEach((staff) => { - this.staff.push(staff); - }); - } - } - +describe('return an array of employees that creates the max fun', () => { let emp1 = new Employee(1); let emp2 = new Employee(5); let emp3 = new Employee(11); From c6e25ef89faa71233f71f0b1e17201ceb6715b2b Mon Sep 17 00:00:00 2001 From: Yoo Jin Lim Date: Wed, 14 Jun 2017 10:56:41 -0700 Subject: [PATCH 14/31] remove unnecessary newline --- solutions/58.js | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/solutions/58.js b/solutions/58.js index 361dd86..3b94e48 100644 --- a/solutions/58.js +++ b/solutions/58.js @@ -5,12 +5,6 @@ // Solution by Yoo Jin -/** - * Given an Employee, return an array of employees that creates the maximum fun. - * There is one condition: An Employee cannot be on the guestlist if his direct manager is going to the party. - * @param {Employee} emp - an Employee with fun value and a list of his staff - * @returns {Employee []]} list - Array of employees that creates the maximum fun - */ class Employee { constructor(funVal, staffArray=[]) { this.funVal = funVal; @@ -25,7 +19,12 @@ class Employee { } } - +/** + * Given an Employee, return an array of employees that creates the maximum fun. + * There is one condition: An Employee cannot be on the guestlist if his direct manager is going to the party. + * @param {Employee} emp - an Employee with fun value and a list of his staff + * @returns {Employee []]} list - Array of employees that creates the maximum fun + */ const solution = (emp) => { if(!emp) { return []; From 4d937c07ef56a406565f8bc45f16e71257adbb6d Mon Sep 17 00:00:00 2001 From: seemcat Date: Sun, 4 Jun 2017 23:08:53 -0700 Subject: [PATCH 15/31] Test & Solution for #8 --- solutions/8a.js | 16 ++++++++++++++++ test/8a.js | 15 +++++++++++++++ 2 files changed, 31 insertions(+) create mode 100644 solutions/8a.js create mode 100644 test/8a.js diff --git a/solutions/8a.js b/solutions/8a.js new file mode 100644 index 0000000..09f5d35 --- /dev/null +++ b/solutions/8a.js @@ -0,0 +1,16 @@ +// Maricris Bonzo +// Create a function that returns a string in reverse. + +const solution = (string) => { + let indexLength = string.length - 1; + let reverseString = ''; + + for(i = 0; i <= indexLength; i++){ + reverseString = string[i] + reverseString; + } + return reverseString; +}; + +module.exports = { + solution +}; diff --git a/test/8a.js b/test/8a.js new file mode 100644 index 0000000..223cd12 --- /dev/null +++ b/test/8a.js @@ -0,0 +1,15 @@ +const expect = require('chai').expect; +let solution = require('../solutions/8a').solution; + +describe('reverse String', () => { + it('should return a string in reverse', () => { + const actual = "react"; + const expected = "tcaer"; + expect(solution(actual)).to.equal(expected); + }); + it('should return a string in reverse', () => { + const actual = "hello"; + const expected = "olleh"; + expect(solution(actual)).to.equal(expected); + }); +}); From 33f4991fb2b398bda183844d26a869715e638846 Mon Sep 17 00:00:00 2001 From: seemcat Date: Mon, 5 Jun 2017 11:26:13 -0700 Subject: [PATCH 16/31] Necessary Changes --- solutions/8.js | 13 +++++++++++++ solutions/8a.js | 16 ---------------- test/8.js | 6 ++++++ test/8a.js | 15 --------------- 4 files changed, 19 insertions(+), 31 deletions(-) delete mode 100644 solutions/8a.js delete mode 100644 test/8a.js diff --git a/solutions/8.js b/solutions/8.js index 16d6ed8..9328029 100644 --- a/solutions/8.js +++ b/solutions/8.js @@ -9,6 +9,19 @@ const solution = (string) => { return newString; }; +// Maricris Bonzo: seemcat + +const solution1 = (string) => { + let indexLength = string.length - 1; + let reverseString = ''; + + for(i = 0; i <= indexLength; i++){ + reverseString = string[i] + reverseString; + } + return reverseString; +}; + module.exports = { solution, + solution1 }; diff --git a/solutions/8a.js b/solutions/8a.js deleted file mode 100644 index 09f5d35..0000000 --- a/solutions/8a.js +++ /dev/null @@ -1,16 +0,0 @@ -// Maricris Bonzo -// Create a function that returns a string in reverse. - -const solution = (string) => { - let indexLength = string.length - 1; - let reverseString = ''; - - for(i = 0; i <= indexLength; i++){ - reverseString = string[i] + reverseString; - } - return reverseString; -}; - -module.exports = { - solution -}; diff --git a/test/8.js b/test/8.js index edb956b..61a4218 100644 --- a/test/8.js +++ b/test/8.js @@ -1,6 +1,12 @@ const expect = require('chai').expect; let solution = require('../solutions/8').solution; +<<<<<<< HEAD solution = require('../yourSolution').solution; +======= +let solution1 = require('../solutions/8').solution; + +// solution = require('./yourSolution').solution; +>>>>>>> Necessary Changes describe('reverse String', () => { it('should reverse a string in reverse', () => { diff --git a/test/8a.js b/test/8a.js deleted file mode 100644 index 223cd12..0000000 --- a/test/8a.js +++ /dev/null @@ -1,15 +0,0 @@ -const expect = require('chai').expect; -let solution = require('../solutions/8a').solution; - -describe('reverse String', () => { - it('should return a string in reverse', () => { - const actual = "react"; - const expected = "tcaer"; - expect(solution(actual)).to.equal(expected); - }); - it('should return a string in reverse', () => { - const actual = "hello"; - const expected = "olleh"; - expect(solution(actual)).to.equal(expected); - }); -}); From d3c2b16c453a28c3c8a564eba8ca7f0197843cc1 Mon Sep 17 00:00:00 2001 From: seemcat Date: Wed, 7 Jun 2017 07:57:37 -0700 Subject: [PATCH 17/31] Modified test file for #8 --- test/8.js | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/test/8.js b/test/8.js index 61a4218..b98b36f 100644 --- a/test/8.js +++ b/test/8.js @@ -1,12 +1,7 @@ const expect = require('chai').expect; let solution = require('../solutions/8').solution; -<<<<<<< HEAD -solution = require('../yourSolution').solution; -======= -let solution1 = require('../solutions/8').solution; - +let solution1 = require('../solutions/8').solution1; // solution = require('./yourSolution').solution; ->>>>>>> Necessary Changes describe('reverse String', () => { it('should reverse a string in reverse', () => { @@ -29,4 +24,25 @@ describe('reverse String', () => { const expected = "welcome emoclew"; expect(solution(actual)).to.equal(expected); }); + it('should reverse a string in reverse', () => { + const actual = "react"; + const expected = "tcaer"; + expect(solution1(actual)).to.equal(expected); + }); + it('should reverse a string in reverse', () => { + const actual = "word"; + const expected = "drow"; + expect(solution1(actual)).to.equal(expected); + }); + it('should reverse a string in reverse', () => { + const actual = "I"; + const expected = "I"; + expect(solution1(actual)).to.equal(expected); + }); + it('should reverse a string in reverse', () => { + const actual = "welcome emoclew"; + const expected = "welcome emoclew"; + expect(solution1(actual)).to.equal(expected); + }); + }); From f5a0be489920e286e9812dc98c4198d085debb58 Mon Sep 17 00:00:00 2001 From: seemcat Date: Fri, 9 Jun 2017 11:28:10 -0700 Subject: [PATCH 18/31] Test & Solution for #31. --- solutions/31.js | 20 +++++++++++++++++--- test/31.js | 16 ++++++++++++++++ 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/solutions/31.js b/solutions/31.js index 4e6a1b3..5282448 100644 --- a/solutions/31.js +++ b/solutions/31.js @@ -3,12 +3,12 @@ // input: ['hi','hello','hola'] // output 'hello' -// Solutions by Colin Xie @ColinX13 - +// Solution by Colin Xie @ColinX13 +// Solution1 by Maricris Bonzo @Seemcat /** * @param {string[]} arr - the array of strings * @return {string} word - the longest word in the array -*/ + */ const solution = (arr) => { let maxLength = 0; let word = 0; @@ -20,6 +20,20 @@ const solution = (arr) => { } return word; }; + +const solution1 = (arr) => { + let big = arr[0]; + let i = 1; + while(i < arr.length){ + if (big.length < arr[i].length){ + big = arr[i]; + } + i++; + }; + return big; +}; + module.exports = { solution, + solution1 }; diff --git a/test/31.js b/test/31.js index a3982d2..ccd624f 100644 --- a/test/31.js +++ b/test/31.js @@ -1,5 +1,6 @@ const expect = require('chai').expect; let solution = require('../solutions/31').solution; +let solution1 = require('../solutions/31').solution1; // solution = require('./yourSolution').solution; describe('longest string in an array', () => { @@ -19,4 +20,19 @@ describe('longest string in an array', () => { it('the longest string in [just] is just', () => { expect(solution(['just'])).eql('just'); }); + it.only('the longest string in [hi,hello,hola] is hello', () => { + expect(solution1(['hi','hello','hola'])).eql('hello'); + }) + it.only('the longest string in [is, whether, approximately] is approximately', () => { + expect(solution1(['is','whether','approximately'])).eql('approximately'); + }) + it.only('the longest string in [wait,see,us] is wait', () => { + expect(solution1(['wait','see','us'])).eql('wait'); + }) + it.only('the longest string in [shout,basic,lead] is shout', () => { + expect(solution1(['shout','basic','lead'])).eql('shout'); + }) + it.only('the longest string in [just] is just', () => { + expect(solution1(['just'])).eql('just'); + }) }); From 404c0c01336c500362fd6d78dbe6a1a360a017c4 Mon Sep 17 00:00:00 2001 From: Rahul Kalra Date: Thu, 8 Jun 2017 09:03:13 -0700 Subject: [PATCH 19/31] another solution for problem #13 --- solutions/13.js | 16 ++++++++++++++++ test/13.js | 15 +++++++++------ 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/solutions/13.js b/solutions/13.js index 65dc514..73fddf1 100644 --- a/solutions/13.js +++ b/solutions/13.js @@ -1,3 +1,17 @@ +//Rahul Kalra +//Checking to see if the string is palindrome + +const solution = (palindomeInput) =>{ + let reverse = ""; + for(let i = palindomeInput.length - 1; i >= 0; i--){ + reverse += palindomeInput[i]; + } + if(reverse === palindomeInput){ + return true; + } + return false; +}; + // vdewinter // Return true if input string is a palindrome @@ -15,4 +29,6 @@ const solution = (str) => { module.exports = { solution, + solution2 }; + diff --git a/test/13.js b/test/13.js index aee0fd3..ab0e319 100644 --- a/test/13.js +++ b/test/13.js @@ -1,12 +1,15 @@ const expect = require('chai').expect; let solution = require('../solutions/13').solution; + solution2 = require('../solutions/13').solution2; // solution = require('./yourSolution').solution; describe('reverse String', () => { - it('should return true if input string is a palindrome', () => { - expect(solution('racecar')).to.equal(true); - }); - it('should return true if input string is a palindrome', () => { - expect(solution('palindrome')).to.equal(false); - }); + it('should return true if input string is a palindrome', () => { + expect(solution("racecar")).to.equal(true); + expect(solution2("racecar")).to.equal(true); + }); + it('should return true if input string is a palindrome', () => { + expect(solution("palindrome")).to.equal(false); + expect(solution2("palindrome")).to.equal(false); + }); }); From d74d5396d85a837823897cc1fc5f6be048c6aabe Mon Sep 17 00:00:00 2001 From: Rahul Kalra Date: Tue, 20 Jun 2017 11:59:21 -0700 Subject: [PATCH 20/31] updated solution --- solutions/13.js | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/solutions/13.js b/solutions/13.js index 73fddf1..0940901 100644 --- a/solutions/13.js +++ b/solutions/13.js @@ -1,12 +1,11 @@ //Rahul Kalra //Checking to see if the string is palindrome - -const solution = (palindomeInput) =>{ +const solution2 = (palindromeInput) =>{ let reverse = ""; - for(let i = palindomeInput.length - 1; i >= 0; i--){ - reverse += palindomeInput[i]; + for(let i = palindromeInput.length - 1; i >= 0; i--){ + reverse += palindromeInput[i]; } - if(reverse === palindomeInput){ + if(reverse === palindromeInput){ return true; } return false; From 2d636332a3703115c8227b517447a607f7feefa8 Mon Sep 17 00:00:00 2001 From: Rahul Kalra Date: Tue, 20 Jun 2017 20:00:24 -0700 Subject: [PATCH 21/31] deleted extra spaces #problem13 --- test/13.js | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/test/13.js b/test/13.js index ab0e319..1fd37fd 100644 --- a/test/13.js +++ b/test/13.js @@ -1,15 +1,13 @@ const expect = require('chai').expect; let solution = require('../solutions/13').solution; solution2 = require('../solutions/13').solution2; -// solution = require('./yourSolution').solution; - describe('reverse String', () => { - it('should return true if input string is a palindrome', () => { - expect(solution("racecar")).to.equal(true); - expect(solution2("racecar")).to.equal(true); - }); - it('should return true if input string is a palindrome', () => { - expect(solution("palindrome")).to.equal(false); - expect(solution2("palindrome")).to.equal(false); + it('should return true if input string is a palindrome', () => { + expect(solution("racecar")).to.equal(true); + expect(solution2("racecar")).to.equal(true); + }); + it('should return true if input string is a palindrome', () => { + expect(solution("palindrome")).to.equal(false); + expect(solution2("palindrome")).to.equal(false); }); }); From b20b514bd41b4476e02843833cb7577859600f4a Mon Sep 17 00:00:00 2001 From: Colin Xie Date: Wed, 21 Jun 2017 12:19:27 -0700 Subject: [PATCH 22/31] solutions and test #15 --- solutions/15.js | 12 ++++++++++++ test/15.js | 23 +++++++++++++++++++++-- 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/solutions/15.js b/solutions/15.js index 981b075..5f1825f 100644 --- a/solutions/15.js +++ b/solutions/15.js @@ -9,6 +9,18 @@ const solution = (fullString, subString) => { return fullString.includes(subString); }; +// Colin Xie +const solution1 = (fullString, subString) => { + for(let i = 0; i < fullString.length; i++){ + let subFull = fullString.substring(i,i + subString.length); + if(subFull === subString){ + return true; + } + } + return false; +}; + module.exports = { solution, + solution1, }; diff --git a/test/15.js b/test/15.js index 26783f7..a962e3b 100644 --- a/test/15.js +++ b/test/15.js @@ -1,16 +1,35 @@ const expect = require('chai').expect; let solution = require('../solutions/15').solution; +let solution1 = require('../solutions/15').solution1; // solution = require('../yourSolution').solution; describe('is substring', () => { it('should return true if second input is a substring of first input', () => { - const result = solution('all your base are belong to us', 'ase ar'); + let result = solution('all your base are belong to us', 'ase ar'); + expect(result).to.equal(true); + result = solution1('all your base are belong to us', 'ase ar'); expect(result).to.equal(true); }); it('should return false is second input is NOT a substring of first input', () => { - const result = solution('i love tacos more than you', 'carne asada'); + let result = solution('i love tacos more than you', 'carne asada'); + expect(result).to.equal(false); + result = solution1('i love tacos more than you', 'carne asada'); + expect(result).to.equal(false); + }); + + it('should return true if second input is a substring of the first input', () => { + let result = solution('ab cd ef g', 'ef'); + expect(result).to.equal(true); + result = solution1('ab cd ef g', 'ef'); + expect(result).to.equal(true); + }); + + it('should return false if second input is NOT a substring of first input', () => { + let result = solution('can', 'cn'); + expect(result).to.equal(false); + result = solution1('can', 'cn'); expect(result).to.equal(false); }); }); From 442d75206b1623c1e2a586315ba18ed3f56fd288 Mon Sep 17 00:00:00 2001 From: Yoo Jin Lim Date: Mon, 3 Jul 2017 17:22:11 -0700 Subject: [PATCH 23/31] fix test issues --- test/13.js | 2 +- test/31.js | 10 +++++----- test/32.js | 2 +- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/test/13.js b/test/13.js index 1fd37fd..aa7c067 100644 --- a/test/13.js +++ b/test/13.js @@ -1,6 +1,6 @@ const expect = require('chai').expect; let solution = require('../solutions/13').solution; - solution2 = require('../solutions/13').solution2; +let solution2 = require('../solutions/13').solution2; describe('reverse String', () => { it('should return true if input string is a palindrome', () => { expect(solution("racecar")).to.equal(true); diff --git a/test/31.js b/test/31.js index ccd624f..9ebe7c5 100644 --- a/test/31.js +++ b/test/31.js @@ -20,19 +20,19 @@ describe('longest string in an array', () => { it('the longest string in [just] is just', () => { expect(solution(['just'])).eql('just'); }); - it.only('the longest string in [hi,hello,hola] is hello', () => { + it('the longest string in [hi,hello,hola] is hello', () => { expect(solution1(['hi','hello','hola'])).eql('hello'); }) - it.only('the longest string in [is, whether, approximately] is approximately', () => { + it('the longest string in [is, whether, approximately] is approximately', () => { expect(solution1(['is','whether','approximately'])).eql('approximately'); }) - it.only('the longest string in [wait,see,us] is wait', () => { + it('the longest string in [wait,see,us] is wait', () => { expect(solution1(['wait','see','us'])).eql('wait'); }) - it.only('the longest string in [shout,basic,lead] is shout', () => { + it('the longest string in [shout,basic,lead] is shout', () => { expect(solution1(['shout','basic','lead'])).eql('shout'); }) - it.only('the longest string in [just] is just', () => { + it('the longest string in [just] is just', () => { expect(solution1(['just'])).eql('just'); }) }); diff --git a/test/32.js b/test/32.js index 9fad46d..946aec9 100644 --- a/test/32.js +++ b/test/32.js @@ -2,7 +2,7 @@ const expect = require('chai').expect; let solution = require('../solutions/32').solution; describe('Finding singleton in an array', () =>{ - it.only('should return singleton in an array', () =>{ + it('should return singleton in an array', () =>{ expect(solution([1,2,3,3,1,4])).to.eql([2, 4]); }); }); From 500287ed964dd0da4fadb86a9d80d2cd5b95f4a4 Mon Sep 17 00:00:00 2001 From: Yoo Jin Lim Date: Tue, 4 Jul 2017 17:33:17 -0700 Subject: [PATCH 24/31] remove obsolte yourSolution file --- solutions/yourSolution.js | 9 --------- 1 file changed, 9 deletions(-) delete mode 100644 solutions/yourSolution.js diff --git a/solutions/yourSolution.js b/solutions/yourSolution.js deleted file mode 100644 index f69d2db..0000000 --- a/solutions/yourSolution.js +++ /dev/null @@ -1,9 +0,0 @@ -// Name -// Problem Description - -const yourSolution = () => { -}; - -module.exports = { - yourSolution, -}; From 5af7d9fa47e5e6103e89b10df9c2f4e8a49f7019 Mon Sep 17 00:00:00 2001 From: Yoo Jin Lim Date: Tue, 4 Jul 2017 17:33:22 -0700 Subject: [PATCH 25/31] fix the test imports and improve template in yourSolution.js --- test/11.js | 2 +- test/13.js | 2 ++ test/16.js | 2 +- test/19.js | 3 ++- test/21.js | 3 ++- test/31.js | 2 +- test/32.js | 1 + test/5.js | 1 + test/59.js | 2 +- test/6.js | 1 + test/60.js | 2 +- test/61.js | 2 +- test/64.js | 2 +- test/77.js | 1 + test/8.js | 2 +- yourSolution.js | 15 +++++++++++++-- 16 files changed, 31 insertions(+), 12 deletions(-) diff --git a/test/11.js b/test/11.js index 69372e9..e5982a2 100644 --- a/test/11.js +++ b/test/11.js @@ -1,6 +1,6 @@ const expect = require('chai').expect; let solution = require('../solutions/11').solution; -// solution = require('./yourSolution').solution; +// solution = require('../yourSolution').solution; describe('repeating elements', () => { it('should return repeating elements of an array', () => { diff --git a/test/13.js b/test/13.js index aa7c067..8c6becd 100644 --- a/test/13.js +++ b/test/13.js @@ -1,6 +1,8 @@ const expect = require('chai').expect; let solution = require('../solutions/13').solution; let solution2 = require('../solutions/13').solution2; +// solution = require('../yourSolution').solution; + describe('reverse String', () => { it('should return true if input string is a palindrome', () => { expect(solution("racecar")).to.equal(true); diff --git a/test/16.js b/test/16.js index a7c7210..0c02457 100644 --- a/test/16.js +++ b/test/16.js @@ -1,6 +1,6 @@ const expect = require('chai').expect; let solution = require('../solutions/16').solution; -// solution = require('./yourSolution').solution; +// solution = require('../yourSolution').solution; describe('check duplicates', () => { it('should return true since array has duplicates', () => { diff --git a/test/19.js b/test/19.js index cfc07b6..8a57e65 100644 --- a/test/19.js +++ b/test/19.js @@ -1,6 +1,7 @@ const expect = require('chai').expect; let solution = require('../solutions/19').solution; - +// solution = require('../yourSolution').solution; + describe('Pairs sum to x', () => { it('should return pairs of number that sum to x', () => { const result = solution([1,2,3,4,5], 6); diff --git a/test/21.js b/test/21.js index 408d751..68507ea 100644 --- a/test/21.js +++ b/test/21.js @@ -1,6 +1,7 @@ let expect = require('chai').expect; let solution = require('../solutions/21').solution; - +// solution = require('../yourSolution').solution; + describe('Return 3 top largest values of an array in a descending order', () =>{ it('should return 3 top largest values of an array', () =>{ expect(solution([8,1,4,2,12,6,7,19,2,9])).to.eql([19, 12, 9]); diff --git a/test/31.js b/test/31.js index 9ebe7c5..a7445ea 100644 --- a/test/31.js +++ b/test/31.js @@ -1,7 +1,7 @@ const expect = require('chai').expect; let solution = require('../solutions/31').solution; let solution1 = require('../solutions/31').solution1; -// solution = require('./yourSolution').solution; +// solution = require('../yourSolution').solution; describe('longest string in an array', () => { it('the longest string in [hi,hello,hola] is hello', () => { diff --git a/test/32.js b/test/32.js index 946aec9..cb99066 100644 --- a/test/32.js +++ b/test/32.js @@ -1,5 +1,6 @@ const expect = require('chai').expect; let solution = require('../solutions/32').solution; +// solution = require('../yourSolution').solution; describe('Finding singleton in an array', () =>{ it('should return singleton in an array', () =>{ diff --git a/test/5.js b/test/5.js index 5bb5cb0..2d98deb 100644 --- a/test/5.js +++ b/test/5.js @@ -1,6 +1,7 @@ const expect = require('chai').expect; let solution = require('../solutions/5').solution; let solution2 = require('../solutions/5').solution2; +// solution = require('../yourSolution').solution; describe('sort arrays', () => { // solution tests diff --git a/test/59.js b/test/59.js index 126ec12..f336676 100644 --- a/test/59.js +++ b/test/59.js @@ -1,6 +1,6 @@ const expect = require('chai').expect; let solution = require('../solutions/59').solution; -// solution = require('./yourSolution').solution; +// solution = require('../yourSolution').solution; describe('prime numbers', () => { it('1 should not be a prime number', () => { diff --git a/test/6.js b/test/6.js index e0a523c..2b21434 100644 --- a/test/6.js +++ b/test/6.js @@ -1,5 +1,6 @@ const expect = require('chai').expect; const tree = require('../solutions/6'); +// tree = require('../yourSolution'); const Node = tree.node; describe('tree', () => { diff --git a/test/60.js b/test/60.js index 5676880..e27b9a8 100644 --- a/test/60.js +++ b/test/60.js @@ -1,6 +1,6 @@ const expect = require('chai').expect; let solution = require('../solutions/60').solution; -// solution = require('./yourSolution').solution; +// solution = require('../yourSolution').solution; describe('sum of factors', () => { it('sum of the factors of 2 is 3', () => { diff --git a/test/61.js b/test/61.js index a42bea1..b644659 100644 --- a/test/61.js +++ b/test/61.js @@ -1,7 +1,7 @@ const expect = require('chai').expect; let solution = require('../solutions/61').solution; let solution1 = require('../solutions/61').solution1; -// solution = require('./yourSolution').solution; +// solution = require('../yourSolution').solution; describe('greatest common denominator', () => { it('the gcd for 16 and 24 is 8', () => { diff --git a/test/64.js b/test/64.js index 3a607a6..084f66a 100644 --- a/test/64.js +++ b/test/64.js @@ -1,6 +1,6 @@ const expect = require('chai').expect; let solution = require('../solutions/64').solution; -// solution = require('./yourSolution').solution; +// solution = require('../yourSolution').solution; describe('reverse word order in a sentence', () => { it('should reverse the word order', () => { diff --git a/test/77.js b/test/77.js index 9f924bf..751906e 100644 --- a/test/77.js +++ b/test/77.js @@ -1,5 +1,6 @@ const expect = require('chai').expect; let solution = require('../solutions/77').solution; +// solution = require('../yourSolution').solution; describe('Return the count of vowels and consonants', () =>{ it('should return { vowel: 2, consonant: 4 } for "yellow"', () =>{ diff --git a/test/8.js b/test/8.js index b98b36f..fe60932 100644 --- a/test/8.js +++ b/test/8.js @@ -1,7 +1,7 @@ const expect = require('chai').expect; let solution = require('../solutions/8').solution; let solution1 = require('../solutions/8').solution1; -// solution = require('./yourSolution').solution; +// solution = require('../yourSolution').solution; describe('reverse String', () => { it('should reverse a string in reverse', () => { diff --git a/yourSolution.js b/yourSolution.js index 8a4f885..4547eda 100644 --- a/yourSolution.js +++ b/yourSolution.js @@ -1,7 +1,18 @@ -// Name -// Problem Description +// [ProblemTitle] +// [OneLineProblemDescription] +// Input: +// Output: + +// Solution by [yourName] + +/** + * [DetailedDescription] + * @param + * @returns + */ const solution = () => { + return ''; }; module.exports = { From 100b5c139353ba7cc08e120fbd74cb54da35f895 Mon Sep 17 00:00:00 2001 From: Himanshu Nachane Date: Fri, 7 Jul 2017 08:44:47 +0530 Subject: [PATCH 26/31] Solution to issue #18 in Python (#26) * Solution to issue #18 in Python * Modified the code,function created to allow passing the parameters * Tests for issue #18 added. SKELETON CODE to test python(2.7) solutions created. * Modification to 18.py Comments in the code have been removed. for loop has been used as discussed. * Modification to test18.py Tests have been modified as per the modified code of 18.py . The code has been tested and it passed the test cases. * Solution to issue #34 in Python 2.7 * Delete 34.py * Delete test34.py * print statement removed from solution * Test skeleton moved from solutions to tests * Test modified to eliminate redundancy. pythontest_skeleton modified with new changes. Name of solution changed from 18.py to sol18.py to avoid conflict while importing the file for testing * Renamed test18.py to 18.py and deleted pythontest_skeleton --- solutions/sol18.py | 17 +++++++++++++++++ test/18.py | 14 ++++++++++++++ 2 files changed, 31 insertions(+) create mode 100644 solutions/sol18.py create mode 100644 test/18.py diff --git a/solutions/sol18.py b/solutions/sol18.py new file mode 100644 index 0000000..0830897 --- /dev/null +++ b/solutions/sol18.py @@ -0,0 +1,17 @@ +#Himanshu Nachane (@Himanshu1495) +'''return missing number in an input array of consecutively increasing numbers. + +i: [5,6,8,9] +o: 7 +''' + +def missing_num(arr): + first_element = arr[0] + last_element = arr[-1] + new_sum = 0 + for x in range(first_element,last_element+1): + new_sum += x + return new_sum - sum(arr) + + + diff --git a/test/18.py b/test/18.py new file mode 100644 index 0000000..164ffa9 --- /dev/null +++ b/test/18.py @@ -0,0 +1,14 @@ +import sys +sys.path.insert(0,'../solutions/') +from sol18 import missing_num +import unittest + +class MyTest(unittest.TestCase): + def test1(self): + self.assertEqual(missing_num([10,11,12,14,15,16]),13) + def test2(self): + self.assertEqual(missing_num([125,126,128,129,130]),127) + def test3(self): + self.assertEqual(missing_num([1125,1127,1128,1129,1130]),1126) + +unittest.main() From 80bf94ae9c4820e5737940e2b9173743d2e87ad3 Mon Sep 17 00:00:00 2001 From: Himanshu Nachane Date: Fri, 7 Jul 2017 08:45:57 +0530 Subject: [PATCH 27/31] Solution and Test for issue #34 (#107) * Solution and Test for issue #34 in Python 2.7 * Solution and Test for Issue #34 --- solutions/sol34.py | 13 +++++++++++++ test/34.py | 22 ++++++++++++++++++++++ 2 files changed, 35 insertions(+) create mode 100644 solutions/sol34.py create mode 100644 test/34.py diff --git a/solutions/sol34.py b/solutions/sol34.py new file mode 100644 index 0000000..ea80e78 --- /dev/null +++ b/solutions/sol34.py @@ -0,0 +1,13 @@ +#Himanshu Nachane (@Himanshu1495) +''' +Input: An array, a number +Output: number of times the number shows up in the array. + +Input: [1,2,2,3], 3 +Output: 1 +''' + +def repeating_element(array,number): + return array.count(number) + +print repeating_element([2,3,4,5,3],3) \ No newline at end of file diff --git a/test/34.py b/test/34.py new file mode 100644 index 0000000..229fd19 --- /dev/null +++ b/test/34.py @@ -0,0 +1,22 @@ +''' +Input: An array, a number +Output: number of times the number shows up in the array. + +Input: [1,2,2,3], 3 +Output: 1 +''' +import sys +sys.path.insert(0,'../solutions/') +from sol34 import repeating_element +import unittest + + +class MyTest(unittest.TestCase): + def test1(self): + self.assertEqual(repeating_element([10,1,12,4,16,16],16),2) + def test2(self): + self.assertEqual(repeating_element([-164,170,61,-164,152,44,16,-164],-164),3) + def test3(self): + self.assertEqual(repeating_element([1340,1456,1452,4546,1667,1237],1667),1) + +unittest.main() From d50a3f64746a64917659da6eefc182722048857d Mon Sep 17 00:00:00 2001 From: Daniel Date: Sat, 12 Aug 2017 19:44:05 -0700 Subject: [PATCH 28/31] flatten array --- solutions/116.js | 21 +++++++++++++++++++++ test/116.js | 21 +++++++++++++++++++++ 2 files changed, 42 insertions(+) create mode 100644 solutions/116.js create mode 100644 test/116.js diff --git a/solutions/116.js b/solutions/116.js new file mode 100644 index 0000000..66bf7ad --- /dev/null +++ b/solutions/116.js @@ -0,0 +1,21 @@ +// flatten the array +// +const solution = (arr) => { + if (arr.length === 0) { + return arr; + } + let a = arr.shift(); + if (a.length) { + a.forEach((elem) => { + arr.push(elem); + }); + solution(arr); + } else { + solution(arr).push(a); + } + return arr; +}; + +module.exports = { + solution: solution, +}; diff --git a/test/116.js b/test/116.js new file mode 100644 index 0000000..4105932 --- /dev/null +++ b/test/116.js @@ -0,0 +1,21 @@ +const expect = require('chai').expect; +let solution = require('../solutions/116.js').solution; + +describe('flatten the array', () => { + it('test 1 base', () => { + const result = solution([]); + expect(result).to.eql([]); + }); + it('test 2 simple', () => { + const result = solution([1]); + expect(result).to.eql([1]); + }); + it('test 3 hairy', () => { + const result = solution([3, [5, 2, 6], 4]); + expect(result).to.eql([6, 2, 5, 4, 3]); + }); + it('test 4 complex', () => { + const result = solution([10, 4, [2, [1, 3]], 6, 10]); + expect(result).to.eql([3, 1, 2, 10, 6, 4, 10]); + }); +}); From ef07689806b61bde728b7f6f9c06c847e2e05200 Mon Sep 17 00:00:00 2001 From: Daniel Date: Sat, 12 Aug 2017 20:01:58 -0700 Subject: [PATCH 29/31] min coin solution --- solutions/56.js | 19 +++++++++++++++++++ test/56.js | 16 ++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/solutions/56.js b/solutions/56.js index 3d63dc5..e59b3e4 100644 --- a/solutions/56.js +++ b/solutions/56.js @@ -27,7 +27,26 @@ const solution = (arr, total) => { return acc; }, total); }; +// return min number of coins +// daniel s. +const solution2 = (arr, value) => { + if (value === 0) { + return 0; + }; + let currentMin = value; + arr.forEach((coin)=> { + let childCoins = 0; + if (value - coin > 0) { + childCoins = solution(arr, value - coin); + } + if ( childCoins < currentMin) { + currentMin = childCoins; + } + }); + return currentMin + 1; +}; module.exports = { solution, + solution2 }; diff --git a/test/56.js b/test/56.js index d9e103e..4446a18 100644 --- a/test/56.js +++ b/test/56.js @@ -1,5 +1,6 @@ const expect = require('chai').expect; const solution = require('../solutions/56.js').solution; +const solution2 = require('../solutions/56.js').solution2; // solution = require('../yourSolution').solution; describe('return smallest number of coins required to return the change', @@ -19,5 +20,20 @@ describe('return smallest number of coins required to return the change', it('hard case [1,2,3,6,7], total=12', () => { expect(solution([1, 2, 3, 6, 7], 12)).to.equal(2); }); + it('simplest case [1], total=1', () => { + expect(solution2([1], 1)).to.equal(1); + }); + it('simplest case [1,2], total=0', () => { + expect(solution2([1, 2], 0)).to.equal(0); + }); + it('simple case [1,2], total=2', () => { + expect(solution2([1, 2], 2)).to.equal(1); + }); + it('simple case [1,2], total=4', () => { + expect(solution2([1, 2], 4)).to.equal(2); + }); + it('hard case [1,2,3,6,7], total=12', () => { + expect(solution2([1, 2, 3, 6, 7], 12)).to.equal(2); + }); }); From f18888fa11e330fb14187a863fac67ccfe0af1eb Mon Sep 17 00:00:00 2001 From: Daniel Date: Mon, 14 Aug 2017 13:13:28 -0700 Subject: [PATCH 30/31] fix comments --- solutions/116.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/solutions/116.js b/solutions/116.js index 66bf7ad..ca5083b 100644 --- a/solutions/116.js +++ b/solutions/116.js @@ -4,14 +4,14 @@ const solution = (arr) => { if (arr.length === 0) { return arr; } - let a = arr.shift(); - if (a.length) { - a.forEach((elem) => { - arr.push(elem); - }); + let elem = arr.shift(); + if (elem.length) { + elem.forEach((elem) => { + arr.push(elem); + }); solution(arr); } else { - solution(arr).push(a); + solution(arr).push(elem); } return arr; }; From 72058986491264c384cdb76ebd8c0ad3053afca5 Mon Sep 17 00:00:00 2001 From: Daniel Date: Mon, 14 Aug 2017 13:27:09 -0700 Subject: [PATCH 31/31] fix comments --- solutions/56.js | 8 +++----- test/56.js | 18 ++++-------------- 2 files changed, 7 insertions(+), 19 deletions(-) diff --git a/solutions/56.js b/solutions/56.js index e59b3e4..a37c3f2 100644 --- a/solutions/56.js +++ b/solutions/56.js @@ -37,16 +37,14 @@ const solution2 = (arr, value) => { arr.forEach((coin)=> { let childCoins = 0; if (value - coin > 0) { - childCoins = solution(arr, value - coin); - } - if ( childCoins < currentMin) { - currentMin = childCoins; + childCoins = solution2(arr, value - coin); } + currentMin = Math.min(currentMin, childCoins); }); return currentMin + 1; }; module.exports = { solution, - solution2 + solution2, }; diff --git a/test/56.js b/test/56.js index 4446a18..cc2d7e2 100644 --- a/test/56.js +++ b/test/56.js @@ -7,32 +7,22 @@ describe('return smallest number of coins required to return the change', () => { it('simplest case [1], total=1', () => { expect(solution([1], 1)).to.equal(1); - }); - it('simplest case [1,2], total=0', () => { - expect(solution([1, 2], 0)).to.equal(0); - }); - it('simple case [1,2], total=2', () => { - expect(solution([1, 2], 2)).to.equal(1); - }); - it('simple case [1,2], total=4', () => { - expect(solution([1, 2], 4)).to.equal(2); - }); - it('hard case [1,2,3,6,7], total=12', () => { - expect(solution([1, 2, 3, 6, 7], 12)).to.equal(2); - }); - it('simplest case [1], total=1', () => { expect(solution2([1], 1)).to.equal(1); }); it('simplest case [1,2], total=0', () => { + expect(solution([1, 2], 0)).to.equal(0); expect(solution2([1, 2], 0)).to.equal(0); }); it('simple case [1,2], total=2', () => { + expect(solution([1, 2], 2)).to.equal(1); expect(solution2([1, 2], 2)).to.equal(1); }); it('simple case [1,2], total=4', () => { + expect(solution([1, 2], 4)).to.equal(2); expect(solution2([1, 2], 4)).to.equal(2); }); it('hard case [1,2,3,6,7], total=12', () => { + expect(solution([1, 2, 3, 6, 7], 12)).to.equal(2); expect(solution2([1, 2, 3, 6, 7], 12)).to.equal(2); }); });