aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Manring <michael@manring>2024-07-22 12:49:16 +1000
committerMichael Manring <michael@manring>2024-07-22 12:49:16 +1000
commitdd7263a3d97abb4546bebec943af4f33a8427746 (patch)
tree90124d053561da0d5ac2a521de5aa84977173a24
parentf59c17748d25a6f16a7e64cc3568ae4190f9f3fb (diff)
downloadperlweeklychallenge-club-dd7263a3d97abb4546bebec943af4f33a8427746.tar.gz
perlweeklychallenge-club-dd7263a3d97abb4546bebec943af4f33a8427746.tar.bz2
perlweeklychallenge-club-dd7263a3d97abb4546bebec943af4f33a8427746.zip
pwc279 solution in python
-rw-r--r--challenge-279/pokgopun/python/ch-1.py53
-rw-r--r--challenge-279/pokgopun/python/ch-2.py73
2 files changed, 126 insertions, 0 deletions
diff --git a/challenge-279/pokgopun/python/ch-1.py b/challenge-279/pokgopun/python/ch-1.py
new file mode 100644
index 0000000000..d68c18c09b
--- /dev/null
+++ b/challenge-279/pokgopun/python/ch-1.py
@@ -0,0 +1,53 @@
+### https://theweeklychallenge.org/blog/perl-weekly-challenge-279/
+"""
+
+Task 1: Sort Letters
+
+Submitted by: [45]Mohammad Sajid Anwar
+ __________________________________________________________________
+
+ You are given two arrays, @letters and @weights.
+
+ Write a script to sort the given array @letters based on the @weights.
+
+Example 1
+
+Input: @letters = ('R', 'E', 'P', 'L')
+ @weights = (3, 2, 1, 4)
+Output: PERL
+
+Example 2
+
+Input: @letters = ('A', 'U', 'R', 'K')
+ @weights = (2, 4, 1, 3)
+Output: RAKU
+
+Example 3
+
+Input: @letters = ('O', 'H', 'Y', 'N', 'P', 'T')
+ @weights = (5, 4, 2, 6, 1, 3)
+Output: PYTHON
+
+Task 2: Split String
+"""
+### solution by pokgopun@gmail.com
+
+def sortLetters(letters,weights):
+ return "".join(
+ e[1] for e in sorted(
+ (weights[i],letters[i]) for i in range(len(letters))
+ )
+ )
+
+import unittest
+
+class TestSortLetters(unittest.TestCase):
+ def test(self):
+ for (letters,weights),otpt in {
+ (('R', 'E', 'P', 'L'),(3, 2, 1, 4)): "PERL",
+ (('A', 'U', 'R', 'K'),(2, 4, 1, 3)): "RAKU",
+ (('O', 'H', 'Y', 'N', 'P', 'T'),(5, 4, 2, 6, 1, 3)): "PYTHON",
+ }.items():
+ self.assertEqual(sortLetters(letters,weights),otpt)
+
+unittest.main()
diff --git a/challenge-279/pokgopun/python/ch-2.py b/challenge-279/pokgopun/python/ch-2.py
new file mode 100644
index 0000000000..129bc186bb
--- /dev/null
+++ b/challenge-279/pokgopun/python/ch-2.py
@@ -0,0 +1,73 @@
+### https://theweeklychallenge.org/blog/perl-weekly-challenge-279/
+"""
+
+Task 2: Split String
+
+Submitted by: [46]Mohammad Sajid Anwar
+ __________________________________________________________________
+
+ You are given a string, $str.
+
+ Write a script to split the given string into two containing exactly
+ same number of vowels and return true if you can otherwise false.
+
+Example 1
+
+Input: $str = "perl"
+Ouput: false
+
+Example 2
+
+Input: $str = "book"
+Ouput: true
+
+Two possible strings "bo" and "ok" containing exactly one vowel each.
+
+Example 3
+
+Input: $str = "good morning"
+Ouput: true
+
+Two possible strings "good " and "morning" containing two vowels each or "good m
+" and "orning" containing two vowels each.
+ __________________________________________________________________
+
+ Last date to submit the solution 23:59 (UK Time) Sunday 28th July 2024.
+ __________________________________________________________________
+
+SO WHAT DO YOU THINK ?
+"""
+### solution by pokgopun@gmail.com
+
+def splitString0(string: str):
+ return sum( 1 for i in range(len(string)) if string[i] in "aeiou" ) % 2 == 0
+
+def splitString1(string: str):
+ idx = tuple( i for i in range(len(string)) if string[i] in "aeiou" )
+ l = len(idx)
+ h = int(l/2)
+ b = l % 2 == 0
+ if b:
+ h -= 1
+ i = idx[h+1] if l > 1 else idx[h]+1
+ return (string[:i], string[i:], b)
+
+import unittest
+
+class TestSplitString(unittest.TestCase):
+ def test0(self):
+ for inpt,otpt in {
+ "perl": False,
+ "book": True,
+ "good morning": True,
+ }.items():
+ self.assertEqual(splitString0(inpt),otpt)
+ def test1(self):
+ for inpt,otpt in {
+ "perl": ("pe","rl",False),
+ "book": ("bo","ok",True),
+ "good morning": ("good m","orning",True),
+ }.items():
+ self.assertEqual(splitString1(inpt),otpt)
+
+unittest.main()