Skip to content

Commit

Permalink
Initialize p884 uncommon_words_from_two_sentences.py
Browse files Browse the repository at this point in the history
  • Loading branch information
yanqd0 committed Sep 22, 2019
1 parent 02310a1 commit c4cee59
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 0 deletions.
3 changes: 3 additions & 0 deletions python/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@
1. No.151 [Reverse Words in a String](https://leetcode.com/problems/reverse-words-in-a-string/) | [翻转字符串里的单词](https://leetcode-cn.com/problems/reverse-words-in-a-string/)
- Code: [reverse_words_in_a_string.py](./src/leetcode/reverse_words_in_a_string.py)
- Test: [reverse_words_in_a_string.py](./tests/reverse_words_in_a_string.py)
1. No.884 [Uncommon Words from Two Sentences](https://leetcode.com/problems/uncommon-words-from-two-sentences/) | [两句话中的不常见单词](https://leetcode-cn.com/problems/uncommon-words-from-two-sentences/)
- Code: [spiral_matrix_iii.py](./src/leetcode/spiral_matrix_iii.py)
- Test: [spiral_matrix_iii.py](./tests/spiral_matrix_iii.py)
1. No.885 [Spiral Matrix III](https://leetcode.com/problems/spiral-matrix-iii/) | [螺旋矩阵 III](https://leetcode-cn.com/problems/spiral-matrix-iii/)
- Code: [spiral_matrix_iii.py](./src/leetcode/spiral_matrix_iii.py)
- Test: [spiral_matrix_iii.py](./tests/spiral_matrix_iii.py)
Expand Down
28 changes: 28 additions & 0 deletions python/src/leetcode/uncommon_words_from_two_sentences.py
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.
11 changes: 11 additions & 0 deletions python/tests/uncommon_words_from_two_sentences.py
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)
7 changes: 7 additions & 0 deletions test/cases/uncommon_words_from_two_sentences.csv
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'}"
17 changes: 17 additions & 0 deletions test/generators/uncommon_words_from_two_sentences.py
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)

0 comments on commit c4cee59

Please sign in to comment.