Skip to content
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

Solution for mirror words challenge #38

Merged
merged 33 commits into from
Jan 11, 2025

Conversation

Aseel-AbuKmail
Copy link

@Aseel-AbuKmail Aseel-AbuKmail commented Jan 5, 2025

Name: Mirror Words Challenge
About: A template PR for code review with a checklist

Behavior

Files

  • The file name describes the function's behavior
  • There is a module docstring in the function file
  • The test file's name matches the function file name -
    /tests/test_file_name.py
  • There is a module docstring in the tests file

Unit Tests

  • The test class has a helpful name in PascalCase
  • The test class has a docstring
  • Every unit test has
    • A helpful name
    • A clear docstring
    • Only one assertion
    • There is no logic in the unit test
  • All tests pass
  • There are tests for defensive assertions
  • There are tests for boundary cases

Function Docstring

  • The function's behavior is described
  • The function's arguments are described:
    • Type
    • Purpose
    • Other assumptions (eg. if it's a number, what's the expected range?)
  • The return value is described
    • Type
    • Other assumptions are documented
  • The defensive assertions are documented using Raises:
    • Each assumption about an argument is checked with an assertion
    • Each assertion checks for only one assumption about the argument
  • Include 3 or more (passing!) doctests

The Function

  • The function's name describes it's behavior
  • The function's name matches the file name
  • The function has correct type annotations
  • The function is not called in the function file

Strategy

Do's

  • Variable names help to understand the strategy
  • Any comments are clear and describe the strategy
  • Lines of code are spaced to help show different stages of the strategy

Don'ts

  • The function's strategy is not described in the documentation
  • Comments explain the strategy, not the implementation
  • The function does not have more comments than code
    • If it does, consider finding a new strategy or a simpler implementation

Implementation

  • The code passes the formatting checks
  • The code passes all Ruff linting checks
  • The code has no (reasonable) Pylint errors
    • In code review, you can decide when fixing a Pylint error is helpful and
      when it's too restricting.
  • Variables are named with snake_case
  • Variable names are clear and helpful
  • The code follows the strategy as simply as possible
  • The implementation is as simple as possible given the strategy
  • There are no commented lines of code
  • There are no print statements anywhere
  • The code includes defensive assertions
  • Defensive assertions include as little logic as possible

Create Solution for mirror words challenge
@Aseel-AbuKmail Aseel-AbuKmail self-assigned this Jan 5, 2025
@Aseel-AbuKmail Aseel-AbuKmail added the challenge a new challenge label Jan 5, 2025
@Aseel-AbuKmail Aseel-AbuKmail linked an issue Jan 5, 2025 that may be closed by this pull request
@abdoalsir
Copy link

Hi Aseel,
I hope you are having a great day.
I see that your solutions didn't pass some checks, if you need assistance feel free to reach out

@Aseel-AbuKmail
Copy link
Author

Hi Aseel, I hope you are having a great day. I see that your solutions didn't pass some checks, if you need assistance feel free to reach out

Hello, Abdulrahman!
Thank you for offering to help; I really appreciate it!

Copy link

@abdoalsir abdoalsir left a comment

Choose a reason for hiding this comment

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

Overall Feedback:

The function and test files are well-constructed and fulfill many criteria for clear and maintainable code. You have effectively utilized docstrings and tests, and the function successfully passes all provided checks. Nevertheless, there are several areas where enhancements could be made to increase the robustness and user-friendliness of the code:

  • Function Naming: The name of the function could be more descriptive to eliminate any ambiguity regarding its purpose. Aligning it with the file name (e.g., mirror_words) may clarify its functionality.
  • Type Annotations: Incorporating type annotations for both the input argument and the return value would enhance clarity for readers who may not refer to the docstring.
  • Docstring Enhancements: Although your docstring is clear, including information about the treatment of special characters and numbers (as indicated in your tests) along with a high-level overview of the strategy would improve its thoroughness and usefulness for others who may review or extend your code.
  • Doctests: Adding a third doctest that illustrates the handling of special characters would further enrich the documentation of the function.
  • Code Structure: Enhancing the spacing and organization of the code will contribute to making the implementation more readable and accessible to a wider audience.
  • Commented Lines: If the commented-out import statements are unnecessary, consider removing them. If they serve a purpose, please add a clarifying comment to explain their intent.

Overall, excellent work!😇 Implementing these recommendations will raise the quality of your code from great to outstanding.


Returns:
str: A new string where each word is reversed, but the word order and punctuation
remain unchanged.

Choose a reason for hiding this comment

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

