From cc2bc4ed9d202a66289f243fb52e121739a36671 Mon Sep 17 00:00:00 2001 From: Francesca Calla Bruc Date: Sun, 22 Jan 2023 12:46:12 -0800 Subject: [PATCH 01/10] added naive search --- src/algorithms/search/index.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/algorithms/search/index.js b/src/algorithms/search/index.js index fd58348c..ebbfa87b 100644 --- a/src/algorithms/search/index.js +++ b/src/algorithms/search/index.js @@ -5,6 +5,7 @@ const exponentialsearch = require('./exponential_search'); const interpolationsearch = require('./interpolation_search'); const jumpsearch = require('./jump_search'); const linearsearch = require('./linear_search'); +const naivesearch = require('./naive_search'); const ternarysearch = require('./ternary_search'); module.exports = { @@ -15,5 +16,6 @@ module.exports = { interpolationsearch, jumpsearch, linearsearch, + naivesearch, ternarysearch }; From 51d06e18f0c782a6a584c252324b5b702910c509 Mon Sep 17 00:00:00 2001 From: Francesca Calla Bruc Date: Sun, 22 Jan 2023 12:46:34 -0800 Subject: [PATCH 02/10] added is_prime function --- src/algorithms/math/is_prime.js | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 src/algorithms/math/is_prime.js diff --git a/src/algorithms/math/is_prime.js b/src/algorithms/math/is_prime.js new file mode 100644 index 00000000..da62d747 --- /dev/null +++ b/src/algorithms/math/is_prime.js @@ -0,0 +1,17 @@ +/** + * Checks if a num is prime or not + * @param {Number} num number to check + * @return {Boolean} true if num is prime, false if num is not prime + * + * References: https://javascript.plainenglish.io/11-mathematical-algorithms-in-modern-javascript-bce71318e2da + */ + const isprime = num => { + const limit = Math.floor(Math.sqrt(num)); + for (let i = 2; i <= limit; i++){ + if (n % i === 0) return false; + } + return n >= 2; + }; + + module.exports = isprime; + \ No newline at end of file From ae6c58d45b76951cd196921086008f71a95a2e18 Mon Sep 17 00:00:00 2001 From: Francesca Calla Bruc Date: Sun, 22 Jan 2023 18:33:46 -0800 Subject: [PATCH 03/10] added basic functions --- src/algorithms/math/index.js | 4 ++++ src/algorithms/math/sum_all.js | 17 +++++++++++++++++ src/algorithms/search/naive_search.js | 21 +++++++++++++++++++++ 3 files changed, 42 insertions(+) create mode 100644 src/algorithms/math/sum_all.js create mode 100644 src/algorithms/search/naive_search.js diff --git a/src/algorithms/math/index.js b/src/algorithms/math/index.js index 41b2e488..34cecf28 100644 --- a/src/algorithms/math/index.js +++ b/src/algorithms/math/index.js @@ -2,6 +2,8 @@ const extendedEuclidean = require('./extended_euclidean'); const gcd = require('./gcd'); const fastexp = require('./fast_exp'); const lcm = require('./lcm'); +const isprime = require('./is_prime'); +const sumall = require('./sum_all'); const modularInverse = require('./modular_inverse'); module.exports = { @@ -9,5 +11,7 @@ module.exports = { gcd, fastexp, lcm, + isprime, + sumall, modularInverse }; diff --git a/src/algorithms/math/sum_all.js b/src/algorithms/math/sum_all.js new file mode 100644 index 00000000..43d94ba9 --- /dev/null +++ b/src/algorithms/math/sum_all.js @@ -0,0 +1,17 @@ +/** + * Calculates the sum of all numbers in an array + * @param {Array} arr of two ints where arr[0] start, arr[1] end + * @return {Number} sum sum of the range of numberse + */ +const sumall = (arr) => { + var sum =0; + var min = arr[0]; + var max = arr[1]; + for (var i = min; i <= max; i++) { + sum += i; + } + + return sum; +}; + +module.exports = sumall; diff --git a/src/algorithms/search/naive_search.js b/src/algorithms/search/naive_search.js new file mode 100644 index 00000000..d8790409 --- /dev/null +++ b/src/algorithms/search/naive_search.js @@ -0,0 +1,21 @@ +/** + * Binary Search Algorithm + * @param {String} st Array to be searched + * @param {String} pattern Element to be searched + * @return {Number} count Frequency the pattern is in st + */ + +const naivesearch = (st,pattern) => + { + let count =0; + for(let i = 0; i < st.length; i++) { + for(let j = 0; j < pattern.length; j++) { + if(st[i + j] !== pattern[j]) break; + if(j === pattern.length - 1) count++; + } + } + + return count; + } + + module.exports = naivesearch; \ No newline at end of file From 00693f6f6913c936aa6f989b18ad363fbb648943 Mon Sep 17 00:00:00 2001 From: Francesca Calla Bruc Date: Sun, 22 Jan 2023 18:53:26 -0800 Subject: [PATCH 04/10] added tests to added functoins --- test/algorithms/math/testPrime.js | 18 ++++++++++++++++++ test/algorithms/math/testSumAll.js | 14 ++++++++++++++ test/algorithms/search/testNaiveSearch.js | 23 +++++++++++++++++++++++ 3 files changed, 55 insertions(+) create mode 100644 test/algorithms/math/testPrime.js create mode 100644 test/algorithms/math/testSumAll.js create mode 100644 test/algorithms/search/testNaiveSearch.js diff --git a/test/algorithms/math/testPrime.js b/test/algorithms/math/testPrime.js new file mode 100644 index 00000000..a2f2717a --- /dev/null +++ b/test/algorithms/math/testPrime.js @@ -0,0 +1,18 @@ +/* eslint-env mocha */ +const isprime = require('../../../src').algorithms.math.isprime; + +const assert = require('assert'); + +describe('isPrime', () => { + it('should return true if number is prime', () => { + assert.equal(isprime(2), true); + assert.equal(isprime(33), true); + assert.equal(isprime(7), true); + assert.equal(isprime(37), true); + }); + it('should return false if number is prime', () => { + assert.equal(isprime(16), false); + assert.equal(isprime(36), false); + assert.equal(isprime(100), false); + }); +}); \ No newline at end of file diff --git a/test/algorithms/math/testSumAll.js b/test/algorithms/math/testSumAll.js new file mode 100644 index 00000000..ee049eca --- /dev/null +++ b/test/algorithms/math/testSumAll.js @@ -0,0 +1,14 @@ +/* eslint-env mocha */ +const sumall = require('../../../src').algorithms.math.sumall; + +const assert = require('assert'); + +describe('sumALL', () => { + it('should return the sum of range', () => { + assert.equal(sumall(2,5), 14); + assert.equal(sumall(1,3), 6); + assert.equal(sumall(3,6), 18); + assert.equal(sumall(37), true); + }); + +}); \ No newline at end of file diff --git a/test/algorithms/search/testNaiveSearch.js b/test/algorithms/search/testNaiveSearch.js new file mode 100644 index 00000000..801d487f --- /dev/null +++ b/test/algorithms/search/testNaiveSearch.js @@ -0,0 +1,23 @@ +/* eslint-env mocha */ +const naivesearch = require('../../../src').algorithms.search.naivesearch; +const assert = require('assert'); + +describe('Naive Search', () => { + it('should return frequency of the pattern', () => { + const freq = naivesearch("akgjfjhuyutomatokajkhgsvkjrtomato", "tomato"); + + assert.equal(freq, 1); + }); + + it('should return frequency of the pattern', () => { + const freq = naivesearch("treeseebeetea", "ee"); + + assert.equal(freq, 3); + }); + + it('should return frequency of the pattern', () => { + const freq = naivesearch("applebottomjeans", "boots"); + + assert.equal(freq, 0); + }); +}); From 9fddbcfca7d9ded40aff938544fce1a2a40399ef Mon Sep 17 00:00:00 2001 From: Francesca Calla Bruc Date: Sun, 22 Jan 2023 23:33:22 -0800 Subject: [PATCH 05/10] fixed for erros --- src/algorithms/math/is_prime.js | 16 ++++++++-------- src/algorithms/math/sum_all.js | 2 +- src/algorithms/search/naive_search.js | 21 ++++++++++----------- test/algorithms/math/testPrime.js | 2 +- test/algorithms/math/testSumAll.js | 8 ++++---- test/algorithms/search/testNaiveSearch.js | 6 +++--- 6 files changed, 27 insertions(+), 28 deletions(-) diff --git a/src/algorithms/math/is_prime.js b/src/algorithms/math/is_prime.js index da62d747..6ec45f1b 100644 --- a/src/algorithms/math/is_prime.js +++ b/src/algorithms/math/is_prime.js @@ -5,13 +5,13 @@ * * References: https://javascript.plainenglish.io/11-mathematical-algorithms-in-modern-javascript-bce71318e2da */ - const isprime = num => { +const isprime = num => { const limit = Math.floor(Math.sqrt(num)); - for (let i = 2; i <= limit; i++){ - if (n % i === 0) return false; + for (let i = 2; i <= limit; i++) { + if (num % i === 0) return false; } - return n >= 2; - }; - - module.exports = isprime; - \ No newline at end of file + return num >= 2; +}; + + +module.exports = isprime; diff --git a/src/algorithms/math/sum_all.js b/src/algorithms/math/sum_all.js index 43d94ba9..90fdd430 100644 --- a/src/algorithms/math/sum_all.js +++ b/src/algorithms/math/sum_all.js @@ -4,7 +4,7 @@ * @return {Number} sum sum of the range of numberse */ const sumall = (arr) => { - var sum =0; + var sum = 0; var min = arr[0]; var max = arr[1]; for (var i = min; i <= max; i++) { diff --git a/src/algorithms/search/naive_search.js b/src/algorithms/search/naive_search.js index d8790409..9b7a68b2 100644 --- a/src/algorithms/search/naive_search.js +++ b/src/algorithms/search/naive_search.js @@ -5,17 +5,16 @@ * @return {Number} count Frequency the pattern is in st */ -const naivesearch = (st,pattern) => - { - let count =0; - for(let i = 0; i < st.length; i++) { - for(let j = 0; j < pattern.length; j++) { - if(st[i + j] !== pattern[j]) break; - if(j === pattern.length - 1) count++; - } +const naivesearch = (st, pattern) => { + let count = 0; + for (let i = 0; i < st.length; i++) { + for (let j = 0; j < pattern.length; j++) { + if (st[i + j] !== pattern[j]) break; + if (j === pattern.length - 1) count = count + 1; } - - return count; } - module.exports = naivesearch; \ No newline at end of file + return count; +} + +module.exports = naivesearch; \ No newline at end of file diff --git a/test/algorithms/math/testPrime.js b/test/algorithms/math/testPrime.js index a2f2717a..8ab7c2fe 100644 --- a/test/algorithms/math/testPrime.js +++ b/test/algorithms/math/testPrime.js @@ -15,4 +15,4 @@ describe('isPrime', () => { assert.equal(isprime(36), false); assert.equal(isprime(100), false); }); -}); \ No newline at end of file +}); diff --git a/test/algorithms/math/testSumAll.js b/test/algorithms/math/testSumAll.js index ee049eca..46c666cc 100644 --- a/test/algorithms/math/testSumAll.js +++ b/test/algorithms/math/testSumAll.js @@ -5,10 +5,10 @@ const assert = require('assert'); describe('sumALL', () => { it('should return the sum of range', () => { - assert.equal(sumall(2,5), 14); - assert.equal(sumall(1,3), 6); - assert.equal(sumall(3,6), 18); + assert.equal(sumall(2, 5), 14); + assert.equal(sumall(1, 3), 6); + assert.equal(sumall(3, 6), 18); assert.equal(sumall(37), true); }); -}); \ No newline at end of file +}); diff --git a/test/algorithms/search/testNaiveSearch.js b/test/algorithms/search/testNaiveSearch.js index 801d487f..0950de2d 100644 --- a/test/algorithms/search/testNaiveSearch.js +++ b/test/algorithms/search/testNaiveSearch.js @@ -4,19 +4,19 @@ const assert = require('assert'); describe('Naive Search', () => { it('should return frequency of the pattern', () => { - const freq = naivesearch("akgjfjhuyutomatokajkhgsvkjrtomato", "tomato"); + const freq = naivesearch('akgjfjhuyutomatokajkhgsvkjrtomato', 'tomato'); assert.equal(freq, 1); }); it('should return frequency of the pattern', () => { - const freq = naivesearch("treeseebeetea", "ee"); + const freq = naivesearch('treeseebeetea', 'ee'); assert.equal(freq, 3); }); it('should return frequency of the pattern', () => { - const freq = naivesearch("applebottomjeans", "boots"); + const freq = naivesearch('applebottomjeans', 'boots'); assert.equal(freq, 0); }); From d5e49050212b1260d5911f73f54e9f6059bf4b1c Mon Sep 17 00:00:00 2001 From: Francesca Calla Bruc Date: Sun, 22 Jan 2023 23:38:12 -0800 Subject: [PATCH 06/10] fixed errors --- src/algorithms/search/naive_search.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/algorithms/search/naive_search.js b/src/algorithms/search/naive_search.js index 9b7a68b2..493fffd6 100644 --- a/src/algorithms/search/naive_search.js +++ b/src/algorithms/search/naive_search.js @@ -17,4 +17,4 @@ const naivesearch = (st, pattern) => { return count; } -module.exports = naivesearch; \ No newline at end of file +module.exports = naivesearch; From 00ceb5cd6c272c0d4e2b73eef2bfb0a07eabb4e7 Mon Sep 17 00:00:00 2001 From: Francesca Calla Bruc Date: Mon, 23 Jan 2023 00:37:00 -0800 Subject: [PATCH 07/10] fixed errors --- src/algorithms/math/is_prime.js | 12 ++++++------ src/algorithms/math/sum_all.js | 16 ++++++++-------- src/algorithms/search/naive_search.js | 8 ++++---- 3 files changed, 18 insertions(+), 18 deletions(-) diff --git a/src/algorithms/math/is_prime.js b/src/algorithms/math/is_prime.js index 6ec45f1b..0f291bac 100644 --- a/src/algorithms/math/is_prime.js +++ b/src/algorithms/math/is_prime.js @@ -5,12 +5,12 @@ * * References: https://javascript.plainenglish.io/11-mathematical-algorithms-in-modern-javascript-bce71318e2da */ -const isprime = num => { - const limit = Math.floor(Math.sqrt(num)); - for (let i = 2; i <= limit; i++) { - if (num % i === 0) return false; - } - return num >= 2; +const isprime = (num) => { + const limit = Math.floor(Math.sqrt(num)); + for (let i = 2; i <= limit; i++) { + if (num % i === 0) return false; + } + return num >= 2; }; diff --git a/src/algorithms/math/sum_all.js b/src/algorithms/math/sum_all.js index 90fdd430..872775e0 100644 --- a/src/algorithms/math/sum_all.js +++ b/src/algorithms/math/sum_all.js @@ -4,14 +4,14 @@ * @return {Number} sum sum of the range of numberse */ const sumall = (arr) => { - var sum = 0; - var min = arr[0]; - var max = arr[1]; - for (var i = min; i <= max; i++) { - sum += i; - } - - return sum; + var sum = 0; + var min = arr[0]; + var max = arr[1]; + var i =0; + for (i = min; i <= max; i = i + 1) { + sum = sum + i; + } + return sum; }; module.exports = sumall; diff --git a/src/algorithms/search/naive_search.js b/src/algorithms/search/naive_search.js index 493fffd6..8177a02f 100644 --- a/src/algorithms/search/naive_search.js +++ b/src/algorithms/search/naive_search.js @@ -7,14 +7,14 @@ const naivesearch = (st, pattern) => { let count = 0; - for (let i = 0; i < st.length; i++) { - for (let j = 0; j < pattern.length; j++) { + for (let i = 0; i < st.length; i = i + 1) { + for (let j = 0; j < pattern.length; j = j + 1) { if (st[i + j] !== pattern[j]) break; - if (j === pattern.length - 1) count = count + 1; + if (j === pattern.length - 1) count += 1; } } return count; -} +}; module.exports = naivesearch; From f9afe4b99e35177b6b9949b3a3d48b1310af9bc0 Mon Sep 17 00:00:00 2001 From: Francesca Calla Bruc Date: Mon, 23 Jan 2023 08:00:10 -0800 Subject: [PATCH 08/10] fix --- src/algorithms/math/is_prime.js | 4 ++-- src/algorithms/math/sum_all.js | 12 ++++++------ src/algorithms/search/naive_search.js | 4 ++-- test/algorithms/math/testSumAll.js | 1 - 4 files changed, 10 insertions(+), 11 deletions(-) diff --git a/src/algorithms/math/is_prime.js b/src/algorithms/math/is_prime.js index 0f291bac..a51523c3 100644 --- a/src/algorithms/math/is_prime.js +++ b/src/algorithms/math/is_prime.js @@ -7,8 +7,8 @@ */ const isprime = (num) => { const limit = Math.floor(Math.sqrt(num)); - for (let i = 2; i <= limit; i++) { - if (num % i === 0) return false; + for (let i = 2; i <= limit; i += 1) { + if (num % i === 0) return false; } return num >= 2; }; diff --git a/src/algorithms/math/sum_all.js b/src/algorithms/math/sum_all.js index 872775e0..8e45cff3 100644 --- a/src/algorithms/math/sum_all.js +++ b/src/algorithms/math/sum_all.js @@ -4,12 +4,12 @@ * @return {Number} sum sum of the range of numberse */ const sumall = (arr) => { - var sum = 0; - var min = arr[0]; - var max = arr[1]; - var i =0; - for (i = min; i <= max; i = i + 1) { - sum = sum + i; + let sum = 0; + let min = arr[0]; + let max = arr[1]; + let i = 0; + for (i = min; i <= max; i+=1) { + sum+=i; } return sum; }; diff --git a/src/algorithms/search/naive_search.js b/src/algorithms/search/naive_search.js index 8177a02f..b50cc416 100644 --- a/src/algorithms/search/naive_search.js +++ b/src/algorithms/search/naive_search.js @@ -7,8 +7,8 @@ const naivesearch = (st, pattern) => { let count = 0; - for (let i = 0; i < st.length; i = i + 1) { - for (let j = 0; j < pattern.length; j = j + 1) { + for (let i = 0; i < st.length; i += 1) { + for (let j = 0; j < pattern.length; j += 1) { if (st[i + j] !== pattern[j]) break; if (j === pattern.length - 1) count += 1; } diff --git a/test/algorithms/math/testSumAll.js b/test/algorithms/math/testSumAll.js index 46c666cc..ad7658df 100644 --- a/test/algorithms/math/testSumAll.js +++ b/test/algorithms/math/testSumAll.js @@ -10,5 +10,4 @@ describe('sumALL', () => { assert.equal(sumall(3, 6), 18); assert.equal(sumall(37), true); }); - }); From 64958585b482ab8d61dc43f1168d6791a37d3881 Mon Sep 17 00:00:00 2001 From: Francesca Calla Bruc Date: Mon, 23 Jan 2023 09:15:44 -0800 Subject: [PATCH 09/10] fix --- src/algorithms/math/sum_all.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/algorithms/math/sum_all.js b/src/algorithms/math/sum_all.js index 8e45cff3..80d836dd 100644 --- a/src/algorithms/math/sum_all.js +++ b/src/algorithms/math/sum_all.js @@ -5,11 +5,11 @@ */ const sumall = (arr) => { let sum = 0; - let min = arr[0]; - let max = arr[1]; + const min = arr[0]; + const max = arr[1]; let i = 0; - for (i = min; i <= max; i+=1) { - sum+=i; + for (i = min; i <= max; i += 1) { + sum += i; } return sum; }; From 26fa6bacc29c0765aacedabca72024cab3fadeae Mon Sep 17 00:00:00 2001 From: Francesca Calla Bruc Date: Mon, 23 Jan 2023 15:54:10 -0800 Subject: [PATCH 10/10] fixed tests --- test/algorithms/math/testPrime.js | 4 ++-- test/algorithms/math/testSumAll.js | 1 - test/algorithms/search/testNaiveSearch.js | 2 +- 3 files changed, 3 insertions(+), 4 deletions(-) diff --git a/test/algorithms/math/testPrime.js b/test/algorithms/math/testPrime.js index 8ab7c2fe..c8f76e67 100644 --- a/test/algorithms/math/testPrime.js +++ b/test/algorithms/math/testPrime.js @@ -6,9 +6,9 @@ const assert = require('assert'); describe('isPrime', () => { it('should return true if number is prime', () => { assert.equal(isprime(2), true); - assert.equal(isprime(33), true); + assert.equal(isprime(3), true); assert.equal(isprime(7), true); - assert.equal(isprime(37), true); + assert.equal(isprime(5), true); }); it('should return false if number is prime', () => { assert.equal(isprime(16), false); diff --git a/test/algorithms/math/testSumAll.js b/test/algorithms/math/testSumAll.js index ad7658df..729e1018 100644 --- a/test/algorithms/math/testSumAll.js +++ b/test/algorithms/math/testSumAll.js @@ -8,6 +8,5 @@ describe('sumALL', () => { assert.equal(sumall(2, 5), 14); assert.equal(sumall(1, 3), 6); assert.equal(sumall(3, 6), 18); - assert.equal(sumall(37), true); }); }); diff --git a/test/algorithms/search/testNaiveSearch.js b/test/algorithms/search/testNaiveSearch.js index 0950de2d..52d7b40a 100644 --- a/test/algorithms/search/testNaiveSearch.js +++ b/test/algorithms/search/testNaiveSearch.js @@ -6,7 +6,7 @@ describe('Naive Search', () => { it('should return frequency of the pattern', () => { const freq = naivesearch('akgjfjhuyutomatokajkhgsvkjrtomato', 'tomato'); - assert.equal(freq, 1); + assert.equal(freq, 2); }); it('should return frequency of the pattern', () => {