MARISSA MOLEJON | OCT2025-1 | Module-Complexity | Sprint 1 | Refactor/python complexity analysis - #3
Open
marissamolejon wants to merge 4 commits into
Open
Conversation
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Analyzes the time and space complexity of all four Sprint 1 Python
functions, documents the findings in docstrings, and refactors the
functions where the complexity could be improved. Companion PR to the
JavaScript version of this work.
Changes made
calculate_sum_and_productsumtototalto avoid shadowing Python's built-insum(), merged two sequential loops into one pass.find_common_itemsincheck against a growing list)sets: one built from the second sequence for O(1) lookup, one for de-duplicating the result.has_pair_with_sumsetto check each number's complement (target_sum - number).remove_duplicatessetfor O(1) membership checks, preserving order of first occurrence.All four functions now have complete docstrings documenting Time
Complexity, Space Complexity, and Optimal Time Complexity.
Testing
All existing tests pass unchanged for every function - no test files
were modified.
Ran locally via
python3 -m pytest -vin each function's folder — allgreen (2, 3, 3, and 4 tests respectively).
Learning points
find_common_itemswas the standout case: a nested loop (O(n·m)) plusan
i not in common_itemscheck on a growinglist(O(k) per check)compounded into roughly O(n·m·k) — worse than a "plain" nested loop,
and not obvious without tracing through what
incosts on alistversus a
set.calculate_sum_and_product)is itself a valid refactor — it doesn't change complexity, but it
removes a second source of truth that could drift out of sync with the
real logic.
across
has_pair_with_sumandremove_duplicates, reinforcing that thealgorithmic idea (O(n²) -> O(n) via a hash-based lookup) is
language-independent, even though the syntax isn't.
Checklist