Your docstring very clear and easy to read, I have a small comment here on the return value of the function. You mentioned the type of the return value and you mentioned punctuation. You can also include that numbers and special characters remain unreversed as you mentioned in the test file. This will add more clarity for some just checking the function file.

Copy link
Author

Choose a reason for hiding this comment

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

Thank you for pointing that out! I'll update the return value description to include that numbers and special characters remain unreversed for added clarity.


>>> reverse_words("Python is fun")
'nohtyP si nuf'

Choose a reason for hiding this comment

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

The doctests are very clear and shows exactly what the output will be. Consider adding a 3rd doctest and maybe include special characters to show they remain unreversed.

Copy link
Author

Choose a reason for hiding this comment

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

Thank you for the feedback! I'll add a third doctest with special characters to demonstrate how they remain unreversed.



def reverse_words(sentence):
"""

Choose a reason for hiding this comment

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

  • The function name describes what the function do, but it can be a little confusing since someone might understand it reverses the entire sentence not just the words. I believe it would be better to use the same file name mirror_words. It gives a better description of the functions behavior.
  • Moreover, make sure to include the type annotation, you mentioned in the function docstring the argument and the return value of the function, it would be more clear for someone who just reads the functions name to understand the argument and return value types. The function name would look a lot better like this:
def mirror_words(sentence: str) -> str

Copy link
Author

Choose a reason for hiding this comment

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

Thank you for the feedback! I will rename the function to mirror_words for clarity and update the function signature to include type annotations as suggested.

if not isinstance(sentence, str):
raise TypeError("Input must be a string")

words = sentence.split()

Choose a reason for hiding this comment

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

Your code is readable, but there is room for improvement in spacing and structure to make it clearer and more organized, so it makes it easier for anyone even those with no experience can read it and understand every stage of the functionality.

Copy link
Author

Choose a reason for hiding this comment

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

Thank you for the suggestion! I will adjust the spacing and structure to make the code more organized and readable for everyone.

sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "../../")))

# from mirror_words_challenge import reverse_words
# from solutions.mirror_words_challenge import reverse_words

Choose a reason for hiding this comment

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

If these lines are unnecessary, delete them. However, if they are intentionally retained for reference or debugging purposes, add a clarifying comment explaining why they are there.

Copy link
Author

Choose a reason for hiding this comment

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

When I push the code, I face an issue where the mirror_words_challenge file cannot be found in the solutions directory. Thank you for the feedback! I'll review these lines to determine if they are necessary. If they are not, I will remove them. If they are intentional for debugging or reference, I will add a comment to clarify their purpose.

"""
Reverses each word in a given sentence while maintaining the order of the words
and correctly handling punctuation marks at the end of words.

Choose a reason for hiding this comment

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

The docstring is well written and it can be even better with a strategy to make the code even easier to understand for others who might work with or review it in the future.

Copy link
Author

Choose a reason for hiding this comment

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

Thank you for the feedback! I'll enhance the docstring by including an example and a brief note on the function's behavior to make it even clearer for others.

@@ -0,0 +1,86 @@
# test_mirror_words_challenge

Choose a reason for hiding this comment

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

The file name already shows what file it's, so I think it would be better to remove this comment so the code appears more clean and professional.

Copy link
Author

Choose a reason for hiding this comment

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

Got it, I will delete the comment as suggested. Thank you!

@@ -0,0 +1,44 @@
# mirror_words_challenge

Choose a reason for hiding this comment

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

The file name already shows what file it's, so I think it would be better to remove this comment so the code appears more clean and professional

Copy link
Author

Choose a reason for hiding this comment

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

Ok, I will remove the comment as you suggested. Thank you for the feedback!

@Aseel-AbuKmail
Copy link
Author

@abdoalsir
Hi, I’ve implemented your suggested feedback. If you don’t have any further suggestions, please go ahead and merge it into the main branch and delete my challenge branch.

@Aseel-AbuKmail
Copy link
Author

Aseel-AbuKmail commented Jan 10, 2025

Lessons Learned from My Mistakes

How to Resolve ET CI Check Issues:

1. For py_formatting:
Run the following commands in the terminal:

  • pip install ruff
  • ruff format
  • ruff check

OR:

Run the following commands in the terminal:

  • pip install black
  • black --check <file_location>
  • black <file_location>

2. For py_tests

  • Verify that every file is in the correct path.

@abdoalsir abdoalsir merged commit bdb706f into main Jan 11, 2025
10 checks passed
@abdoalsir abdoalsir deleted the Challenge/1-Mirror_Words-Aseel-AbuKmail-branch branch January 11, 2025 18:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
challenge a new challenge
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Mirror Words Challenge
2 participants