generated from CodeYourFuture/Module-Template
-
-
Notifications
You must be signed in to change notification settings - Fork 14
ZA | 25-SDC-July | Luke Manyamazi | Sprint 1 | JavaScript and Python Exercises #61
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
Open
Luke-Manyamazi
wants to merge
5
commits into
CodeYourFuture:main
Choose a base branch
from
Luke-Manyamazi:sprintOne-Exercises
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
70abc4e
updated my gitignore file
Luke-Manyamazi 4a33688
answered JS calculate sum and product
Luke-Manyamazi 8e24202
answered JS find common items
Luke-Manyamazi 04854fa
answered Python has pair with sum exercise
Luke-Manyamazi 10c8c2e
answered 2 python exercises
Luke-Manyamazi 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 |
|---|---|---|
|
|
@@ -2,3 +2,6 @@ | |
| node_modules | ||
| .venv | ||
| __pycache__ | ||
| venv/ | ||
| *.pyc | ||
| .env | ||
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 |
|---|---|---|
| @@ -1,14 +1,50 @@ | ||
| /** | ||
| * Finds common items between two arrays. | ||
| * | ||
| * Time Complexity: | ||
| * Space Complexity: | ||
| * Optimal Time Complexity: | ||
| * Time Complexity: O(N + M) | ||
| * Space Complexity: O(M) | ||
| * Optimal Time Complexity: O(N + M) | ||
| * | ||
| * @param {Array} firstArray - First array to compare | ||
| * @param {Array} secondArray - Second array to compare | ||
| * @returns {Array} Array containing unique common items | ||
| */ | ||
| export const findCommonItems = (firstArray, secondArray) => [ | ||
| ...new Set(firstArray.filter((item) => secondArray.includes(item))), | ||
| ]; | ||
| export const findCommonItems = (firstArray, secondArray) => { | ||
| // 1. Create a Set from the second array. (O(M) time) | ||
| // This allows for O(1) average time lookups using the has() method, | ||
| // as per the typical performance characteristics of Set. | ||
| const secondSet = new Set(secondArray); | ||
|
|
||
| // 2. Filter the first array (O(N) time) using O(1) lookups. | ||
| // The filter uses Set.prototype.has() for O(1) average time complexity. | ||
| // The overall time complexity for the filter step becomes O(N * 1) = O(N). | ||
| const commonItems = firstArray.filter((item) => secondSet.has(item)); | ||
|
|
||
| // 3. Use a final Set to guarantee unique results (O(N) time) and return an array. | ||
| return [...new Set(commonItems)]; | ||
| }; | ||
|
|
||
| /* | ||
| Explanation: | ||
| The function first creates a Set from the second array, which takes O(M) time, | ||
| where M is the length of the second array. This allows for O(1) average time | ||
| lookups when checking for common items. | ||
|
|
||
| Next, it filters the first array, which takes O(N) time, where N is the length | ||
| of the first array. Each lookup in the Set is O(1) on average, so the overall | ||
| time complexity for this step remains O(N). | ||
|
|
||
| Finally, to ensure that the result contains only unique items, a new Set is | ||
| created from the filtered results, which also takes O(N) time in the worst case. | ||
| Converting this Set back to an array is also O(N). | ||
|
|
||
| Overall, the total time complexity is O(N + M), which is optimal for this problem, | ||
| as each element from both arrays must be examined at least once. | ||
|
|
||
| Space Complexity: | ||
| The space complexity is O(M) due to storing the second array in a Set. | ||
| The additional space used for the result array is O(K), where K is the number | ||
| of unique common items, but this is typically less than or equal to M. | ||
|
|
||
| Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set | ||
| */ |
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 |
|---|---|---|
| @@ -1,18 +1,47 @@ | ||
| from typing import List, TypeVar | ||
| from typing import List, TypeVar, Set | ||
|
|
||
| Number = TypeVar("Number", int, float) | ||
|
|
||
|
|
||
| def has_pair_with_sum(numbers: List[Number], target_sum: Number) -> bool: | ||
| """ | ||
| Find if there is a pair of numbers that sum to a target value. | ||
| This uses a hash set (Python's set) to achieve O(N) time complexity. | ||
|
|
||
| Time Complexity: | ||
| Space Complexity: | ||
| Optimal time complexity: | ||
| Time Complexity: O(N) | ||
|
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. I think this "Time Complexity" refers to the complexity of the original code, and the optimal one refers to the refactored code. |
||
| Space Complexity: O(N) | ||
| Optimal time complexity: O(N) | ||
| """ | ||
| for i in range(len(numbers)): | ||
| for j in range(i + 1, len(numbers)): | ||
| if numbers[i] + numbers[j] == target_sum: | ||
| return True | ||
| seen_numbers: Set[Number] = set() | ||
|
|
||
| # Iterate through the list once (O(N) time) | ||
| for current_num in numbers: | ||
| complement = target_sum - current_num | ||
|
|
||
| # Check if the required complement is already in the set (O(1) average time) | ||
| if complement in seen_numbers: | ||
| return True | ||
|
|
||
| # Add the current number to the set for future lookups | ||
| seen_numbers.add(current_num) | ||
|
|
||
| return False | ||
|
|
||
| ''' | ||
| The complexity is driven by the nested for loops | ||
| The bottleneck is the inner loop, which forces us to check every possible pair. | ||
| We can use a Hash Set (a set in Python) to store the numbers we've already seen. | ||
| This allows us to perform lookups in O(1) average time. | ||
|
|
||
| Complexity of Refactor | ||
| Time Complexity: O(N)(Linear Time). | ||
| The function performs a single pass over the N elements, with constant-time operations inside the loop. | ||
| This is the optimal complexity. | ||
|
|
||
| Space Complexity: | ||
| O(N)(Linear Space). We introduce the seen_numbers set, which, in the worst case, | ||
| will store up to N elements from the input list. | ||
|
|
||
| Resources: https://www.w3schools.com/python/ref_set_intersection.asp | ||
|
|
||
| ''' | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,5 @@ | ||
| { | ||
| "dependencies": { | ||
| "pytest": "^1.0.0" | ||
| } | ||
| } |
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 |
|---|---|---|
| @@ -1,25 +1,35 @@ | ||
| from typing import List, Sequence, TypeVar | ||
| from typing import List, Sequence, TypeVar, Set | ||
|
|
||
| ItemType = TypeVar("ItemType") | ||
|
|
||
|
|
||
| def remove_duplicates(values: Sequence[ItemType]) -> List[ItemType]: | ||
| """ | ||
| Remove duplicate values from a sequence, preserving the order of the first occurrence of each value. | ||
| Refactored to use a set for O(1) average time lookups, achieving O(N) overall time complexity. | ||
|
|
||
| Time complexity: | ||
| Space complexity: | ||
| Optimal time complexity: | ||
| Time complexity: O(N) | ||
| Space complexity: O(N) | ||
| Optimal time complexity: O(N) | ||
| """ | ||
| unique_items = [] | ||
| seen: Set[ItemType] = set() | ||
| unique_items: List[ItemType] = [] | ||
|
|
||
| for value in values: | ||
| is_duplicate = False | ||
| for existing in unique_items: | ||
| if value == existing: | ||
| is_duplicate = True | ||
| break | ||
| if not is_duplicate: | ||
| if value not in seen: | ||
| seen.add(value) | ||
| unique_items.append(value) | ||
|
|
||
| return unique_items | ||
|
|
||
| """ | ||
| Explanation: | ||
| The inner loop performs a linear scan through the unique_items list to check for duplicates, which is the source of the high time complexity. | ||
|
|
||
| The only effective way to reduce O(N^2) complexity for this problem is to replace the | ||
| linear search (for existing in unique_items) with an O(1) average time lookup, | ||
| which requires a hash set (set in Python).The optimal approach is to use a set to track seen items, achieving O(N)$ time complexity. | ||
|
|
||
| Resource: https://www.w3schools.com/python/ref_set_intersection.asp | ||
|
|
||
| """ |
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.
Performance can be improved by combining the loops, but the complexity remains O(N).
In complexity analysis, the constant factor is ignored.
N, 2N, or even 100N operations, are all considered as O(N) complexity.