Skip to content

MARISSA MOLEJON | OCT2025-1 | Module-Complexity | Sprint 1 | Refactor/python complexity analysis - #3

Open
marissamolejon wants to merge 4 commits into
mainfrom
refactor/python-complexity-analysis
Open

MARISSA MOLEJON | OCT2025-1 | Module-Complexity | Sprint 1 | Refactor/python complexity analysis#3
marissamolejon wants to merge 4 commits into
mainfrom
refactor/python-complexity-analysis

Conversation

@marissamolejon

Copy link
Copy Markdown
Owner

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

Function Before After Change type
calculate_sum_and_product O(n) time, O(1) space O(n) time, O(1) space Removed a redundant empty-list guard clause (dead code — the loop already handles it), renamed sum to total to avoid shadowing Python's built-in sum(), merged two sequential loops into one pass.
find_common_items O(n·m·k) worst case (nested loop plus an in check against a growing list) O(n+m) Replaced the nested loop and list-based membership check with two sets: one built from the second sequence for O(1) lookup, one for de-duplicating the result.
has_pair_with_sum O(n²) O(n) Replaced nested-loop pairwise comparison with a single pass using a set to check each number's complement (target_sum - number).
remove_duplicates O(n²) O(n) Replaced the inner linear scan through the result list with a set for 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 -v in each function's folder — all
green (2, 3, 3, and 4 tests respectively).

Learning points

  • find_common_items was the standout case: a nested loop (O(n·m)) plus
    an i not in common_items check on a growing list (O(k) per check)
    compounded into roughly O(n·m·k) — worse than a "plain" nested loop,
    and not obvious without tracing through what in costs on a list
    versus a set.
  • Removing dead code (the empty-list guard in 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.
  • Applied the same JS -> Python translation of the "seen set" pattern
    across has_pair_with_sum and remove_duplicates, reinforcing that the
    algorithmic idea (O(n²) -> O(n) via a hash-based lookup) is
    language-independent, even though the syntax isn't.

Checklist

  • All tests pass
  • Complexity documented for every function
  • No test files modified
  • Self-reviewed for readability, naming, and duplication

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant