aboutsummaryrefslogtreecommitdiff
path: root/challenge-266/steven-wilson/python/ch-1.py
diff options
context:
space:
mode:
authorSteven <steven1170@zoho.eu>2024-04-22 17:27:40 +0100
committerSteven <steven1170@zoho.eu>2024-04-22 17:27:40 +0100
commit0b485706acdd66590b293ff90d7f6a4b489d55c2 (patch)
treeae9280bf4c5d36a4c9f2c77d0a93989e2d3f7ee8 /challenge-266/steven-wilson/python/ch-1.py
parentaed41ed119210ab32e32b82af8039f2c1457726b (diff)
downloadperlweeklychallenge-club-0b485706acdd66590b293ff90d7f6a4b489d55c2.tar.gz
perlweeklychallenge-club-0b485706acdd66590b293ff90d7f6a4b489d55c2.tar.bz2
perlweeklychallenge-club-0b485706acdd66590b293ff90d7f6a4b489d55c2.zip
add solutions week 266 in python
Diffstat (limited to 'challenge-266/steven-wilson/python/ch-1.py')
-rw-r--r--challenge-266/steven-wilson/python/ch-1.py30
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)