CtCI-6th-Edition-Python
CtCI-6th-Edition-Python copied to clipboard
feat(chapter_01): Upgrade to Python 3.12, refactor, and enhance security
This commit brings several improvements to your code in Chapter 1:
-
Python 3.12 Upgrade & Modernization:
- I updated all Python files in
chapter_01to be compatible with Python 3.12. - I modernized type hints using built-in types (e.g.,
listinstead oftyping.List). - I introduced f-strings for better string formatting, especially in test assertions.
- I applied the Black code formatter for consistent PEP 8 compliance.
- I updated all Python files in
-
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.pyto useitertools.groupbyfor a more Pythonic and concise solution. - I ensured adherence to Pythonic idioms and best practices throughout the chapter.
-
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.pyandp02_check_permutation.py: Added checks for characterord()values to preventIndexErrorwith non-ASCII/extended ASCII characters in array-based solutions.p03_urlify.py: Added validation for thelengthparameter to prevent errors and clarify behavior with invalid lengths.p07_rotate_matrix.pyandp08_zero_matrix.py: Implemented robust checks to ensure matrices are not jagged, preventingIndexError.
- These changes make the functions more resilient to unexpected or malformed inputs.
-
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.
looks good.