-
-
Notifications
You must be signed in to change notification settings - Fork 39
Glasgow | 26- SDC-Mar | Taras Mykytiuk | Sprint 1 | Analyse and Refactor Functions #166
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 all commits
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,14 +1,17 @@ | ||
| /** | ||
| * Finds common items between two arrays. | ||
| * | ||
| * Time Complexity: | ||
| * Space Complexity: | ||
| * Optimal Time Complexity: | ||
| * Time Complexity: worst O(N1 * N2) | ||
| * Space Complexity: worst O(N1 + N2) | ||
| * Optimal Time Complexity: worst become: O(N1 + N2) | ||
| * | ||
| * @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) => { | ||
| const firstSet = new Set(firstArray); | ||
| const secondSet = new Set(secondArray); | ||
| secondArray = [...secondSet].filter((element) => firstSet.has(element)) | ||
|
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. Note: Most set implementation supports typical set operations like union and intersect. You could also explore using them. |
||
| return secondArray; | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,20 +1,20 @@ | ||
| /** | ||
| * Find if there is a pair of numbers that sum to a given target value. | ||
| * | ||
| * Time Complexity: | ||
| * Space Complexity: | ||
| * Optimal Time Complexity: | ||
| * Time Complexity: worst O(N!) - factorial | ||
|
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. How did you derive the complexity of O(N!)? |
||
| * Space Complexity: O(numbers.length) | ||
| * Optimal Time Complexity: worth become O(N) | ||
| * | ||
| * @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; | ||
| } | ||
| if (numbers.includes(target - numbers[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. What is the complexity of |
||
| return true; | ||
| } | ||
| } | ||
| return false; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,36 +1,20 @@ | ||
| /** | ||
| * 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: O(N*N) quadratic | ||
| * Space Complexity: O(N) | ||
| * Optimal Time Complexity: become O(N) | ||
| * | ||
| * @param {Array} inputSequence - Sequence to remove duplicates from | ||
| * @returns {Array} New sequence with duplicates removed | ||
| */ | ||
| export function removeDuplicates(inputSequence) { | ||
| const uniqueItems = []; | ||
|
|
||
| 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]); | ||
| inputSequence.forEach((element) => { | ||
| if (!uniqueItems.includes(element)) { | ||
| uniqueItems.push(element); | ||
|
Comment on lines
+13
to
+15
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. The complexity of this implementation is still O(N*N). Can you figure out why? |
||
| } | ||
| } | ||
| }); | ||
|
|
||
| return uniqueItems; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,13 +9,10 @@ def find_common_items( | |
| """ | ||
| Find common items between two arrays. | ||
|
|
||
| Time Complexity: | ||
| Space Complexity: | ||
| Optimal time complexity: | ||
| Time Complexity: worst O(N1 * N2) | ||
| Space Complexity: worst O(N1 + N2) | ||
| Optimal time complexity: worst become: O(N1 + N2) | ||
| """ | ||
| 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 | ||
| firstSet = set(first_sequence) | ||
| secondSet = set(first_sequence) | ||
| return list(firstSet.intersection(secondSet)) | ||
|
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. Good use of built-in function. |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,12 +7,12 @@ 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. | ||
|
|
||
| Time Complexity: | ||
| Space Complexity: | ||
| Optimal time complexity: | ||
| Time Complexity: worst O(N!) - factorial | ||
| Space Complexity: O(numbers.length) | ||
| Optimal time complexity: worth become 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 | ||
| if (numbers[i] - target_sum) in numbers: | ||
|
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 you look up the complexity of |
||
| return True | ||
| return False | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,19 +7,14 @@ def remove_duplicates(values: Sequence[ItemType]) -> List[ItemType]: | |
| """ | ||
| 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: O(N*N) quadratic | ||
| Space complexity: O(N) | ||
| Optimal time complexity: become O(N) | ||
| """ | ||
| unique_items = [] | ||
|
|
||
| unique_items = [] | ||
| 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 unique_items: | ||
| unique_items.append(value) | ||
|
Comment on lines
+17
to
18
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.
|
||
|
|
||
| return unique_items | ||
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.