CtCI-6th-Edition-Python icon indicating copy to clipboard operation
CtCI-6th-Edition-Python copied to clipboard

feat(chapter_01): Upgrade to Python 3.12, refactor, and enhance security

Open awake416 opened this issue 6 months ago • 1 comments

This commit brings several improvements to your code in Chapter 1:

  1. Python 3.12 Upgrade & Modernization:

    • I updated all Python files in chapter_01 to be compatible with Python 3.12.
    • I modernized type hints using built-in types (e.g., list instead of typing.List).
    • I introduced f-strings for better string formatting, especially in test assertions.
    • I applied the Black code formatter for consistent PEP 8 compliance.
  2. Refactoring for Clarity & Pythonic Practices:

    • I improved variable names for better descriptiveness.
    • I significantly enhanced function and class docstrings, including algorithm explanations, assumptions, and time/space complexity analyses.
    • I refactored p06_string_compression.py to use itertools.groupby for a more Pythonic and concise solution.
    • I ensured adherence to Pythonic idioms and best practices throughout the chapter.
  3. Security Enhancements:

    • I performed a security analysis focusing on input validation and edge case handling.
    • I added stricter input validation in several files:
      • p01_is_unique.py and p02_check_permutation.py: Added checks for character ord() values to prevent IndexError with non-ASCII/extended ASCII characters in array-based solutions.
      • p03_urlify.py: Added validation for the length parameter to prevent errors and clarify behavior with invalid lengths.
      • p07_rotate_matrix.py and p08_zero_matrix.py: Implemented robust checks to ensure matrices are not jagged, preventing IndexError.
    • These changes make the functions more resilient to unexpected or malformed inputs.
  4. Test Updates:

    • I reviewed and updated existing tests to align with code modifications.
    • I added new test cases to specifically cover the new input validation logic and edge cases introduced during the security hardening. This includes tests for non-ASCII inputs, invalid matrix structures, and out-of-bounds parameters.
    • I expanded test coverage for Unicode characters in several problem solutions to ensure robust handling.

All changes have been tested, and existing functionality is preserved or correctly modified as per the enhancements.

awake416 avatar Jun 03 '25 10:06 awake416

looks good.

awake416 avatar Jun 03 '25 11:06 awake416