diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-07-16 11:43:14 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-07-16 11:43:14 +0100 |
| commit | 7afd76cd18a98ae3b3535cfd90d292a88ac8b9a2 (patch) | |
| tree | 3add2739160fb596d1a24b4d0b455ebadc8f18b4 /challenge-278/pokgopun/python/ch-2.py | |
| parent | 6d0af37dbeba795de3a812874f1d711995d16c2d (diff) | |
| parent | a92039a93c92921d5522ca3e43eee35b3a29ea75 (diff) | |
| download | perlweeklychallenge-club-7afd76cd18a98ae3b3535cfd90d292a88ac8b9a2.tar.gz perlweeklychallenge-club-7afd76cd18a98ae3b3535cfd90d292a88ac8b9a2.tar.bz2 perlweeklychallenge-club-7afd76cd18a98ae3b3535cfd90d292a88ac8b9a2.zip | |
Merge pull request #10435 from pokgopun/pwc278
Pwc278
Diffstat (limited to 'challenge-278/pokgopun/python/ch-2.py')
| -rw-r--r-- | challenge-278/pokgopun/python/ch-2.py | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/challenge-278/pokgopun/python/ch-2.py b/challenge-278/pokgopun/python/ch-2.py new file mode 100644 index 0000000000..a5e1e34059 --- /dev/null +++ b/challenge-278/pokgopun/python/ch-2.py @@ -0,0 +1,55 @@ +### https://theweeklychallenge.org/blog/perl-weekly-challenge-278/ +""" + +Task 2: Reverse Word + +Submitted by: [52]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given a word, $word and a character, $char. + + Write a script to replace the substring up to and including $char with + its characters sorted alphabetically. If the $char doesn’t exist then + DON'T do anything. + +Example 1 + +Input: $str = "challenge", $char = "e" +Ouput: "acehllnge" + +Example 2 + +Input: $str = "programming", $char = "a" +Ouput: "agoprrmming" + +Example 3 + +Input: $str = "champion", $char = "b" +Ouput: "champion" + __________________________________________________________________ + + Last date to submit the solution 23:59 (UK Time) Sunday 21st July 2024. + __________________________________________________________________ + +SO WHAT DO YOU THINK ? +""" +### solution by pokgopun@gmail.com + +def reverseWord(word, char): + i = word.find(char) + if i < 0: + return word + return "".join(sorted(word[:i+1]))+word[i+1:] + +import unittest + +class TestReverseWord(unittest.TestCase): + def test(self): + for (word,char), otpt in { + ("challenge", "e"): "acehllnge", + ("programming", "a"):"agoprrmming", + ("champion", "b"): "champion", + }.items(): + self.assertEqual(reverseWord(word,char), otpt) + +unittest.main() |
