-
-
Notifications
You must be signed in to change notification settings - Fork 47
West Midlands | 26 March SDC | Iswat Bello | Sprint 1 | Analyse and Refactor Functions #186
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Iswanna
wants to merge
10
commits into
CodeYourFuture:main
from
Iswanna:analyse-and-refactor-functions
Closed
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
1f5b1ae
refactor: combine sum and product into a single loop
Iswanna 783b681
refactor: optimize findCommonItems to linear time complexity
Iswanna c5a6a87
refactor: optimize hasPairWithSum to O(n) time complexity
Iswanna c19d1d4
refactor: optimize removeDuplicates to O(n) time complexity
Iswanna 4d9c4bd
docs: add complexity analysis and refactoring summary for Sprint 1/Ja…
Iswanna 22391c5
refactor: optimize calculate_sum_and_product to single pass (Python)
Iswanna 4308c42
refactor: optimize find_common_items using sets for O(n+m) complexity
Iswanna ae0bae2
refactor: optimize has_pair_with_sum to O(n) using a set (Python)
Iswanna 549cd03
refactor: optimize remove_duplicates to O(n) using a hash set (Python)
Iswanna 6af9b57
docs: add Python-specific complexity analysis for Sprint 1
Iswanna File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| This is a great way to document your learning and show the impact of your refactoring. Below is the content for your `changes-made.md` file, formatted exactly like the comparison table you provided. | ||
|
|
||
| --- | ||
|
|
||
| # Sprint 1: Complexity Analysis & Refactoring | ||
|
|
||
| ## 1. calculateSumAndProduct.js | ||
| | Feature | Original Code | Refactored Code | | ||
| | :--- | :--- | :--- | | ||
| | **Time Complexity** | **Linear $O(n)$** | **Linear $O(n)$** | | ||
| | **Why?** | Two separate loops ($n + n$). | Combined into a single loop ($n$). | | ||
| | **Space Complexity** | **Constant $O(1)$** | **Constant $O(1)$** | | ||
| | **Why?** | Only two scalar variables used. | Only two scalar variables used. | | ||
|
|
||
| --- | ||
|
|
||
| ## 2. findCommonItems.js | ||
| | Feature | Original Code | Refactored Code | | ||
| | :--- | :--- | :--- | | ||
| | **Time Complexity** | **Quadratic $O(n \times m)$** | **Linear $O(n + m)$** | | ||
| | **Why?** | Nested loop: `.includes()` inside `.filter()`. | Set lookup: `.has()` inside `.filter()`. | | ||
| | **Space Complexity** | **Constant $O(1)$** | **Linear $O(m)$** | | ||
| | **Why?** | No extra search structure created. | Created a `Set` to store the second array. | | ||
|
|
||
| --- | ||
|
|
||
| ## 3. hasPairWithSum.js | ||
| | Feature | Original Code | Refactored Code | | ||
| | :--- | :--- | :--- | | ||
| | **Time Complexity** | **Quadratic $O(n^2)$** | **Linear $O(n)$** | | ||
| | **Why?** | Nested `for` loops comparing every pair. | One loop with instant "Complement" lookup. | | ||
| | **Space Complexity** | **Constant $O(1)$** | **Linear $O(n)$** | | ||
| | **Why?** | No extra storage used. | Created a `Set` to store visited numbers. | | ||
|
|
||
| --- | ||
|
|
||
| ## 4. removeDuplicates.js | ||
| | Feature | Original Code | Refactored Code | | ||
| | :--- | :--- | :--- | | ||
| | **Time Complexity** | **Quadratic $O(n^2)$** | **Linear $O(n)$** | | ||
| | **Why?** | Nested loop searching the result array. | One loop using a Set for "Seen" items. | | ||
| | **Space Complexity** | **Linear $O(n)$** | **Linear $O(n)$** | | ||
| | **Why?** | Stored unique items in an array. | Stored unique items in both an array and a Set. | | ||
|
|
||
| --- | ||
|
|
||
| ### Key Takeaway: The Space-Time Trade-off | ||
| In the `findCommonItems.js`, `hasPairWithSum.js` and `removeDuplicates.js` tasks, I successfully reduced the **Time Complexity** from Quadratic to Linear. I did this by choosing a **Set** instead of an **Array** for searching. This is a classic "Space-Time Trade-off": I used a bit more memory (Space) to make the program run significantly faster (Time). | ||
|
|
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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| # Sprint 1: Complexity Analysis & Refactoring (Python) | ||
|
|
||
| ## 1. calculate_sum_and_product.py | ||
| | Feature | Original Code | Refactored Code | | ||
| | :--- | :--- | :--- | | ||
| | **Time Complexity** | **Linear $O(n)$** | **Linear $O(n)$** | | ||
| | **Why?** | Two separate `for` loops ($n + n$). | Combined into a single `for` loop ($n$). | | ||
| | **Space Complexity** | **Constant $O(1)$** | **Constant $O(1)$** | | ||
| | **Why?** | Used two scalar variables. | Used two scalar variables. | | ||
|
|
||
| **Note:** Renamed the variable `sum` to `total_sum` to avoid shadowing Python's built-in `sum()` function. | ||
|
|
||
| --- | ||
|
|
||
| ## 2. find_common_items.py | ||
| | Feature | Original Code | Refactored Code | | ||
| | :--- | :--- | :--- | | ||
| | **Time Complexity** | **Quadratic $O(n \times m)$** | **Linear $O(n + m)$** | | ||
| | **Why?** | Nested loops searching a **list** with the `in` keyword ($O(m)$). | Used a **set** for $O(1)$ membership lookups with the `in` keyword. | | ||
| | **Space Complexity** | **Constant $O(1)$** | **Linear $O(m)$** | | ||
| | **Why?** | No extra storage created. | Created a `set` to store the second sequence for fast lookups. | | ||
|
|
||
| --- | ||
|
|
||
| ## 3. has_pair_with_sum.py | ||
| | Feature | Original Code | Refactored Code | | ||
| | :--- | :--- | :--- | | ||
| | **Time Complexity** | **Quadratic $O(n^2)$** | **Linear $O(n)$** | | ||
| | **Why?** | Nested loops comparing every index. | Single pass with a `set` to find the `remaining_number_needed`. | | ||
| | **Space Complexity** | **Constant $O(1)$** | **Linear $O(n)$** | | ||
| | **Why?** | No extra storage used. | Created a `seen` set to store visited numbers. | | ||
|
|
||
| --- | ||
|
|
||
| ## 4. remove_duplicates.py | ||
| | Feature | Original Code | Refactored Code | | ||
| | :--- | :--- | :--- | | ||
| | **Time Complexity** | **Quadratic $O(n^2)$** | **Linear $O(n)$** | | ||
| | **Why?** | Nested loops checking if an item was `not in` the result **list**. | Used an `element_seen` **set** for $O(1)$ "already seen" checks. | | ||
| | **Space Complexity** | **Linear $O(n)$** | **Linear $O(n)$** | | ||
| | **Why?** | Stored unique items in a list. | Stored items in both a result list (for order) and a set (for speed). | | ||
|
|
||
| --- | ||
|
|
||
| ### Key Python Takeaways | ||
|
|
||
| 1. **The `in` Keyword Efficiency:** In Python, the `in` operator is very readable, but its speed depends on the container. Checking `in` for a **list** is $O(n)$, while checking `in` for a **set** is $O(1)$. Leveraging this difference allowed me to optimize three of the four tasks. | ||
| 2. **Space-Time Trade-off:** To improve the runtime of `find_common_items`, `has_pair_with_sum`, and `remove_duplicates` from $O(n^2)$ to $O(n)$, I utilized extra memory (Space) by creating Python **sets**. | ||
| 3. **Built-in Shadowing:** I learned to be careful with variable naming (e.g., using `total_sum` instead of `sum`) to avoid overwriting Python’s built-in functions. | ||
| 4. **Preserving Order:** In `remove_duplicates`, I learned that while sets are fast, they don't preserve order. Using a list for results and a set for tracking allowed for both $O(n)$ speed and correct ordering. | ||
|
|
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,12 +20,10 @@ def calculate_sum_and_product(input_numbers: List[int]) -> Dict[str, int]: | |
| if not input_numbers: | ||
| return {"sum": 0, "product": 1} | ||
|
|
||
| sum = 0 | ||
| for current_number in input_numbers: | ||
| sum += current_number | ||
|
|
||
| total_sum = 0 | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could just use either |
||
| product = 1 | ||
| for current_number in input_numbers: | ||
| total_sum += current_number | ||
| product *= current_number | ||
|
|
||
| return {"sum": sum, "product": product} | ||
| return {"sum": total_sum, "product": product} | ||
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
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
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.