NW | 26-SDC-Mar | TzeMing Ho | Sprint 1 | Analyse and Refactor Functions#164
NW | 26-SDC-Mar | TzeMing Ho | Sprint 1 | Analyse and Refactor Functions#164TzeMingHo wants to merge 13 commits into
Conversation
| targetSet = set() | ||
|
|
||
| for num in numbers: | ||
| targetNum = target_sum - num |
There was a problem hiding this comment.
Note: snake_case is the standard convention for variables and functions in Python.
| unique_items.append(value) | ||
|
|
||
| return unique_items |
There was a problem hiding this comment.
Why not use the same approach you used in Sprint-1/JavaScript/removeDuplicates/removeDuplicates.mjs?
There was a problem hiding this comment.
I tried to return list(set(values)). I thought it would have been the same as JavaScript. However, it can't pass the test, as the order of the list is different from the test case. So, I had to add the element to the list one by one.
There was a problem hiding this comment.
Right. Python Set does not preserve order. There is, however, another built-in Python class that we can use to produce a single line solution.
| firstSet = {*first_sequence} | ||
|
|
||
| commonInSecond = filter(lambda item: item in firstSet, second_sequence) | ||
|
|
||
| return list({*commonInSecond}) No newline at end of file |
There was a problem hiding this comment.
Could also use the built-in Set intersect operation.
| common_in_second = filter(lambda item: item in first_set, second_sequence) | ||
|
|
||
| return list(set(common_in_second)) |
There was a problem hiding this comment.
I meant, using this https://docs.python.org/3/library/stdtypes.html#frozenset.intersection
And there is even an overridden operator, &, for set intersection.
| # return common_items | ||
|
|
||
| firstSet = {*first_sequence} | ||
| first_set = set(first_sequence) |
Learners, PR Template
Self checklist
Changelist
I have attempted the exercises with comments to explain in big O, and tried to optimize the function if possible.