diff options
| author | Santiago Leyva <99155189+DevSanti12@users.noreply.github.com> | 2024-07-25 23:57:49 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-07-25 23:57:49 -0500 |
| commit | db40d75f603f3fae34944cb3a762b290b18577d8 (patch) | |
| tree | 1205c733ae416bcbce12e225a5252841316c5540 /challenge-279/pokgopun/python/ch-2.py | |
| parent | 350f495b09a4b6b6db8506533e2dccc68d9fc3f4 (diff) | |
| parent | 3d6cbce024396f3950dad9b40151458e4134202d (diff) | |
| download | perlweeklychallenge-club-db40d75f603f3fae34944cb3a762b290b18577d8.tar.gz perlweeklychallenge-club-db40d75f603f3fae34944cb3a762b290b18577d8.tar.bz2 perlweeklychallenge-club-db40d75f603f3fae34944cb3a762b290b18577d8.zip | |
Merge branch 'manwar:master' into master
Diffstat (limited to 'challenge-279/pokgopun/python/ch-2.py')
| -rw-r--r-- | challenge-279/pokgopun/python/ch-2.py | 73 |
1 files changed, 73 insertions, 0 deletions
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() |
