aboutsummaryrefslogtreecommitdiff
path: root/challenge-279/steven-wilson/python
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2024-07-23 16:30:58 +0100
committerGitHub <noreply@github.com>2024-07-23 16:30:58 +0100
commit9b74ec607f269641387884e9d0f72b5c378cef9b (patch)
treeec1b74d8cfd3f7c927c584a72bc24f9eb4bbd463 /challenge-279/steven-wilson/python
parentb84620c724d2b39eb6958d18d1e6c9178769b388 (diff)
parente3042c4327200990cfc2d05943479f2aa713aa80 (diff)
downloadperlweeklychallenge-club-9b74ec607f269641387884e9d0f72b5c378cef9b.tar.gz
perlweeklychallenge-club-9b74ec607f269641387884e9d0f72b5c378cef9b.tar.bz2
perlweeklychallenge-club-9b74ec607f269641387884e9d0f72b5c378cef9b.zip
Merge pull request #10487 from oWnOIzRi/week279
add solutions week 279 in python
Diffstat (limited to 'challenge-279/steven-wilson/python')
-rw-r--r--challenge-279/steven-wilson/python/ch-1.py20
-rw-r--r--challenge-279/steven-wilson/python/ch-2.py35
2 files changed, 55 insertions, 0 deletions
diff --git a/challenge-279/steven-wilson/python/ch-1.py b/challenge-279/steven-wilson/python/ch-1.py
new file mode 100644
index 0000000000..755c3b4141
--- /dev/null
+++ b/challenge-279/steven-wilson/python/ch-1.py
@@ -0,0 +1,20 @@
+#!/usr/bin/env python3
+
+
+def sort_letters(letters, weights):
+ """ Given two arrays, sort the given array letters based on the weights.
+ >>> sort_letters(('R', 'E', 'P', 'L'), (3, 2, 1, 4))
+ 'PERL'
+ >>> sort_letters(('A', 'U', 'R', 'K'), (2, 4, 1, 3))
+ 'RAKU'
+ >>> sort_letters(('O', 'H', 'Y', 'N', 'P', 'T'), (5, 4, 2, 6, 1, 3))
+ 'PYTHON'
+ """
+ letters_sorted = (l for _, l in sorted(zip(weights, letters)))
+ return "".join(letters_sorted)
+
+
+if __name__ == "__main__":
+ import doctest
+
+ doctest.testmod(verbose=True)
diff --git a/challenge-279/steven-wilson/python/ch-2.py b/challenge-279/steven-wilson/python/ch-2.py
new file mode 100644
index 0000000000..be2b25d38f
--- /dev/null
+++ b/challenge-279/steven-wilson/python/ch-2.py
@@ -0,0 +1,35 @@
+#!/usr/bin/env python3
+
+from pprint import pprint
+
+
+def split_string(string, print_strings=False):
+ """ Given a string, split the given string into two containing exactly same
+ number of vowels and return true if you can otherwise false.
+ >>> split_string("perl")
+ False
+ >>> split_string("book")
+ True
+ >>> split_string("good morning")
+ True
+ """
+ if len(string) < 2:
+ return False
+
+ position_vowels = [i for i, c in enumerate(string) if c.casefold() in 'aeiou']
+
+ if not len(position_vowels) % 2 == 0:
+ return False
+
+ if print_strings:
+ split_index = int(len(position_vowels)/2) - 1
+ (start, stop) = (position_vowels[split_index], position_vowels[split_index+1])
+ pprint([(string[:i+1], string[i+1:]) for i in range(start, stop)])
+
+ return True
+
+
+if __name__ == "__main__":
+ import doctest
+
+ doctest.testmod(verbose=True)