-
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
Upgraded Algorithm of alpha numeric palindrome #1006
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
Merged
raklaptudirm
merged 52 commits into
TheAlgorithms:master
from
fahimfaisaal:upgrade-alphaNumericPalindrome
May 7, 2022
Merged
Changes from all commits
Commits
Show all changes
52 commits
Select commit
Hold shift + click to select a range
72def41
feat: improved memoize function
fahimfaisaal 8548ef1
docs: modified documentation
fahimfaisaal ce9d3b9
style: format with standard
fahimfaisaal 1e2fb53
docs: modified stringify doc
fahimfaisaal 2a5c28b
refactor: remove two repetition implementation
fahimfaisaal c582f58
feat: added validation, test codes
fahimfaisaal cdff0e3
chore: remove useless words
fahimfaisaal 5727b64
feat: added types for jest
fahimfaisaal 025a8b8
Merge branch 'upgrade-abs' of https://github.com/fahimfaisaal/Javascr…
fahimfaisaal 9efda24
chore: added link box
fahimfaisaal 98ffd1d
feat: added new validation test casses & methods
fahimfaisaal b8d6e68
style: formated with standard
fahimfaisaal b7c961c
feat: added parse method & test cases
fahimfaisaal 6b7b715
docs: added js docs
fahimfaisaal 269a3f4
chore: added default import export
fahimfaisaal 5cedaa9
feat: imporved algorithm via replace method
fahimfaisaal d00a7bf
test: added two test cases
fahimfaisaal bbcce39
feat: added jest type for suggestions
fahimfaisaal 9be30a8
feat: added `reduceRight` & `trim` method
fahimfaisaal 489544d
chore: added helper variable
fahimfaisaal 68cb5ad
feat: added new rotation option
fahimfaisaal addf721
Revert "chore: added helper variable"
fahimfaisaal 07a2b17
Merge branch 'TheAlgorithms:master' into master
fahimfaisaal 8c297ae
Merge branch 'upgrade-CaesarsCipher'
fahimfaisaal 605cfb7
Merge branch 'upgrade-reverseWord'
fahimfaisaal 6636af2
Merge branch 'upgrade-packageJson'
fahimfaisaal 3905c5d
Merge branch 'upgrade-LRU'
fahimfaisaal 42b13b8
Merge branch 'upgrade-ciphers'
fahimfaisaal 64981b7
Merge branch 'upgrade-memoize'
fahimfaisaal e1c6666
Merge branch 'upgrade-abs'
fahimfaisaal e783547
remove: yarn lock
fahimfaisaal 399e353
chore: fix grammer
fahimfaisaal 189e492
Merge branch 'master' of github.com:fahimfaisaal/Javascript
fahimfaisaal 4887aff
feat: used replace method & added test case
fahimfaisaal a946220
Merge branch 'TheAlgorithms:master' into master
fahimfaisaal 48329bf
feat: remove revert
fahimfaisaal 08955a7
chore: added new line
fahimfaisaal a613aee
Merge branch 'TheAlgorithms:master' into master
fahimfaisaal 05ce4b8
Merge branch 'upgrade-XORCipher'
fahimfaisaal 91f6c78
Merge branch 'TheAlgorithms:master' into master
fahimfaisaal 9151b89
Merge branch 'TheAlgorithms:master' into master
fahimfaisaal b30f149
Merge branch 'TheAlgorithms:master' into master
fahimfaisaal 0b54cb3
Merge branch 'TheAlgorithms:master' into master
fahimfaisaal 4c5c722
feat: optimized algo n to n / 2 & replaced test cases
fahimfaisaal 63e4104
chore: update node version
raklaptudirm c743dbe
chore: set node version to lts
raklaptudirm 36bb6a2
chore: updated the node version & added engines prop
fahimfaisaal 5958e31
Merge branches 'upgrade-alphaNumericPalindrome' and 'upgrade-alphaNum…
fahimfaisaal af6133c
resolve: removed while loop
fahimfaisaal d6c9dc6
chore: added right shift ops comment
fahimfaisaal 8c54c74
chore: update comment
fahimfaisaal 30d8b04
chore: removed abs
fahimfaisaal 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 |
---|---|---|
@@ -1,32 +1,35 @@ | ||
/***************************************************************************** | ||
* @function alphaNumericPlaindrome | ||
* @description alphaNumericPlaindrome should return true if the string has alphanumeric characters that are palindrome irrespective of special characters and the letter case. | ||
/** | ||
* @function alphaNumericPalindrome | ||
* @description alphaNumericPalindrome should return true if the string has alphanumeric characters that are palindrome irrespective of special characters and the letter case. | ||
* @param {string} str the string to check | ||
* @returns {Boolean} | ||
* @see [Factorial](https://en.wikipedia.org/wiki/Palindrome) | ||
* @returns {boolean} | ||
* @see [Palindrome](https://en.wikipedia.org/wiki/Palindrome) | ||
* @example | ||
* The function alphaNumericPlaindrome() receives a string with varying formats | ||
* The function alphaNumericPalindrome() receives a string with varying formats | ||
* like "racecar", "RaceCar", and "race CAR" | ||
* The string can also have special characters | ||
* like "2A3*3a2", "2A3 3a2", and "2_A3*3#A2" | ||
* | ||
* But the catch is, we have to check only if the alphanumeric characters | ||
* are palindrome i.e remove spaces, symbols, punctuations etc | ||
* and the case of the characters doesn't matter | ||
* | ||
****************************************************************************/ | ||
*/ | ||
const alphaNumericPalindrome = (str) => { | ||
if (typeof str !== 'string') { | ||
throw new TypeError('Argument should be string') | ||
} | ||
|
||
const alphaNumericPlaindrome = (str) => { | ||
// removing all the special characters and turning everything to lowercase | ||
const newStr = str.replace(/[^a-zA-Z0-9]*/g, '').toLowerCase() | ||
const newStr = str.replace(/[^a-z0-9]+/ig, '').toLowerCase() | ||
const midIndex = newStr.length >> 1 // x >> y = floor(x / 2^y) | ||
|
||
for (let i = 0; i < newStr.length; i++) { | ||
if (newStr[i] !== newStr[newStr.length - 1 - i]) { | ||
for (let i = 0; i < midIndex; i++) { | ||
if (newStr.at(i) !== newStr.at(~i)) { // ~n = -(n + 1) | ||
return false | ||
} | ||
} | ||
|
||
return true | ||
} | ||
|
||
export { alphaNumericPlaindrome } | ||
export default alphaNumericPalindrome |
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 |
---|---|---|
@@ -1,21 +1,21 @@ | ||
import { alphaNumericPlaindrome } from '../AlphaNumericPalindrome' | ||
import alphaNumericPalindrome from '../AlphaNumericPalindrome' | ||
|
||
test('should return true if the given string has alphanumeric characters that are palindrom irrespective of case and symbols', () => { | ||
expect(alphaNumericPlaindrome('eye')).toBe(true) | ||
}) | ||
|
||
test('should return true if the given string has alphanumeric characters that are palindrom irrespective of case and symbols', () => { | ||
expect(alphaNumericPlaindrome('0_0 (: /-:) 0-0')).toBe(true) | ||
}) | ||
describe('Testing the alpha numeric palindrome', () => { | ||
// should return true if the given string has alphanumeric characters that are palindrome irrespective of case and symbols | ||
it('Testing with valid alphabetic palindrome', () => { | ||
expect(alphaNumericPalindrome('eye')).toBe(true) | ||
expect(alphaNumericPalindrome('Madam')).toBe(true) | ||
expect(alphaNumericPalindrome('race CAR')).toBe(true) | ||
expect(alphaNumericPalindrome('A man, a plan, a canal. Panama')).toBe(true) | ||
}) | ||
|
||
test('should return true if the given string has alphanumeric characters that are palindrom irrespective of case and symbols', () => { | ||
expect(alphaNumericPlaindrome('five|_/|four')).toBe(false) | ||
}) | ||
|
||
test('should return true if the given string has alphanumeric characters that are palindrom irrespective of case and symbols', () => { | ||
expect(alphaNumericPlaindrome('A man, a plan, a canal. Panama')).toBe(true) | ||
}) | ||
it('Testing with number and symbol', () => { | ||
expect(alphaNumericPalindrome('0_0 (: /-:) 0-0')).toBe(true) | ||
expect(alphaNumericPalindrome('03_|53411435|_30')).toBe(true) | ||
}) | ||
|
||
test('should return true if the given string has alphanumeric characters that are palindrom irrespective of case and symbols', () => { | ||
expect(alphaNumericPlaindrome('1 eye for of 1 eye.')).toBe(false) | ||
it('Testing with alphabets and symbols', () => { | ||
expect(alphaNumericPalindrome('five|_/|evif')).toBe(true) | ||
expect(alphaNumericPalindrome('five|_/|four')).toBe(false) | ||
}) | ||
}) |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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.
Uh oh!
There was an error while loading. Please reload this page.