aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2024-02-13 01:05:38 +0000
committerGitHub <noreply@github.com>2024-02-13 01:05:38 +0000
commitaf54bc1c86be3171ab78fcc7eff1c99de62044c5 (patch)
tree09f3e0e2f33add8c4dad67b89c705c7a9158ac30
parentc1feab0cc06db96f9e1ce9e137fc18a03dc7fff5 (diff)
parent2b637ebaee0ee2c82474f2364e5a4cfa0fb7d828 (diff)
downloadperlweeklychallenge-club-af54bc1c86be3171ab78fcc7eff1c99de62044c5.tar.gz
perlweeklychallenge-club-af54bc1c86be3171ab78fcc7eff1c99de62044c5.tar.bz2
perlweeklychallenge-club-af54bc1c86be3171ab78fcc7eff1c99de62044c5.zip
Merge pull request #9569 from oWnOIzRi/week256
add solutions week 256 in python
-rw-r--r--challenge-256/steven-wilson/python/ch-1.py27
-rw-r--r--challenge-256/steven-wilson/python/ch-2.py25
2 files changed, 52 insertions, 0 deletions
diff --git a/challenge-256/steven-wilson/python/ch-1.py b/challenge-256/steven-wilson/python/ch-1.py
new file mode 100644
index 0000000000..c36d2c3f51
--- /dev/null
+++ b/challenge-256/steven-wilson/python/ch-1.py
@@ -0,0 +1,27 @@
+#!/usr/bin/env python3
+
+
+def maximum_pairs(*words):
+ ''' Given an array of distinct words. Find the maximum pairs in the given
+ array. The words $words[i] and $words[j] can be a pair one is reverse of
+ the other.
+ >>> maximum_pairs("ab", "de", "ed", "bc")
+ 1
+ >>> maximum_pairs("aa", "ba", "cd", "ed")
+ 0
+ >>> maximum_pairs("uv", "qp", "st", "vu", "mn", "pq")
+ 2
+ '''
+
+ words_set = set(words)
+ if not len(words) == len(words_set):
+ raise AssertionError("Array of words were not distinct")
+
+ sum_pairs = sum(1 for word in words if word[::-1] in words_set)
+ return int(sum_pairs/2)
+
+
+if __name__ == "__main__":
+ import doctest
+
+ doctest.testmod()
diff --git a/challenge-256/steven-wilson/python/ch-2.py b/challenge-256/steven-wilson/python/ch-2.py
new file mode 100644
index 0000000000..dea6e0dc6d
--- /dev/null
+++ b/challenge-256/steven-wilson/python/ch-2.py
@@ -0,0 +1,25 @@
+#!/usr/bin/env python3
+
+from itertools import chain
+
+
+def merge_strings(first, second):
+ ''' Given two strings, merge the given strings by adding in alternative
+ order starting with the first string. If a string is longer than the other
+ then append the remaining at the end.
+ >>> merge_strings("abcd", "1234")
+ 'a1b2c3d4'
+ >>> merge_strings("abc", "12345")
+ 'a1b2c345'
+ >>> merge_strings("abcde", "123")
+ 'a1b2c3de'
+ '''
+ return ("".join(chain.from_iterable(zip(first, second))) +
+ first[len(second):] +
+ second[len(first):])
+
+
+if __name__ == "__main__":
+ import doctest
+
+ doctest.testmod()