From 2e8b8ba919c07325dfa83115b4c1eef09a3be085 Mon Sep 17 00:00:00 2001 From: Simon Green Date: Sun, 14 Jul 2024 23:01:13 +1000 Subject: sgreen solutions to challenge 277 --- challenge-277/sgreen/python/ch-1.py | 32 ++++++++++++++++++++++++++++++++ challenge-277/sgreen/python/ch-2.py | 27 +++++++++++++++++++++++++++ challenge-277/sgreen/python/test.py | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 91 insertions(+) create mode 100755 challenge-277/sgreen/python/ch-1.py create mode 100755 challenge-277/sgreen/python/ch-2.py create mode 100755 challenge-277/sgreen/python/test.py (limited to 'challenge-277/sgreen/python') 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() -- cgit