Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork5.7k
New Algorithm: Parity Outlier#1314
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
Show all changes
10 commits Select commitHold shift + click to select a range
fb9154d
[feat] New algorithm
mrmagic2020791df98
[test] Add new test for ParityOutlier.js
mrmagic2020c7eeff2
[fix] Reset indentation
mrmagic20202d342dc
[fix] Reset indentation
mrmagic2020568763d
[fix] Style changes
mrmagic20206df2a72
fix: improve code efficiency and a glitch
mrmagic20208202eb1
test: adds a new possible test case
mrmagic20202dfc2c0
fix: style fix
mrmagic2020fd4bda4
fix: delete redundant comments and else statements
mrmagic20204a4d7e3
[fix] style fix
mrmagic2020File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/** | ||
* @author mrmagic2020 | ||
* @description The function will find the parity outlier from an array of integers. | ||
* @see https://en.wikipedia.org/wiki/Parity_(mathematics) | ||
* @param {number[]} integers - An array of integers. | ||
* @returns {number} - The parity outlier. | ||
* @example parityOutlier([1, 3, 5, 8, 9]) = 8 | ||
*/ | ||
const parityOutlier = (integers) => { | ||
let oddsCount = 0 // define counter for odd number(s) | ||
let evensCount = 0 // define counter for even number(s) | ||
let odd, even | ||
for (const e of integers) { | ||
if (!Number.isInteger(e)) { // detect non-integer elements | ||
return null | ||
} | ||
if (e % 2 === 0) { // an even number | ||
even = e | ||
evensCount++ | ||
} else { // an odd number | ||
odd = e | ||
oddsCount++ | ||
} | ||
} | ||
if (oddsCount === 0 || evensCount === 0) return null // array has only odd/even number(s) | ||
if (oddsCount > 1 && evensCount > 1) return null // array has more than one even and odd number | ||
return oddsCount === 1 ? odd : even | ||
} | ||
export { parityOutlier } |
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,30 @@ | ||
import { parityOutlier } from '../ParityOutlier' | ||
describe('Testing parityOutlier function', () => { | ||
it('should return the odd number in an array of even numbers', () => { | ||
expect(parityOutlier([1, 2, 16, -8848, 5126])).toBe(1) | ||
}) | ||
it('should return the even number in an array of odd numbers', () => { | ||
expect(parityOutlier([177, 5, 76, 1919])).toBe(76) | ||
}) | ||
it('should, if the given array has only one integer element, return the integer itself', () => { | ||
expect(parityOutlier([83])).toBe(83) | ||
expect(parityOutlier([54])).toBe(54) | ||
}) | ||
it('should, if the given array has only an odd and an even number, return the odd outlier', () => { | ||
expect(parityOutlier([1, 2])).toBe(1) | ||
expect(parityOutlier([4, 3])).toBe(3) | ||
}) | ||
it('should return null if the given array is empty, contains only one integer, contains non-interger elements or does not have a parity outlier', () => { | ||
expect(parityOutlier([])).toBe(null) | ||
expect(parityOutlier([2])).toBe(null) | ||
expect(parityOutlier([2, {}, 5, 'GitHub'])).toBe(null) | ||
expect(parityOutlier([1, 3, 5, 7, 9])).toBe(null) | ||
expect(parityOutlier([0, 2, 4, 6, 8])).toBe(null) | ||
expect(parityOutlier([1, 3, 5, 7, 2, 4, 6, 8])).toBe(null) | ||
}) | ||
}) |
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.