-
-
Notifications
You must be signed in to change notification settings - Fork 29
Sheffield | 25-SDC-Nov | Sheida Shabankari | Sprint 1 | analyse refactor functions #99
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
base: main
Are you sure you want to change the base?
Changes from 4 commits
121c7aa
403c5c4
3e425da
bd26423
12b3ba1
46c6ea3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,21 +1,30 @@ | ||
| /** | ||
| * Find if there is a pair of numbers that sum to a given target value. | ||
| * | ||
| * Time Complexity: | ||
| * Space Complexity: | ||
| * Optimal Time Complexity: | ||
| * | ||
| * Time Complexity:before refactoring O(n²)- after refactoring O(n log n) | ||
| * Space Complexity:before refactoring O(1)- after refactoring O(1) | ||
| * Optimal Time Complexity:O(n log n) | ||
| * https://www.hellointerview.com/learn/code/two-pointers/overview | ||
| * The refactored solution first sorts the array, which takes O(n log n) time, | ||
| * and then uses the two-pointer technique to scan the array in a single pass (O(n)). The sorting step dominates the overall complexity. | ||
| * @param {Array<number>} numbers - Array of numbers to search through | ||
| * @param {number} target - Target sum to find | ||
| * @returns {boolean} True if pair exists, false otherwise | ||
| */ | ||
| export function hasPairWithSum(numbers, target) { | ||
| for (let i = 0; i < numbers.length; i++) { | ||
| for (let j = i + 1; j < numbers.length; j++) { | ||
| if (numbers[i] + numbers[j] === target) { | ||
| return true; | ||
| } | ||
| } | ||
| numbers.sort((a,b)=>a-b); | ||
| let left=0; | ||
| let right=numbers.length-1; | ||
| while (left<right) { | ||
| const current_sum=numbers[left]+numbers[right]; | ||
| if (current_sum === target){ | ||
| return true;} | ||
| if (current_sum<target){ | ||
| left +=1; | ||
| } else { | ||
| right -=1; | ||
| } | ||
| } | ||
|
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. Currently the spacing around the operators are not very consistent. Suggestion: Enable "Format on save" in VSCode to ensure the code is always properly formatted.
Author
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. Thanks !I will do it. |
||
| return false; | ||
|
|
||
| } | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,36 +1,24 @@ | ||
| /** | ||
| * Remove duplicate values from a sequence, preserving the order of the first occurrence of each value. | ||
| * | ||
| * Time Complexity: | ||
| * Space Complexity: | ||
| * Optimal Time Complexity: | ||
| * | ||
| * Time Complexity:before refactoring O(n²)- after refactoring O(n) | ||
| * Space Complexity:O(n) | ||
| * Optimal Time Complexity:O(n) | ||
|
|
||
| * The original implementation relied on nested loops to detect duplicates, | ||
| *resulting in O(n²) time complexity, which does not scale well for large inputs. | ||
| *The refactored solution uses a Set to perform duplicate checks in constant time. | ||
| *This removes the need for an inner loop and reduces the overall time complexity | ||
| *to O(n), which is optimal for this problem. | ||
| *using https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set | ||
| * @param {Array} inputSequence - Sequence to remove duplicates from | ||
| * @returns {Array} New sequence with duplicates removed | ||
| */ | ||
| export function removeDuplicates(inputSequence) { | ||
| const uniqueItems = []; | ||
| const uniqueItemSet = new Set; | ||
|
|
||
| for ( | ||
| let currentIndex = 0; | ||
| currentIndex < inputSequence.length; | ||
| currentIndex++ | ||
| ) { | ||
| let isDuplicate = false; | ||
| for ( | ||
| let compareIndex = 0; | ||
| compareIndex < uniqueItems.length; | ||
| compareIndex++ | ||
| ) { | ||
| if (inputSequence[currentIndex] === uniqueItems[compareIndex]) { | ||
| isDuplicate = true; | ||
| break; | ||
| } | ||
| } | ||
| if (!isDuplicate) { | ||
| uniqueItems.push(inputSequence[currentIndex]); | ||
| } | ||
| for (let i=0 ;i<inputSequence.length;i++){ | ||
| uniqueItemSet.add(inputSequence[i]); | ||
|
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. A for-of loop is probably cleaner when we don't need to access array index. Better still, we could pass the array to the Set constructor directly.
Author
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. Did it. |
||
| } | ||
|
|
||
| return uniqueItems; | ||
| return [...uniqueItemSet]; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,21 +1,24 @@ | ||
| from typing import List, Sequence, TypeVar | ||
| from typing import List, TypeVar | ||
|
|
||
| ItemType = TypeVar("ItemType") | ||
|
|
||
| # Original version: O(n × m) due to nested loops | ||
| # Refactored version: O(n + m) using a set for constant-time lookups | ||
| # This is optimal and cannot be further reduced because all input | ||
| # elements must be processed at least once. | ||
| # use this link :https://www.w3schools.com/python/ref_set_intersection.asp | ||
|
|
||
| def find_common_items( | ||
| first_sequence: Sequence[ItemType], second_sequence: Sequence[ItemType] | ||
| first_sequence, second_sequence | ||
| ) -> List[ItemType]: | ||
| """ | ||
| Find common items between two arrays. | ||
|
|
||
| Time Complexity: | ||
| Space Complexity: | ||
| Optimal time complexity: | ||
| Time Complexity:O(n + m) | ||
| Space Complexity:O(n + m) | ||
| Optimal time complexity:O(n + m) | ||
| """ | ||
| common_items: List[ItemType] = [] | ||
| for i in first_sequence: | ||
| for j in second_sequence: | ||
| if i == j and i not in common_items: | ||
| common_items.append(i) | ||
| return common_items | ||
| second_set=set(second_sequence) | ||
| first_set=set(first_sequence) | ||
| common_set=first_set.intersection(second_set) | ||
| return list(common_set) |
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.
An O(n) time complexity is possible -- the approach relies on looking up values in a map (hash table).
Can you implement this approach?
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.
Did it.