aboutsummaryrefslogtreecommitdiff
path: root/challenge-256/sgreen/python
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-256/sgreen/python')
-rwxr-xr-xchallenge-256/sgreen/python/ch-1.py35
-rwxr-xr-xchallenge-256/sgreen/python/ch-2.py26
-rwxr-xr-xchallenge-256/sgreen/python/test.py21
3 files changed, 82 insertions, 0 deletions
diff --git a/challenge-256/sgreen/python/ch-1.py b/challenge-256/sgreen/python/ch-1.py
new file mode 100755
index 0000000000..e84c83e639
--- /dev/null
+++ b/challenge-256/sgreen/python/ch-1.py
@@ -0,0 +1,35 @@
+#!/usr/bin/env python3
+
+import sys
+
+
+def count_pairs(words: list) -> int:
+ """Count the number of pairs of strings where one is reversed
+
+ Args:
+ words (list): A list of words
+
+ Returns:
+ int: The number of pairs
+ """
+ count = 0
+
+ while words:
+ # Take one word from the list
+ word = words.pop()
+
+ # See if the reverse of it is also in the list
+ if word[::-1] in words:
+ count += 1
+
+ # Return the number of pairs
+ return count
+
+
+def main():
+ result = count_pairs(sys.argv[1:])
+ print(result)
+
+
+if __name__ == '__main__':
+ main()
diff --git a/challenge-256/sgreen/python/ch-2.py b/challenge-256/sgreen/python/ch-2.py
new file mode 100755
index 0000000000..051efa1d4c
--- /dev/null
+++ b/challenge-256/sgreen/python/ch-2.py
@@ -0,0 +1,26 @@
+#!/usr/bin/env python3
+
+import sys
+from itertools import zip_longest
+
+
+def merge_strings(words: list) -> str:
+ """Merge strings together by taking the first character from each word,
+ then the second, and so on.
+
+ Args:
+ words (list): A list of words
+
+ Returns:
+ str: The merged string
+ """
+ return ''.join(''.join(s) for s in zip_longest(*words, fillvalue=''))
+
+
+def main():
+ result = merge_strings(sys.argv[1:])
+ print(result)
+
+
+if __name__ == '__main__':
+ main()
diff --git a/challenge-256/sgreen/python/test.py b/challenge-256/sgreen/python/test.py
new file mode 100755
index 0000000000..ae0ffaef4e
--- /dev/null
+++ b/challenge-256/sgreen/python/test.py
@@ -0,0 +1,21 @@
+#!/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_pairs(["ab", "de", "ed", "bc"]), 1)
+ self.assertEqual(ch_1.count_pairs(["aa", "ba", "cd", "ed"]), 0)
+ self.assertEqual(ch_1.count_pairs(["uv", "qp", "st", "vu", "mn", "pq"]), 2)
+
+ def test_ch_2(self):
+ self.assertEqual(ch_2.merge_strings(["abcd", "1234"]), "a1b2c3d4")
+ self.assertEqual(ch_2.merge_strings(["abc", "12345"]), "a1b2c345")
+ self.assertEqual(ch_2.merge_strings(["abcde", "123"]), "a1b2c3de")
+
+
+if __name__ == "__main__":
+ unittest.main()