diff options
Diffstat (limited to 'challenge-266/steven-wilson/python/ch-1.py')
| -rw-r--r-- | challenge-266/steven-wilson/python/ch-1.py | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/challenge-266/steven-wilson/python/ch-1.py b/challenge-266/steven-wilson/python/ch-1.py new file mode 100644 index 0000000000..8ed85e183a --- /dev/null +++ b/challenge-266/steven-wilson/python/ch-1.py @@ -0,0 +1,30 @@ +#!/usr/bin/env python3 + +from collections import Counter + + +def uncommon_words(*sentences): + ''' Given two sentences, find all uncommmon words in any order in the + given two sentences. Return ('') if none found. A word is uncommon if it + appears exactly once in one of the sentences and doesn’t appear in other + sentence. + + >>> uncommon_words('Mango is sweet', 'Mango is sour') + ['sweet', 'sour'] + >>> uncommon_words('Mango Mango', 'Orange') + ['Orange'] + >>> uncommon_words('Mango is Mango', 'Orange is Orange') + [''] + ''' + if not sentences: + raise TypeError('Provide at least one sentence.') + + counter = Counter(word for sentence in sentences for word in sentence.split()) + uncommon = [word for word, count in counter.items() if count == 1] + return uncommon if uncommon else [''] + + +if __name__ == "__main__": + import doctest + + doctest.testmod(verbose=True) |
