Skip to content

Adding an algorithm AlphaNumericPalindrome from freecodecamp #769

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
merged 7 commits into from
Oct 20, 2021
Merged

Adding an algorithm AlphaNumericPalindrome from freecodecamp #769

merged 7 commits into from
Oct 20, 2021

Conversation

SyedFasiuddin
Copy link

@SyedFasiuddin SyedFasiuddin commented Oct 10, 2021

Welcome to JavaScript community

Open in Gitpod know more

Describe your change:

  • Add an algorithm?
  • Fix a bug or typo in an existing algorithm?
  • Documentation change?

Checklist:

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized.
  • I know that pull requests will not be merged if they fail the automated tests.
  • This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
  • All new JavaScript files are placed inside an existing directory.
  • All filenames should use the UpperCamelCase (PascalCase) style. There should be no spaces in filenames.
    Example:UserProfile.js is allowed but userprofile.js,Userprofile.js,user-Profile.js,userProfile.js are not
  • All new algorithms have a URL in its comments that points to Wikipedia or other similar explanation.
  • If this pull request resolves one or more open issues then the commit message contains Fixes: #{$ISSUE_NO}.

Copy link
Author

@SyedFasiuddin SyedFasiuddin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I have done all my checks of naming convention and ES6 syntax among others.

@raklaptudirm raklaptudirm added the on hold Being discussed by the maintainers label Oct 10, 2021
@SyedFasiuddin
Copy link
Author

can I get a review and workflow approval please

@raklaptudirm
Copy link
Member

@cclauss Would you consider this as a proper algorithm?

@cclauss
Copy link
Member

cclauss commented Oct 16, 2021

I don’t do JavaScript so I cannot comment on efficiency but I would have written this as return isPalandrome(s) and isAlphaNumeric(s). In Python, each of those functions would be one-liners.

It does however seem to fit the definition of an algorithm unless I am missing something.

@raklaptudirm raklaptudirm added algorithm Adds or improves an algorithm changes required This pull request needs changes feature Adds a new feature Reviewed and removed on hold Being discussed by the maintainers labels Oct 16, 2021
@SyedFasiuddin
Copy link
Author

I don’t do JavaScript so I cannot comment on efficiency but I would have written this as return isPalandrome(s) and isAlphaNumeric(s). In Python, each of those functions would be one-liners.

It does however seem to fit the definition of an algorithm unless I am missing something.

Yes this can be done in two line in JS as well but, this repository being beginner friendly, I wanted to make it as simple as possible, and if you not include the comments and empty lines this function only takes 8 lines

@SyedFasiuddin
Copy link
Author

This error that is popping up saying "unnecessary escape character" in file "Javascript\String\test\AlphaNumericPalindrome.test.js" at lines 8 and 12 is actually inside a string and is a test case requirement, so I don't think this error is any significant in this case.

@cclauss
Copy link
Member

cclauss commented Oct 16, 2021

A Python version with tests included…

from string import ascii_letters, digits


def is_alphanumeric_palendrome(s: str) -> bool:
    """
    To test, run: `python3 -m doctest v alphanumeric_palendrome.py`
    >>> is_alphanumeric_palendrome("eye")
    True
    >>> is_alphanumeric_palendrome("0_0 (: /-\ :) 0-0")
    True
    >>> is_alphanumeric_palendrome("five|\_/|four")
    False
    >>> is_alphanumeric_palendrome("A man, a plan, a canal. Panama")
    True
    >>> is_alphanumeric_palendrome(" eye for of 1 eye.")
    False
    """
    s = "".join(char for char in s if char in ascii_letters + digits).lower()
    return s == "".join(reversed(s))

@cclauss
Copy link
Member

cclauss commented Oct 16, 2021

this can be done in two line in JS

For my edification, can you please show me here?

@SyedFasiuddin
Copy link
Author

SyedFasiuddin commented Oct 16, 2021

For my edification, can you please show me here?

const alphaNumericPlaindrome = (str) => {
    const string = str.replace(/[^a-zA-Z0-9]*/g, '').toLowerCase()
    return string === string.split('').reverse().join('')
}

Here you go, this will work exactly as your Python code but there is nothing like doctest in js so...

@cclauss
Copy link
Member

cclauss commented Oct 16, 2021

@raklaptudirm
Copy link
Member

We actually removed all the doctests and migrated all of them to jest. Since it is a proper algorithm, I am marking it as valid.

Copy link
Member

@raklaptudirm raklaptudirm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please format your code using standard.js.

@raklaptudirm raklaptudirm added code style issue Failing style checks and removed changes required This pull request needs changes labels Oct 20, 2021
Copy link
Author

@SyedFasiuddin SyedFasiuddin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed it

@cclauss cclauss merged commit f4590b7 into TheAlgorithms:master Oct 20, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
algorithm Adds or improves an algorithm code style issue Failing style checks feature Adds a new feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants