-
-
Notifications
You must be signed in to change notification settings - Fork 58
London| 26 March SDC | Jamal Laqdiem | Sprint 1 | Refactor Functions #196
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 2 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,21 @@ | ||
| /** | ||
| * Finds common items between two arrays. | ||
| * | ||
| * Time Complexity: | ||
| * Space Complexity: | ||
| * Optimal Time Complexity: | ||
| * Time Complexity:O(n*m) | ||
| * Space Complexity:O(u) | ||
| * 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) => { | ||
| const allowedItems = new Set(secondArray); | ||
|
|
||
| const uniqueValues = [ | ||
| ...new Set(firstArray.filter((item) => allowedItems.has(item))), | ||
| ]; | ||
| return uniqueValues; | ||
| }; | ||
|
|
||
| console.log(findCommonItems([2, 5, 7], [5, 6, 7])); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,21 +1,25 @@ | ||
| /** | ||
| * Find if there is a pair of numbers that sum to a given target value. | ||
| * | ||
| * Time Complexity: | ||
| * Space Complexity: | ||
| * Optimal Time Complexity: | ||
| * Time Complexity:O(n^2) | ||
| * Space Complexity:O(1) | ||
| * Optimal Time Complexity: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; | ||
| } | ||
| const seen = new Set(); | ||
|
|
||
| for (let num of numbers) { | ||
| const complement = target - num; | ||
| if (seen.has(complement)) { | ||
| return true; | ||
| } | ||
| seen.add(num); | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| console.log(hasPairWithSum([1, 2, 3], 4)); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,36 +1,15 @@ | ||
| /** | ||
| * 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^2) | ||
| * Space Complexity:O(n) | ||
| * Optimal Time Complexity: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]); | ||
| } | ||
| } | ||
|
|
||
| return uniqueItems; | ||
| return [...new Set(inputSequence)]; | ||
| } | ||
|
|
||
| console.log(removeDuplicates([3, 3, 5, 6, 3, 7])); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,13 +9,16 @@ def find_common_items( | |
| """ | ||
| Find common items between two arrays. | ||
|
|
||
| Time Complexity: | ||
| Space Complexity: | ||
| Optimal time complexity: | ||
| Time Complexity: O(n^3) | ||
| Space Complexity:O(u) | ||
| Optimal time complexity: O(n+m) | ||
|
Comment on lines
+12
to
+14
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.
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. You are completely right about the inconsistency between old and new Time complex:
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 much space needed by
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. Now I got it, n the original script first_set and second_set do not exist in that implementation, so they take up no additional space. 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. It wasn't clear whether the space complexity analysis is for the original or the new implementation. In that case, all good. |
||
| """ | ||
| 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 | ||
|
|
||
| first_set = set(first_sequence) | ||
| second_set = set(second_sequence) | ||
| # we use the & operator shortcut, of The intersection() method | ||
| # to return a set that contains the similarity between two or more sets | ||
| common_items= first_set & second_set | ||
| return list(common_items) | ||
|
|
||
| print(find_common_items([1,3,5,4],[1,4,8,0])) | ||
Uh oh!
There was an error while loading. Please reload this page.