-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initialize p884 uncommon_words_from_two_sentences.py
- Loading branch information
Showing
5 changed files
with
66 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 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,28 @@ | ||
from typing import List | ||
|
||
|
||
class Solution: | ||
def uncommonFromSentences(self, A: str, B: str) -> List[str]: | ||
repeats = set() | ||
a_set = set() | ||
for i in A.split(): | ||
if i in a_set: | ||
repeats.add(i) | ||
else: | ||
a_set.add(i) | ||
b_set = set() | ||
for i in B.split(): | ||
if i in b_set: | ||
repeats.add(i) | ||
else: | ||
b_set.add(i) | ||
|
||
diff = a_set.symmetric_difference(b_set) | ||
return list(diff.difference(repeats)) | ||
|
||
|
||
# Python 3 | ||
# Runtime: 36 ms, faster than 80.46% of Python3 online submissions | ||
# for Uncommon Words from Two Sentences. | ||
# Memory Usage: 14 MB, less than 9.09% of Python3 online submissions | ||
# for Uncommon Words from Two Sentences. |
This file contains 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,11 @@ | ||
from pytest import mark | ||
|
||
from leetcode.uncommon_words_from_two_sentences import Solution | ||
|
||
from . import read_csv | ||
|
||
|
||
@mark.parametrize('a, b, expect', read_csv(__file__)) | ||
def test_two_sum(a, b, expect): | ||
result = Solution().uncommonFromSentences(a, b) | ||
assert set(result) == eval(expect) |
This file contains 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,7 @@ | ||
nums,target,expect | ||
,,set() | ||
apple,,{'apple'} | ||
this apple is sweet,this apple is sour,"{'sour', 'sweet'}" | ||
apple apple,banana,{'banana'} | ||
apple apple,banana banana,set() | ||
apple,banana,"{'apple', 'banana'}" |
This file contains 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,17 @@ | ||
#!/usr/bin/env python | ||
# -*- coding:utf-8 -*- | ||
|
||
FIELDS = ['nums', 'target', 'expect'] | ||
ROWS = [ | ||
('', '', set()), | ||
('apple', '', {'apple'}), | ||
('this apple is sweet', 'this apple is sour', {'sweet', 'sour'}), | ||
('apple apple', 'banana', {'banana'}), | ||
('apple apple', 'banana banana', set()), | ||
('apple', 'banana', {'apple', 'banana'}), | ||
] | ||
|
||
if __name__ == '__main__': | ||
from utils import generate_csv | ||
|
||
generate_csv(__file__, FIELDS, ROWS) |