aboutsummaryrefslogtreecommitdiff
path: root/challenge-277/sgreen/python
diff options
context:
space:
mode:
authorRyan Thompson <rjt-pl@users.noreply.github.com>2024-07-31 09:03:36 -0600
committerGitHub <noreply@github.com>2024-07-31 09:03:36 -0600
commit8ad894d37f0f4ef50a43241c4b2bf2a1cb6d37c1 (patch)
tree1c9f93dfd881614c5e29d50fdde54132ced01281 /challenge-277/sgreen/python
parent7e59eae7a697c2d04e5b64fbef42b87e1a104544 (diff)
parente9916addb3e14008166982fce954d0dc8fd3bafa (diff)
downloadperlweeklychallenge-club-8ad894d37f0f4ef50a43241c4b2bf2a1cb6d37c1.tar.gz
perlweeklychallenge-club-8ad894d37f0f4ef50a43241c4b2bf2a1cb6d37c1.tar.bz2
perlweeklychallenge-club-8ad894d37f0f4ef50a43241c4b2bf2a1cb6d37c1.zip
Merge branch 'manwar:master' into master
Diffstat (limited to 'challenge-277/sgreen/python')
-rwxr-xr-xchallenge-277/sgreen/python/ch-1.py32
-rwxr-xr-xchallenge-277/sgreen/python/ch-2.py27
-rwxr-xr-xchallenge-277/sgreen/python/test.py32
3 files changed, 91 insertions, 0 deletions
diff --git a/challenge-277/sgreen/python/ch-1.py b/challenge-277/sgreen/python/ch-1.py
new file mode 100755
index 0000000000..0338922088
--- /dev/null
+++ b/challenge-277/sgreen/python/ch-1.py
@@ -0,0 +1,32 @@
+#!/usr/bin/env python3
+
+from collections import Counter
+import sys
+
+
+def count_common(*words_list: list[list]) -> int:
+ count = 0
+
+ # Take a list of unique words in the first list
+ for word in set(words_list[0]):
+ # Check that it occurs once in each list
+ for words in words_list:
+ if words.count(word) != 1:
+ # It doesn't, go to the next word.
+ break
+ else:
+ # It does, add to the count
+ count+=1
+
+ # Count the number of words that appear in all lists
+ return count
+
+def main():
+ # Convert two space-separated strings into muliple lists
+ word_list = (words.split(' ')for words in sys.argv[1:])
+ result = count_common(*word_list)
+ print(result)
+
+
+if __name__ == '__main__':
+ main()
diff --git a/challenge-277/sgreen/python/ch-2.py b/challenge-277/sgreen/python/ch-2.py
new file mode 100755
index 0000000000..7896268051
--- /dev/null
+++ b/challenge-277/sgreen/python/ch-2.py
@@ -0,0 +1,27 @@
+#!/usr/bin/env python3
+
+import sys
+
+
+def strong_pair(ints: list) -> int:
+ # Remove duplicates
+ ints = list(set(ints))
+ count = 0
+
+ for i in range(len(ints)-1):
+ for j in range(i+1, len(ints)):
+ if abs(ints[i]-ints[j]) < min(ints[i], ints[j]):
+ count += 1
+
+ return count
+
+
+def main():
+ # Convert input into integers
+ array = [int(n) for n in sys.argv[1:]]
+ result = strong_pair(array)
+ print(result)
+
+
+if __name__ == '__main__':
+ main()
diff --git a/challenge-277/sgreen/python/test.py b/challenge-277/sgreen/python/test.py
new file mode 100755
index 0000000000..fd5e90dbf2
--- /dev/null
+++ b/challenge-277/sgreen/python/test.py
@@ -0,0 +1,32 @@
+#!/usr/bin/env python3
+
+import unittest
+ch_1 = __import__('ch-1')
+ch_2 = __import__('ch-2')
+
+
+class TestClass(unittest.TestCase):
+ def test_ch_1(self):
+ self.assertEqual(
+ ch_1.count_common(
+ ["Perl", "is", "my", "friend"],
+ ["Perl", "and", "Raku", "are", "friend"]),
+ 2)
+ self.assertEqual(
+ ch_1.count_common(
+ ["Perl", "and", "Python", "are", "very", "similar"],
+ ["Python", "is", "top", "in", "guest", "languages"]),
+ 1)
+ self.assertEqual(
+ ch_1.count_common(
+ ["Perl", "is", "imperative", "Lisp", "is", "functional"],
+ ["Crystal", "is", "similar", "to", "Ruby"]),
+ 0)
+
+ def test_ch_2(self):
+ self.assertEqual(ch_2.strong_pair([1, 2, 3, 4, 5]), 4)
+ self.assertEqual(ch_2.strong_pair([5, 7, 1]), 1)
+
+
+if __name__ == '__main__':
+ unittest.main()