West Midlands | 26 March SDC | Iswat Bello | Sprint 1 | Analyse and Refactor Functions#186
West Midlands | 26 March SDC | Iswat Bello | Sprint 1 | Analyse and Refactor Functions#186Iswanna wants to merge 10 commits into
Conversation
- Refactored logic to calculate both sum and product in one pass - Reduced iterations from 2n to n while maintaining O(n) time complexity - Maintained O(1) space complexity
- Replaced Array.includes (O(m)) with Set.has (O(1)) inside the filter loop - Improved time complexity from O(n * m) to O(n + m) - Utilized a Set for precomputing lookups to avoid nested linear searches
- Replaced nested loops (O(n^2)) with a single-pass approach - Implemented a Set to store visited numbers for O(1) complement (remainingNumberNeeded) lookups - Improved performance for large datasets by trading space (O(n)) for time (O(n))
- Replaced nested loops (O(n^2)) with a Set-based lookup approach - Achieved O(n) time complexity by utilizing O(1) Set.has() checks - Maintained the original order of elements while improving scalability - Increased space complexity to O(n) to store the Set of seen elements
…vaScript - Added CHANGES-MADE.md to document performance improvements - Summarized the transition from O(n^2) to O(n) across JavaScript tasks - Explained the Space-Time trade-offs involved in using Sets for optimization
- Combined sum and product calculations into a single for-loop - Renamed 'sum' variable to 'total_sum' to avoid shadowing Python's built-in sum() - Maintained O(n) time complexity while reducing operations from 2n to n - Maintained O(1) space complexity
- Replaced nested loops (O(n * m)) with a Set for constant time lookups - Converted second_sequence to a set to enable O(1) membership checks - Utilized a result set to ensure uniqueness without linear searches in a list - Improved time complexity from quadratic to linear (O(n + m))
- Replaced nested O(n^2) loops with a single-pass hash set approach - Implemented 'remaining_number_needed' logic for O(1) membership lookups - Improved time complexity from quadratic to linear - Accepted O(n) space complexity as a trade-off for significantly faster execution
- Replaced nested loops (O(n^2)) with a single-pass approach using a Set - Leveraged O(1) membership checks with 'element_seen' to identify duplicates - Utilized a result list to preserve the original order of first occurrences - Documented O(n) time and space complexity in the docstring
- Documented refactoring results for the 4 Python exercises - Explained the efficiency of Python sets vs lists for membership checks - Highlighted the transition from quadratic to linear time complexity
cjyuan
left a comment
There was a problem hiding this comment.
Well formatted and clear complexity analysis.
Code looks good. Excellent job!
| export const findCommonItems = (firstArray, secondArray) => { | ||
| const secondArraySet = new Set(secondArray); | ||
|
|
||
| return [...new Set(firstArray.filter((item) => secondArraySet.has(item)))]; |
There was a problem hiding this comment.
Could also take advantage of Set's build-in methods. Set implementation in most programming languages typically supports standard set operations like union, intersect, diff, etc.
There was a problem hiding this comment.
Thanks for the suggestion. I will look into the Set's built-in methods and update the code accordingly.
| for current_number in input_numbers: | ||
| sum += current_number | ||
|
|
||
| total_sum = 0 |
There was a problem hiding this comment.
Thanks for the suggestion. I will update the code accordingly.
|
Closing PR because the SDC run has finished. Feel free to re-open if you're still working on it. |
Thank you so much for the feedback. |
Learners, PR Template
Self checklist
Changelist
This PR completes the Sprint 1 module focused on Time and Space Complexity. I have refactored 4 programming tasks in both JavaScript and Python to improve their algorithmic efficiency.
Technical Changes
The primary focus of this refactor was identifying and eliminating Quadratic Time Complexity
O(n²).forloops and linear array searches (.includes()/in list) with Hash Set lookups.CHANGES-MADE.mdfiles in both language directories to document the specific complexity trade-offs for each task.Key Learnings
sumtototal_sum) and implemented the more efficientinkeyword for Python sets.remaining_number_neededto make the underlying mathematical logic (finding complements) clear to other developers.Verification
nodeandnpm test.pytest.