-
Notifications
You must be signed in to change notification settings - Fork 129
Added Functions: isPrime, sumAll, Naive Search #141
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
chs-mix
wants to merge
17
commits into
manrajgrover:master
Choose a base branch
from
chs-mix:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
cc2bc4e
added naive search
chs-mix 51d06e1
added is_prime function
chs-mix ae6c58d
added basic functions
chs-mix 00693f6
added tests to added functoins
chs-mix acbce26
Merge pull request #1 from chs-mix/francesca-branch
chs-mix 9fddbcf
fixed for erros
chs-mix 65d2ccb
Merge pull request #2 from chs-mix/francesca-branch
chs-mix d5e4905
fixed errors
chs-mix 0483d23
Merge pull request #3 from chs-mix/francesca-branch
chs-mix 00ceb5c
fixed errors
chs-mix aa6c763
Merge pull request #4 from chs-mix/francesca-branch
chs-mix f9afe4b
fix
chs-mix ad62a6f
Merge pull request #5 from chs-mix/francesca-branch
chs-mix 6495858
fix
chs-mix 77ebd13
Merge pull request #6 from chs-mix/francesca-branch
chs-mix 26fa6ba
fixed tests
chs-mix a687639
Merge pull request #7 from chs-mix/francesca-branch
chs-mix File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 += 1) { | ||
if (num % i === 0) return false; | ||
} | ||
return num >= 2; | ||
}; | ||
|
||
|
||
module.exports = isprime; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
*/ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. here the wanted function and you implement is different |
||
const sumall = (arr) => { | ||
let sum = 0; | ||
const min = arr[0]; | ||
const max = arr[1]; | ||
let i = 0; | ||
for (i = min; i <= max; i += 1) { | ||
sum += i; | ||
} | ||
return sum; | ||
}; | ||
|
||
module.exports = sumall; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/** | ||
* 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 += 1) { | ||
for (let j = 0; j < pattern.length; j += 1) { | ||
if (st[i + j] !== pattern[j]) break; | ||
if (j === pattern.length - 1) count += 1; | ||
} | ||
} | ||
|
||
return count; | ||
}; | ||
|
||
module.exports = naivesearch; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/* eslint-env mocha */ | ||
const isprime = require('../../../src').algorithms.math.isprime; | ||
|
||
const assert = require('assert'); | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. put testing for negative number and float |
||
describe('isPrime', () => { | ||
it('should return true if number is prime', () => { | ||
assert.equal(isprime(2), true); | ||
assert.equal(isprime(3), true); | ||
assert.equal(isprime(7), true); | ||
assert.equal(isprime(5), true); | ||
}); | ||
it('should return false if number is prime', () => { | ||
assert.equal(isprime(16), false); | ||
assert.equal(isprime(36), false); | ||
assert.equal(isprime(100), false); | ||
}); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
/* 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); | ||
}); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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, 2); | ||
}); | ||
|
||
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); | ||
}); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
before this you have to verify if the number are greater or equal to 1 or if is a integer
if (num <= 1 || !Number.isInteger(num)) {
return false;
}